views:

172

answers:

1

I'm using Outline from netbeans to display some structured data.

How can I map selected row to an object in tree?

+2  A: 

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.

trashgod
Outline provides only getSelectedRow() method. But index of the row depends on expanded/collapsed state of nodes above.I see no way to map index of selected row to an object in TreeModel.
p4553d
See above. I'm not sure what you're trying to do, but it seems like you can traverse your `RowModel` or `TreeModel` as required.
trashgod
It is some kind of ugly solution, to track view to get information for model, that I tried to avoid. But it seems to be the only way at the moment. Thanks for the help!
p4553d
Interesting. I think this an artifact of Swing's architecture; see above.
trashgod