Question

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. 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);
    }
}
Was it helpful?

Solution

SwingWorker is intended for this. Data acquisition can take place asynchronously in doInBackground(), while process() safely updates the TableModel on the event dispatch thread via publish(). In particular, see the section entitled Sample Usage and this tutorial. Moreover, DefaultTableModel fires the appropriate update events for which the JTable listens. No additional code should be required. As an aside, use layouts rather than setBounds().

OTHER TIPS

You need the function fireTableDataChanged. You should call it upon your table model after changing the values of table cells.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top