我在滚动窗格(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);
}
}
最佳答案
我总是在JXTreeTable
上遇到同样的问题,解决方案似乎是像这样重置模型:
myTree.setTreeTableModel(new MyTreeTableModel(rootNode));
在这个示例中,我的模型构造函数将
TreeNode
类型的根树节点作为参数。