2010-05-15 89 views

回答

6

你可以看看Announcing the new Swing Tree Table today中的例子。它看起来像作者Creating a Data Model,所以Responding to Node Selection应该是有帮助的。我觉得在NetBeans 6.8类org.netbeans.swing.outline.Outline

NetBeans/platform11/modules/org-netbeans-swing-outline.jar

附录:

注意OutlineJTable下降,所以How to Use Tables: User Selections可能会有所帮助。基于上面提到的例子,这里有一个听众,显示行数的显着变化为节点展开和折叠和选择保持不变:

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); 
     } 
    } 
}); 

虽然provisional,你可能看OutlineModelDefaultOutlineModel。前者实施TreeModelTableModel并提供TreePathSupport;后者提到了“TableModelEvent和TreeModelEvent之间的阻抗不匹配”。

JTable,在视图中选择的行指数可以不对应的行中的模型相匹配,这可能是由于分选等。getValueAt()方法似乎是一个方便的方法来调用convertRowIndexToModel()。这在Swing的可分离模型体系结构中很常见,它将每个组件的视图和控制器部分折叠为单个UI(用户界面)对象。“见A Swing Architecture Overview

+0

Outline只提供getSelectedRow()方法。但是行的索引取决于上面节点的展开/折叠状态。 我看不出将所选行的索引映射到TreeModel中的对象。 – p4553d 2010-05-16 12:58:36

+0

见上文。我不确定你在做什么,但是你可以根据需要遍历你的'RowModel'或'TreeModel'。 – trashgod 2010-05-16 19:44:30

+0

这是一种丑陋的解决方案,跟踪视图来获取模型的信息,我试图避免。但它似乎是目前唯一的方法。谢谢您的帮助! – p4553d 2010-05-22 12:11:24