Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何实现侦听器?_Java_Listener_Openide - Fatal编程技术网

Java 如何实现侦听器?

Java 如何实现侦听器?,java,listener,openide,Java,Listener,Openide,我有一些动态树。现在我需要实现一些功能,每次只需单击节点就可以实现这些功能。(我的意思是只需点击一次节点,它就会“变蓝”) **EDIT2:*我使用beanTreeView和包openide 如何实现此操作的侦听器 编辑-添加伪代码 public class MyNode extends AbstractNode{ //openide package private String name; public MyNode(String nameOfNode){ supe

我有一些动态树。现在我需要实现一些功能,每次只需单击节点就可以实现这些功能。(我的意思是只需点击一次节点,它就会“变蓝”)

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

我假设它是一个Swing树。您可以通过使用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 );
            ....
    }

假设您使用的是Swing
JTree
类,您应该定义一个
TreeSelectionListener
,并将其添加到底层的
TreeModel
。如果您希望使用
ActionListener
,则需要编写一些适配器代码来将
TreeSelectionEvent
s转换为
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!");
  }
});
/**
*适配器类负责将TreeSelectionEvents转换为
*行动事件。
*/
公共类TreeSelectionAdapter实现TreeSelectionListener{
私有最终AtomicInteger nextId=新的AtomicInteger(0);
//如果
//ActionListener在通知期间将自身作为侦听器删除。
私有最终CopyOnWriteArrayList侦听器;
公用树选择适配器(){

this.listeners=new copyonwritearraylist也许你已经有了一些我们将从中开始的代码?目前你只能获得sun教程的链接(实际上应该足够了)除了上面Roman所说的,一旦您了解了侦听器,我建议您检查匿名内部类。它们可以帮助侦听器在实现IMO时减少一些麻烦。我需要类间actionListener。请参阅我提出问题的代码