package Tree;
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();
// }
// }
// });
new AppDemo();
}
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);
this.setVisible(true);
tree.addTreeSelectionListener(new TreeSelectionListener() {
public void valueChanged(TreeSelectionEvent e) {
DefaultMutableTreeNode note = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
String name = note.toString();//获得这个结点的名称
textField.setText(name);
}
});
}
}
参考链接
https://zhidao.baidu.com/question/746015792869057252.html