如何实现监听器?

时间:2010-03-29 13:09:10

标签: java listener openide

我有一些动态树。而现在我需要实现一些功能,每次只需点击节点就会发生这种功能。 (我的意思是只有一次点击节点,“使它成为蓝色”)

** 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,并在每次选择节点时更改它。

2 个答案:

答案 0 :(得分:1)

假设您正在使用Swing JTree类,则应定义TreeSelectionListener并将其添加到基础TreeModel。如果您希望使用ActionListener,则需要编写一些适配器代码,以便将TreeSelectionEvent转换为ActionEvent s(尽管这实际上是毫无意义的)。

示例

/**
 * 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!");
  }
});

答案 1 :(得分:0)

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

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

您感兴趣的代码是

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