首页 > Java开发 > 实现JTree与JTable的联动,以及对JTable数据的增加与删除

实现JTree与JTable的联动,以及对JTable数据的增加与删除

在作业中有遇到有关JTree与JTable的联动的问题,后来在网上查了好多,都写得不全的样子,所以就写了一个小程序以作巩固和供需要的人参考

一 实现功能

 

1  将数据显示在相应Table中

2  实现了JTree与JTable的联动

3  对相应的Table表进行一行的增加与删除(删除时需要点击一行)

二  代码实现

1 JTree与JTable面板代码实现

 

[java][/java] view plaincopy

  1. package Library;
  2. import java.awt.BorderLayout;
  3. import java.awt.Dimension;
  4. import java.awt.FlowLayout;
  5. import java.awt.event.*;
  6. import java.io.BufferedReader;
  7. import java.io.File;
  8. import java.io.FileReader;
  9. import java.io.IOException;
  10. import javax.swing.BorderFactory;
  11. import javax.swing.JButton;
  12. import javax.swing.JFrame;
  13. import javax.swing.JPanel;
  14. import javax.swing.JScrollPane;
  15. import javax.swing.JSplitPane;
  16. import javax.swing.JTable;
  17. import javax.swing.JTree;
  18. import javax.swing.event.*;
  19. import javax.swing.table.DefaultTableModel;
  20. import javax.swing.tree.DefaultMutableTreeNode;
  21. import javax.swing.tree.TreePath;
  22. import javax.swing.tree.TreeSelectionModel;
  23. /**
  24.  * @author HuangRong
  25.  * Date 2013/11/18
  26.  *
  27.  */
  28. public class TreeTableText extends JFrame{
  29.     /**
  30.      * @param args
  31.      */
  32.     private JPanel panel;
  33.     public JTable table;
  34.     private JButton but_Add,but_Delect;
  35.     private JScrollPane treePane;
  36.     private JScrollPane tablePane;
  37.     private Object[] node={"借阅图书的学生信息","借阅图书的学生信息"};
  38.     private DefaultMutableTreeNode node1,node2,top;
  39.     private DefaultTableModel tableModel1,tableModel2;
  40.     private JSplitPane split;
  41.     private String[] colum1 = {"姓名","学号","电话","借书时间"};
  42.     private String[][] data1 = {{ "李月", "1108010..", "183...","2013、2、16" },
  43.             {"莫寒冰", "11080102...", "183..","2013、09、21" }, { "李露", "1108020...", "183...","2013、03、24" } };
  44.     private String[] colum2 = {"书目类别","书目名称","单价","作者"};
  45.     private String[][] data2 = {{ "计算机类", "数据结构", "78","王伟" },
  46.             {"光电类", "数字电子技术", "56","张文" }, { "计算机类", "操作系统", "90","杨帆" } };
  47.     private JTree tree;
  48.     private String name = node[0].toString();
  49.     TreeTableText(){
  50.         this.init();
  51.         setSize(new Dimension(700,500));
  52.         setVisible(true);
  53.         addEvent(this);
  54.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  55.     }
  56.     public void init(){
  57.         node1 = new DefaultMutableTreeNode("借阅图书的学生信息");
  58.         node1.add(new DefaultMutableTreeNode(new User("计算机学院")));
  59.         node1.add(new DefaultMutableTreeNode(new User("电控学院")));
  60.         node1.add(new DefaultMutableTreeNode(new User("人文与外国语学院")));
  61.         node1.add(new DefaultMutableTreeNode(new User("机械学院")));
  62.         node1.add(new DefaultMutableTreeNode(new User("通信学院")));
  63.         node1.add(new DefaultMutableTreeNode(new User("能源学院")));
  64.         node2 = new DefaultMutableTreeNode("图书馆的图书库存信息");
  65.         node2.add(new DefaultMutableTreeNode(new User("文学类")));
  66.         node2.add(new DefaultMutableTreeNode(new User("商学类")));
  67.         node2.add(new DefaultMutableTreeNode(new User("计算机类")));
  68.         node2.add(new DefaultMutableTreeNode(new User("建筑类")));
  69.         node2.add(new DefaultMutableTreeNode(new User("光电类")));
  70.         node2.add(new DefaultMutableTreeNode(new User("会计类")));
  71.         top = new DefaultMutableTreeNode("图书管理系统");
  72.         top.add(node1);
  73.         top.add(node2);
  74.         tree = new JTree(top);
  75.         add(tree);
  76.         but_Add=new JButton("增加");
  77.         but_Delect=new JButton("删除");
  78.         tableModel1 = new DefaultTableModel(data1,colum1);
  79.         tableModel2 = new DefaultTableModel(data2,colum2);
  80.         table = new JTable(tableModel2);
  81.         tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
  82.         panel=new JPanel();
  83.         panel.setLayout(new FlowLayout(FlowLayout.CENTER,120,70));
  84.         panel.add(but_Add);
  85.         panel.add(but_Delect);
  86.         treePane = new JScrollPane();
  87.         treePane.getViewport().add(tree);
  88.         tablePane = new JScrollPane();
  89.         tablePane.getViewport().add(table);
  90.         split=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,true,treePane,tablePane);
  91.         split.setDividerLocation(0.5);
  92.         this.getContentPane().add(split,BorderLayout.CENTER);
  93.         this.getContentPane().add(panel,BorderLayout.SOUTH);
  94.     }
  95.     private void addEvent(final TreeTableText value){
  96.         //匿名类
  97.         tree.addTreeSelectionListener(new TreeSelectionListener(){
  98.             public void valueChanged(TreeSelectionEvent e){
  99.                 JTree tree = (JTree)e.getSource();
  100.                 DefaultMutableTreeNode node = (DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
  101.                 String name = node.toString();
  102.                 value.name = name;
  103.                 if(name.equalsIgnoreCase("图书馆的图书库存信息")){
  104.                     value.table.setModel(value.tableModel2);
  105.                     value.table.validate();
  106.                 }
  107.                 else {
  108.                     value.table.setModel(value.tableModel1);
  109.                     value.table.validate();
  110.                 }
  111.             }
  112.         });
  113.         but_Add.addActionListener(new ActionListener(){
  114.             public void actionPerformed(ActionEvent e){
  115.                 DefaultTableModel model = (DefaultTableModel)value.table.getModel();
  116.                 System.out.println(value.name==null);
  117.                 if(value.name.equalsIgnoreCase("图书馆的图书库存信息")){
  118.                     AddUser factory = new AddUser("增加图书馆图书");
  119.                     factory.setSuper(value);
  120.                 }
  121.                 else if(value.name.equalsIgnoreCase("借阅图书的学生信息")){
  122.                     AddStudent car = new AddStudent("增加借阅的学生信息");
  123.                     //car.setSuper(value);
  124.                 }
  125.                 else{
  126.                     model.addRow(new Object[]{"12","33"});
  127.                 }
  128.             }
  129.         });
  130.         but_Delect.addActionListener(new ActionListener(){
  131.             public void actionPerformed(ActionEvent e){
  132.                 DefaultTableModel model = (DefaultTableModel)value.table.getModel();
  133.                 int si = table.getSelectedRow();
  134.                 model.removeRow(si);
  135.             }
  136.         });
  137.     }
  138.     public static void main(String[] args) {
  139.         // TODO Auto-generated method stub
  140.         TreeTableText text=new TreeTableText();
  141.     }
  142. }

二 添加类别

 

 

[java][/java] view plaincopy

  1. package Library;
  2. /**
  3.  * @author HuangRong
  4.  * Date 2013/11/13
  5.  *
  6.  */
  7. public class User {
  8.         private String name;
  9.         public User(String n) {
  10.             name = n;
  11.         }
  12.         // 重点在toString,节点的显示文本就是toString
  13.         public String toString() {
  14.             return name;
  15.         }
  16. }

三 增加学生或书籍(两个代码一样,这就贴一个了)

 

 

[java][/java] view plaincopy

  1. package Library;
  2. import java.awt.BorderLayout;
  3. import java.awt.Dimension;
  4. import java.awt.FlowLayout;
  5. import java.awt.Font;
  6. import java.awt.GridBagConstraints;
  7. import java.awt.GridBagLayout;
  8. import java.awt.event.ActionEvent;
  9. import java.awt.event.ActionListener;
  10. import javax.swing.BorderFactory;
  11. import javax.swing.JButton;
  12. import javax.swing.JComboBox;
  13. import javax.swing.JFrame;
  14. import javax.swing.JLabel;
  15. import javax.swing.JPanel;
  16. import javax.swing.JTextField;
  17. import javax.swing.table.DefaultTableModel;
  18. /**
  19.  * @author HuangRong
  20.  * Date 2013/11/18
  21.  *
  22.  */
  23. public class AddUser extends JFrame{
  24.     private JLabel b_type,b_name,b_price,b_author;
  25.     JTextField field1,field2,field3,field4;
  26.     private JComboBox box;
  27.     private static JButton sureButton;
  28.     private static JButton cancelButton;
  29.     public static TreeTableText superModel = new TreeTableText();
  30.     String b_type1[]={"文学类","商学类","计算机类","建筑类","光电类","会计类"};
  31.     AddUser(String name){
  32.         this.setBounds(300,300,500,400);
  33.         this.setTitle(name);
  34.         this.setLayout(new GridBagLayout());
  35.         GridBagConstraints gc = new GridBagConstraints();
  36.         gc.weightx = 10;
  37.         gc.weighty = 10;
  38.         gc.fill = GridBagConstraints.BOTH;
  39.         gc.gridwidth = GridBagConstraints.REMAINDER;
  40.         JPanel centerPanel = new JPanel();
  41.         centerPanel.setBorder(BorderFactory.createTitledBorder("增加内容"));
  42.         b_type=new JLabel("图书类型:");
  43.         b_name=new JLabel("图书名称:");
  44.         b_price=new JLabel("图书价格:");
  45.         b_author=new JLabel("图书作者:");
  46.         field3 = new JTextField();
  47.         field2 = new JTextField();
  48.         field1 = new JTextField();
  49.         field4 = new JTextField();
  50.         box = new JComboBox(b_type1);
  51.         GridBagLayout centerLayout = new GridBagLayout();
  52.         centerPanel.setLayout(centerLayout);
  53.         GridBagConstraints centerGc = new GridBagConstraints();
  54.         centerGc.fill = GridBagConstraints.BOTH;
  55.         //weightx分布额外的水平空间
  56.         centerGc.weightx = 1;
  57.         //weighty分布额外的垂直空间
  58.         centerGc.weighty = 1;
  59.         centerLayout.setConstraints(b_name,centerGc);
  60.         centerGc.weightx = 3;
  61.         //REMAINDER指定此组件是其行或列中的最后一个组件
  62.         centerGc.gridwidth = GridBagConstraints.REMAINDER;
  63.         centerLayout.setConstraints(field1,centerGc);
  64.         centerGc.weighty = 2;
  65.         centerGc.weightx = 1;
  66.         centerGc.gridwidth = GridBagConstraints.BOTH;
  67.         centerLayout.setConstraints(b_price,centerGc);
  68.         centerGc.weightx = 3;
  69.         centerGc.gridwidth = GridBagConstraints.REMAINDER;
  70.         centerLayout.setConstraints(field2,centerGc);
  71.         centerGc.weightx = 3;
  72.         centerGc.gridwidth = GridBagConstraints.BOTH;
  73.         centerGc.weightx = 1;
  74.         centerGc.weighty = 1;
  75.         centerLayout.setConstraints(b_type,centerGc);
  76.         centerGc.gridwidth = GridBagConstraints.REMAINDER;
  77.         centerGc.weightx = 3;
  78.         centerLayout.setConstraints(box,centerGc);
  79.         centerGc.gridwidth = GridBagConstraints.BOTH;
  80.         centerGc.weightx = 1;
  81.         centerGc.weighty = 1;
  82.         centerLayout.setConstraints(b_author,centerGc);
  83.         centerGc.weightx = 3;
  84.         centerGc.gridwidth = GridBagConstraints.REMAINDER;
  85.         centerLayout.setConstraints(field4,centerGc);
  86.         centerPanel.add(b_name);
  87.         centerPanel.add(field1);
  88.         centerPanel.add(b_price);
  89.         centerPanel.add(field2);
  90.         centerPanel.add(b_type);
  91.         centerPanel.add(box);
  92.         centerPanel.add(b_author);
  93.         centerPanel.add(field4);
  94.         this.add(centerPanel,gc);
  95.         gc.weighty = 10;
  96.         JPanel bottomPanel = new JPanel();
  97.         bottomPanel.setLayout(new FlowLayout());
  98.         sureButton = new JButton("确定");
  99.         sureButton.setFont(new Font("宋体",3,20));
  100.         cancelButton = new JButton("取消");
  101.         cancelButton.setFont(new Font("宋体",3,20));
  102.         bottomPanel.add(sureButton,BorderLayout.CENTER);
  103.         bottomPanel.add(cancelButton,BorderLayout.CENTER);
  104.         addEvent(this);
  105.         this.add(bottomPanel,gc);
  106.         pack();
  107.         this.setVisible(true);
  108.     }
  109.      public static void addEvent(final AddUser w){
  110.             sureButton.addActionListener(new ActionListener(){
  111.                 public void actionPerformed(ActionEvent e){
  112.                     DefaultTableModel model = (DefaultTableModel)w.superModel.table.getModel();
  113.                     model.addRow(new Object[]{w.field1.getText(),w.field2.getText(),w.box.getSelectedItem(),w.field4.getText()});
  114.                     w.superModel.table.validate();
  115.                     w.dispose();
  116.                 }
  117.             });
  118.             cancelButton.addActionListener(new ActionListener(){
  119.                 public void actionPerformed(ActionEvent e){
  120.                     w.dispose();
  121.                 }
  122.             });
  123.         }
  124.         public void setSuper(TreeTableText model){
  125.             superModel = model;
  126.         }
  127. }

四 运行截图

 

 

 

 

 

 

 


本文固定链接: http://www.devba.com/index.php/archives/3563.html | 开发吧

报歉!评论已关闭.