Search code examples
javaxmlswingtreejxtreetable

Replace and refresh old JXTreeTable in ScrollPane


I have a JxTreeTable in a scrollpane (gbc_constraints), and by clicking on the button, a file chooser window should open, afterwards the user selects a xml file, which should get parsed as a new JxTreeTable. I want to replace the old one by the new one. But I'm struggling to make the changes somehow visible. The program doesn't adapt visibly the tree, or refresh or repaint it. I hope you can help me.

public class pnlXMLTree extends JPanel {

     public TreeTable treeTable;
     public JScrollPane scrollPane;
     public JXTreeTable jxttable;
     public pnlXMLTree() {
            List<Object[]> content = new ArrayList<>();
            final JFileChooser fc = new JFileChooser();
            treeTable = new TreeTable(content);
            ....
            jxttable = treeTable.getTreeTable();
            scrollPane = new JScrollPane(jxttable);
            GridBagConstraints gbc_scrollPane = new GridBagConstraints();
            gbc_scrollPane.anchor = GridBagConstraints.NORTH;
            gbc_scrollPane.fill = GridBagConstraints.HORIZONTAL;
            gbc_scrollPane.gridwidth = 5;
            gbc_scrollPane.gridheight = 13;
            gbc_scrollPane.gridx = 2;
            gbc_scrollPane.gridy = 1;
            add(scrollPane, gbc_scrollPane);
            ...
            ...

            btnOpenXML.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent arg0) {

                    FileNameExtensionFilter xmlfilter = new FileNameExtensionFilter(
                             "xml files (*.xml)", "xml");
                    fc.setFileFilter(xmlfilter);
                    fc.setDialogTitle("Open schedule file");
                    // set selected filter
                    fc.setFileFilter(xmlfilter);
                    int returnVal = fc.showOpenDialog(null);
                    String filepath;
                    if (returnVal == JFileChooser.APPROVE_OPTION) {
                        File file = fc.getSelectedFile();
                        //This is where a real application would open the file.
                        System.out.println("Opening: " + file.getName() + ".");
                        filepath = file.getAbsolutePath();
                        jxttable = treeTable.getTreeTable(filepath);
                        //scrollPane = new JScrollPane(treeTable.getTreeTable(filepath));
                        //jxttable.getModel().getRow(0).;

                    } else {
                        System.out.println("Open command cancelled by user.");
                    }


                }
            });

        setVisible(true);

     }

 }

Solution

  • I always have this same problem with JXTreeTable and the solution seems to be to reset the model like this:

    myTree.setTreeTableModel(new MyTreeTableModel(rootNode));
    

    In this examply my model constructor takes the root tree node of TreeNode type as parameter.