I\'m using Outline
from netbeans to display some structured data.
How can I map selected row to an object in tree?
You might look at the example in Announcing the new Swing Tree Table today. It looks like the author is Creating a Data Model, so Responding to Node Selection should be helpful. I find the class org.netbeans.swing.outline.Outline
in NetBeans 6.8:
NetBeans/platform11/modules/org-netbeans-swing-outline.jar
Addenda:
Note that Outline
descends from JTable
, so How to Use Tables: User Selections may be helpful. Based on the example cited above, here's a listener that shows the apparent change in row number as nodes expand and collapse and the selection remains constant:
outline.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
int row = outline.getSelectedRow();
File f = (File) outline.getValueAt(row, 0);
if (!e.getValueIsAdjusting()) {
System.out.println(row + ": " + f);
}
}
});
Although provisional, you might look at OutlineModel
and DefaultOutlineModel
. The former implements both TreeModel
and TableModel
and offers TreePathSupport
; the latter mentions the "impedance mismatch between TableModelEvent and TreeModelEvent."
Like JTable
, the selected row index in the view may not match the corresponding row in the model, perhaps due to sorting, etc. The getValueAt()
method seems a convenient way to call convertRowIndexToModel()
. This is common in Swing's separable model architecture, which "collapses the view and controller parts of each component into a single UI (user-interface) object." See A Swing Architecture Overview.