Java Swing JTree怎么获取选中节点值
1个回答
2015-06-06 · 知道合伙人数码行家
关注
展开全部
首先用Eclipse设置好界面
2
设计出来的运行界面如下图:
3
当选中树中的某个节点时,节点值设置到了文本框中, 实现效果如下:
4
主要功能代码如下:
END
全部功能代码
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTree;import javax.swing.border.EmptyBorder;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
public class AppDemo extends JFrame {
private JPanel contentPane;
private JTextField textField;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
AppDemo frame = new AppDemo();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public AppDemo() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
setTitle("测试");
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(30, 20, 196, 217);
contentPane.add(scrollPane);
final JTree tree = new JTree();
scrollPane.setViewportView(tree); textField = new JTextField();
textField.setBounds(252, 35, 172, 27);
contentPane.add(textField);
tree.addTreeSelectionListener(new TreeSelectionListener() {
public void valueChanged(TreeSelectionEvent e) {
DefaultMutableTreeNode note = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
String name = note.toString();//获得这个结点的名称
textField.setText(name);
}
});
}
}
2
设计出来的运行界面如下图:
3
当选中树中的某个节点时,节点值设置到了文本框中, 实现效果如下:
4
主要功能代码如下:
END
全部功能代码
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTree;import javax.swing.border.EmptyBorder;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
public class AppDemo extends JFrame {
private JPanel contentPane;
private JTextField textField;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
AppDemo frame = new AppDemo();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public AppDemo() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
setTitle("测试");
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(30, 20, 196, 217);
contentPane.add(scrollPane);
final JTree tree = new JTree();
scrollPane.setViewportView(tree); textField = new JTextField();
textField.setBounds(252, 35, 172, 27);
contentPane.add(textField);
tree.addTreeSelectionListener(new TreeSelectionListener() {
public void valueChanged(TreeSelectionEvent e) {
DefaultMutableTreeNode note = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
String name = note.toString();//获得这个结点的名称
textField.setText(name);
}
});
}
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询