【发布时间】:2010-12-15 10:08:06
【问题描述】:
我有一个 JTree,当你单击叶子时,它会更改 JTable 模型并显示一个新表(很像电子邮件系统)。当我尝试选择新表模型的列时出现问题,无论如何它总是给出-1。请注意,这对于第一个 JTable 模型非常有效,但是在更改表之后,
System.out.println(table.getSelectedColumn());
总是返回 -1。
编辑:好吧,老实说,我不知道要在此处发布哪个代码 sn-p。
//constructor
public TreeSection() {
super();
//TREE
top = new DefaultMutableTreeNode("EMAIL");
createNodes(top);
//Create a tree that allows one selection at a time.
tree = new JTree(top);
tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
//Listen for when the selection changes.
tree.addTreeSelectionListener(this);
//TABLE
tableModel = new MyTableModel();
table = new JTable(tableModel);
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
table.setFillsViewportHeight(true);
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
selectionModel = table.getSelectionModel();
selectionModel.addListSelectionListener(this);
//SCROLL PANE
treeView = new JScrollPane(tree);
tableView = new JScrollPane(table);
//SPLIT PANE
splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
splitPane.setTopComponent(treeView);
splitPane.setBottomComponent(tableView);
splitPane.setDividerLocation(400);
splitPane.setPreferredSize(new Dimension(200, 700));
//Add the split pane to this panel.
panel.add(splitPane,BorderLayout.WEST);
}
/** Required by TreeSelectionListener interface. */
public void valueChanged(TreeSelectionEvent e) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
if (node == null) return;
Object nodeInfo = node.getUserObject();
if (node.isLeaf()) {
MailObject mObj = (MailObject)nodeInfo;
table.removeAll();
tableModel.setTableData(mObj.tableD);//changes tree model
table = new JTable(tableModel);
panel.repaint();
}
}
public void valueChanged(ListSelectionEvent event) {
// Get the data model for this table
//table.changeSelection(table.getSelectedColumn(), table.getSelectedRow(), false, false);
TableModel model = (TableModel)table.getModel();
System.out.println(table.getSelectedColumn());
}
【问题讨论】: