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());
}
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
table = new JTable(tableModel);
in valueChanged with
table.setModel(tableModel);