import java.awt.EventQueue;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionListener;
import java.awt.SystemColor;
import javax.swing.JTextPane;
import javax.swing.JTabbedPane;
import java.awt.Font;
public class TreeSelectionModel extends JFrame {
private JPanel contentPane;
private JTree tree;
private JTextPane textPane,textPane2;
private JTabbedPane tabbedPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
TreeSelectionModel frame = new TreeSelectionModel();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public TreeSelectionModel() {
addWindowListener(new WindowAdapter(){
@Override
public void windowActivated(WindowEvent e){
do_this_windowActivated(e);
}
});
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBackground(SystemColor.menu);
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
tree = new JTree();
tree.addTreeSelectionListener(new TreeSelectionListener() {
public void valueChanged(TreeSelectionEvent e) {
do_tree_valueChanged(e);
}
});
tree.setBounds(10, 10, 220, 242);
contentPane.add(tree);
tabbedPane = new JTabbedPane(JTabbedPane.TOP);
tabbedPane.setFont(new Font("方正舒体", Font.PLAIN, 14));
tabbedPane.setBounds(240, 10, 184, 242);
contentPane.add(tabbedPane);
textPane = new JTextPane();
textPane.setFont(new Font("楷体", Font.PLAIN, 12));
textPane2 = new JTextPane();
textPane2.setFont(new Font("楷体", Font.PLAIN, 12));
tabbedPane.addTab("书籍", null, textPane, null);
tabbedPane.addTab("价格", null, textPane2, null);
}
protected void do_this_windowActivated(WindowEvent e){
DefaultMutableTreeNode root = new DefaultMutableTreeNode("《明日科技新书》");
DefaultMutableTreeNode parent1 = new DefaultMutableTreeNode("从入门到精通系列");
root.add(parent1);
DefaultMutableTreeNode parent2 = new DefaultMutableTreeNode("编程词典系列");
root.add(parent2);
DefaultTreeModel model = new DefaultTreeModel(root);
tree.setModel(model);
}
protected void do_tree_valueChanged(TreeSelectionEvent e){
TreePath path = tree.getSelectionPath();
if(path == null){
return;
}
DefaultMutableTreeNode node = (DefaultMutableTreeNode)path.getLastPathComponent();
String text1 ="《Java从入门到精通(第二版)》\n《PHP从入门到精通(第二版)》\n《C#从入门到精通(第二版)》\n《C++从入门到精通(第二版)》";
String text3 = "138$";
String text2 ="《JAVA编程词典》\n《PHP编程词典》\n《C#编程词典》\n《C++编程词典》";
String text4 ="200.9$";
if(node.toString().equals("从入门到精通系列")){
textPane.setText(text1);
textPane2.setText(text3);
}else{
textPane.setText(text2);
textPane2.setText(text4);
}
}
}
程序可以直接用。