简体   繁体   English

如何在JTable中填充数据?

[英]How do I fill data in a JTable?

I am trying to update my table, but it is not working. 我正在尝试更新我的表,但是它不起作用。 The table does not show up until the entire program calling it is done. 在调用它的整个程序完成之前,该表不会显示。 How can I change this? 我该如何更改? Upon opening the window, I would like to fill the JTable with data. 打开窗口后,我想用数据填充JTable If I stop the execution of the code, the table is filled with data. 如果我停止执行代码,则表中将填充数据。 Do I need a thread? 我需要线吗? How would I use one correctly? 我如何正确使用一个? My code is below. 我的代码如下。

public class TestGUI extends DefaultTableCellRenderer implements TeststepEventListener {

    public JFrame frame;
    private JTable testcase_table;
    private JTable teststep_table;

    /**
     * Create the application.
     */
    public TestGUI() {
        initialize();
        frame.setVisible(true);
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 800, 600);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        JProgressBar progressBar = new JProgressBar();
        progressBar.setBounds(50, 68, 700, 14);
        frame.getContentPane().add(progressBar);

        JTextPane txtpnAutomotiveHmi = new JTextPane();
        txtpnAutomotiveHmi.setText("Automotive HMI");
        txtpnAutomotiveHmi.setBounds(362, 21, 205, 20);
        frame.getContentPane().add(txtpnAutomotiveHmi);

        testcase_table = new JTable();
        testcase_table.setBounds(50, 125, 350, 426);
        frame.getContentPane().add(testcase_table);

        teststep_table = new JTable();
        teststep_table.setBounds(399, 125, 350, 426);
        frame.getContentPane().add(teststep_table);
    }
    private boolean testcase = true;

    @Override
    public void myEventOccurred(TeststepEvent event) {
        TeststepData data = event.data();
        if (testcase) {
            set_values(data.getDoc(), data.getTestcase());
        }
        testcase = false;

    }
    private int i = 0;
    LinkedList names = new LinkedList();

    private void set_values(Document doc, int testcase) {
        frame.setTitle("Wuratbrot" + i);
        i++;
        Element element = doc.getRootElement();
        names.clear();
        if (element != null) {
            List<Element> testCases = element.getChildren();
            //testcase_table.removeAll();
            //String[] title = {"Testcases"};


            for (Element testCase : testCases) {
                names.add(testCase.getAttributeValue("name"));
            }

            DisplayData(names);
        }

        testcase_table.revalidate();
        frame.validate();
    }

    private void DisplayData(List<String> Testcases) {

        DefaultTableModel aModel = new DefaultTableModel() {
            //setting the jtable read only
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        //setting the column name
        Object[] tableColumnNames = new Object[1];
        tableColumnNames[0] = "TestCases";


        aModel.setColumnIdentifiers(tableColumnNames);
        if (Testcases == null) {

            testcase_table.setModel(aModel);

            return;
        }

        Object[] objects = new Object[1];
        ListIterator<String> lstrg = Testcases.listIterator();
        //populating the tablemodel
        while (lstrg.hasNext()) {
            String newcus = lstrg.next();
            objects[0] = newcus;


            aModel.addRow(objects);
        }

        //binding the jtable to the model
        testcase_table.setModel(aModel);
    }
}

SwingWorker is intended for this. SwingWorker专门用于此目的。 Data acquisition can take place asynchronously in doInBackground() , while process() safely updates the TableModel on the event dispatch thread via publish() . 数据获取可以在doInBackground()异步进行,而process()通过publish()安全地更新事件分发线程上的TableModel In particular, see the section entitled Sample Usage and this tutorial . 特别是,请参阅标题为样本用法的小节和本教程 Moreover, DefaultTableModel fires the appropriate update events for which the JTable listens. 此外, DefaultTableModel触发JTable侦听的适当更新事件。 No additional code should be required. 不需要其他代码。 As an aside, use layouts rather than setBounds() . setBounds() ,使用布局而不是setBounds()

You need the function fireTableDataChanged . 您需要功能fireTableDataChanged You should call it upon your table model after changing the values of table cells. 更改表单元格的值后,应在表模型上调用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM