Java Jtree MouseeEvent(如果未选择任何内容)

Java Jtree MouseeEvent(如果未选择任何内容),java,swing,mouseevent,jtree,Java,Swing,Mouseevent,Jtree,我试图弄清楚如何编写一个事件,如果在Swing JTree中单击,而鼠标没有单击树中的任何内容,则会得到一个显示“nothing”的println 假设我有一个带有AWT.event.mouwevent的树“info_tree”: private void info_treeMouseClicked(java.awt.event.MouseEvent evt) { if(info_tree.getSelectionPath() /*something along the lines

我试图弄清楚如何编写一个事件,如果在Swing JTree中单击,而鼠标没有单击树中的任何内容,则会得到一个显示“nothing”的println

假设我有一个带有AWT.event.mouwevent的树“info_tree”:

private void info_treeMouseClicked(java.awt.event.MouseEvent evt) {   
    if(info_tree.getSelectionPath() /*something along the lines of is empty or if !info_tree.getSelectionPath().isEmpty()*/
       ){system.out.println("Nothing");
      }else{
          system.out.println("Something");
      }
}
我找不到任何东西来比较所选路径或树的元素。

您可能希望尝试

If (info_tree.getSelectionPath()==null)

但是,您可能需要切换输出线。因为这是一个正确/错误的条件。如果为false,则转到其他。这也可以解决问题。

您可以尝试以下程序:

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class JTreeGetSelectedNode extends JFrame {
    public JTreeGetSelectedNode() throws HeadlessException {
        initializeUI();
    }

    private void initializeUI() {
        setSize(200, 400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        DefaultMutableTreeNode root = new DefaultMutableTreeNode("Countries");
        DefaultMutableTreeNode asia = new DefaultMutableTreeNode("Asia");
        String[] countries = new String[] {"India", "Singapore", "Indonesia", "Vietnam"};
        for (String country : countries) {
            DefaultMutableTreeNode node = new DefaultMutableTreeNode(country);
            asia.add(node);
        }

        DefaultMutableTreeNode northAmerica = new DefaultMutableTreeNode("North America");
        countries = new String[] {"United States", "Canada"};
        for (String country : countries) {
            DefaultMutableTreeNode node = new DefaultMutableTreeNode(country);
            northAmerica.add(node);
        }

        DefaultMutableTreeNode southAmerica = new DefaultMutableTreeNode("South America");
        countries = new String[] {"Brazil", "Argetina", "Uruguay"};
        for (String country : countries) {
            DefaultMutableTreeNode node = new DefaultMutableTreeNode(country);
            southAmerica.add(node);
        }

        DefaultMutableTreeNode europe = new DefaultMutableTreeNode("Europe");
        countries = new String[] {"United Kingdom", "Germany", "Spain", "France", "Italy"};
        for (String country : countries) {
            DefaultMutableTreeNode node = new DefaultMutableTreeNode(country);
            europe.add(node);
        }

        root.add(asia);
        root.add(northAmerica);
        root.add(southAmerica);
        root.add(europe);

        final JTree tree = new JTree(root);
        JScrollPane pane = new JScrollPane(tree);
        pane.setPreferredSize(new Dimension(200, 400));


        JButton button = new JButton("Get Selected");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Object paths = tree.getLastSelectedPathComponent();

      if(paths == null){
      System.out.println("nothig");
      }else{
      System.out.println("Something");
      }
            }
        });

        getContentPane().setLayout(new BorderLayout());
        getContentPane().add(pane, BorderLayout.CENTER);
        getContentPane().add(button, BorderLayout.SOUTH);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new JTreeGetSelectedNode().setVisible(true);
            }
        });
    }
}

“我试图找出如何编写一个事件代码,如果在Swing JTree中单击,而鼠标没有单击树中的任何内容,则会得到一个显示“nothing”的println。”。。为什么?这确实解决了问题。问题是,如果我在JTree面板中单击,而我没有选择任何内容,我会得到一个NullPointerException(当然是有意义的)。解决这个问题的方法是在树中添加一个默认选择器<代码>信息树。设置选择行(0)NullPointerException,因为没有
LastSelectedPathComponent