JTable 的 getSelectedColumn 不适用于更改模型
我有一个 JTree,当您单击叶子时,它会更改 JTable 模型并显示一个新表(很像电子邮件系统)。当我尝试选择新表模型的列时,问题就出现了,无论如何它总是给出-1。请注意,这对于第一个 JTable 模型非常有效,但在更改表后,
System.out.println(table.getSelectedColumn());
始终返回 -1。
编辑:好吧,说实话,我不知道要在这里发布哪个代码片段。
//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());
}
i have a JTree, and when u click leafs, it changes the JTable model and displays a new table (much like an email system). the problem arises when i try to select the columns of the new Table model, it always gives -1, no matter what. NOTE that this works perfectly for the 1st JTable model, but after the changed table,
System.out.println(table.getSelectedColumn());
always returns -1.
EDIT: well, i dont know which code snippet to post here to be honest.
//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());
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当没有选择列时,getSelectedColumn 返回 -1。
如果您确定正在选择一列,那么也许在创建新表时您正在对旧表调用 getSelectedColumn ?
发布片段后编辑:
您正在创建一个新的 JTable,它不仅没有设置(例如 FillsViewPortHeight,而且也没有选择模型)。尝试
将 valueChanged 替换为
getSelectedColumn returns -1 when there is no column selected.
If you are sure that a column is being selected then perhaps when you are creating the new table you are calling getSelectedColumn on the old table?
Edit after snippet posted:
You're creating a new JTable which not only doesn't have the settings (such as FillsViewPortHeight but also doesn't have the selection model. Try replacing
in valueChanged with
当您更改支持模型时,它会重置选择信息。请记住,新模型甚至可能具有不同的列数等。
您可以做的是在更改模型之前保存选择信息,然后在设置新模型后重新选择正确的列。
When you change the backing model, it resets the selection information. Remember, a new model might even have a different number of columns, etc.
What you could do is save the selection information before you change the model, and then re-select the correct column once the new model is set.