替换并刷新ScrollPane中的旧JXTreeTable

时间:2015-08-06 12:25:35

标签: java xml swing tree jxtreetable

我在滚动窗格(gbc_constraints)中有一个JxTreeTable,通过单击该按钮,应该打开一个文件选择器窗口,然后用户选择一个xml文件,该文件应该被解析为一个新的JxTreeTable。我想用新的替换旧的。但我正在努力使变化以某种方式显现出来。该程序不会明显地适应树,或刷新或重绘它。我希望你能帮助我。

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);

     }

 }

1 个答案:

答案 0 :(得分:1)

JXTreeTable我总是遇到同样的问题,解决方法似乎是重置模型:

myTree.setTreeTableModel(new MyTreeTableModel(rootNode));

在这个示例中,我的模型构造函数将TreeNode类型的根树节点作为参数。