0

我有一些动态树。现在我需要实现一些功能,每次都发生这种情况,只需单击节点即可。(我的意思是只需单击节点,“使其变为蓝色”)

** EDIT2: ** 我使用 beanTreeView 并打包 openide

如何实现这个动作的监听器?

编辑 - 添加伪代码

public class MyNode extends AbstractNode{ //openide package
   private String name;

   public MyNode(String nameOfNode){
       super (new Children.LEAF);
       name = nameOfNode;
   }
   ....
   ....
}

public class IWantNameOfSelectedNode extends JPanel{   
    private JLabel jLnameOfNode;

   public IWantNameOfSelectedNode(){
       jLnameOfNode.setText("wiating for node selection");
   }

现在,我需要将所选节点的名称放入 jLabel,并在每次选择节点时更改它。

4

2 回答 2

1

假设您使用的是 SwingJTree类,您应该定义 aTreeSelectionListener并将其添加到底层TreeModel. 如果您想改用ActionListener,则需要编写一些适配器代码来将TreeSelectionEvents 转换为ActionEvents(尽管这实际上毫无意义)。

例子

/**
 * Adapter class responsible for translating TreeSelectionEvents into
 * ActionEvents.
 */
public class TreeSelectionAdapter implements TreeSelectionListener {
  private final AtomicInteger nextId = new AtomicInteger(0);
  // Prefer CopyOnWriteArrayList to avoid ConcurrentModificationException if an
  // ActionListener removes itself as a listener during notification.
  private final CopyOnWriteArrayList<ActionListener> listeners;

  public TreeSelectionAdapter() {
    this.listeners = new CopyOnWriteArrayList<ActionListener();
  }

  public void addActionListener(ActionListener l) {
    this.listeners.add(l);
  }

  public void removeActionListener(ActionListener l) {
    this.listeners.remove(l);
  }

  public void valueChanged(TreeSelectionEvent evt) {
    // Create new ActionEvent which corresponds to incoming TreeSelectionEvent
    // and notify registered ActionListeners.
    ActionEvent aEvt = new ActionEvent(evt.getSource(),
      nextId.getAndIncrement(), "selectionChanged");

    for (ActionListener listener : listeners) {
      listener.actionPerformed(listener);
    }
  }
}

TreeNode rootNode = createTreeModel(); // Create custom model
JTree tree = new JTree(rootNode); // Install model into JTree.

// Add adapter listener to underlying selection model.
tree.getSelectionModel().addTreeSelectionListener(adapter);

// Register ActionListener with adapter listener.
adapter.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent evt) {
    System.err.println("Selection has changed in some way!");
  }
});
于 2010-03-29T13:43:50.257 回答
0

我假设它是一棵秋千树。您可以通过使用 CustomRenderer 组件或使用 TreeSelectionListener 接口来实现此目的。

这个链接有一个关于如何改变图标、背景等的高级示例的教程。你需要的是一个比这更简单的版本。

您将感兴趣的代码是

public Component getTreeCellRendererComponent( JTree tree,
                    Object value, boolean bSelected, boolean bExpanded,
                            boolean bLeaf, int iRow, boolean bHasFocus )
    {
        // Find out which node we are rendering and get its text
        DefaultMutableTreeNode node = (DefaultMutableTreeNode)value;
        String  labelText = (String)node.getUserObject();

        this.bSelected = bSelected;

        // Set the correct foreground color
        if( !bSelected )
            setForeground( Color.black );
        else
            setForeground( Color.white );
            ....
    }
于 2010-03-29T13:25:00.210 回答