Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
133 views
in Technique[技术] by (71.8m points)

java - Access tree object in netbeans outline

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...