[英]Java Jtree mouseEvent if nothing is selected
我試圖弄清楚如何編寫一個事件,該事件如果在Swing JTree中單擊並且鼠標沒有在樹中單擊任何東西,則得到一個表示“ nothing”的println。
假設我有一棵帶有AWT.event.Mouwesvent的樹“ 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");
}
}
我找不到任何東西來比較selectedPath或樹的元素。
您不妨嘗試一下
If (info_tree.getSelectionPath()==null)
但是,那么您可能想要切換輸出線。 因為原樣是一個真/假條件。 如果為假,它將轉到其他位置。 那也可以解決。
您可以嘗試以下程序:
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);
}
});
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.