替换并刷新 ScrollPane 中的旧 JXTreeTable
Replace and refresh old JXTreeTable in ScrollPane
我在滚动窗格中有一个 JxTreeTable (gbc_constraints),通过单击按钮,应该打开一个文件选择器 window,然后用户选择一个 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
类型的根树节点作为参数。
我在滚动窗格中有一个 JxTreeTable (gbc_constraints),通过单击按钮,应该打开一个文件选择器 window,然后用户选择一个 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
类型的根树节点作为参数。