jtable增删改查和jtree连接

时间:2014-08-31 02:38:57
【文件属性】:

文件名称:jtable增删改查和jtree连接

文件大小:10KB

文件格式:JAVA

更新时间:2014-08-31 02:38:57

jtable和jtree

package BookTest; import java.awt.*; import java.awt.event.*; import java.util.AbstractQueue; import java.util.Vector; import javax.swing.*; import javax.swing.table.*; import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.DefaultTreeCellRenderer; import javax.swing.tree.DefaultTreeModel; import javax.swing.tree.TreeSelectionModel; import javax.swing.event.*; import javax.swing.table.TableColumnModel; //import DbHelp.OperatorJobs; import Dao.ArrayBook; public class Test extends JFrame { public JPanel jp=null; public String aa; //JTable jTable1 = new JTable(); public JTable jTable1=null; private JTree jTree1; DefaultTableModel mode; private JScrollPane scrollPane = null; DefaultMutableTreeNode node; JButton[] btn={new JButton("增加"),new JButton("删除"),new JButton("修改"),new JButton("查询"),new JButton("重置")}; public JTextField[] txt ={new JTextField(),new JTextField(),new JTextField(),new JTextField(),new JTextField(),new JTextField()}; public String[] str1={"1","summer","boy","20","",""}; public String[] str2={"2","wulei","girl","19","",""}; public String[] str3={"3","west","boy","20","",""}; public String[] str4={"4","fish","girl","18","",""}; public String[] str5={"5","youku","boy","21","",""}; public String[] str6={"6","aa","boy","21","",""}; public String[][] tempData1={str1,str2,str3,str4,str5,str6}; public String[] aa1={"1","summer","boy","20","123","123"}; public String[] aa2={"2","wulei","girl","19","123",""}; public String[] aa3={"3","west","boy","20","123",""}; public String[] aa4={"4","fish","girl","18","1",""}; public String[] aa5={"5","youku","boy","21","12",""}; public String[] aa6={"6","aa","boy","21","123",""}; public String[][] tempData4={aa1,aa2,aa3,aa4,aa5,aa6}; //设置表头数据 public String[] tempTitle={"ID","标题","内容","作者","时间","是否选中"}; //设置表格的数据集合 Vector v1=new Vector(); Vector v2=new Vector(); Vector v3=new Vector(); Vector v4=new Vector(); Vector v5=new Vector(); Vector v6=new Vector(); Vector rows=new Vector(); //设置表头数据集合 Vector[] tempData2={v1,v2,v3,v4,v5,v6}; Vector data=new Vector(); Vector title=new Vector(); Vector data1=new Vector(); Vector v=new Vector(); DefaultTableModel model; public Test() { this.setSize(800,600); JPanel jp=(JPanel)this.getContentPane(); jp.setLayout(null); jp.setBorder(BorderFactory.createTitledBorder("JTable和JTree")); DefaultMutableTreeNode root = new DefaultMutableTreeNode("图书"); DefaultMutableTreeNode node1=new DefaultMutableTreeNode("java"); DefaultMutableTreeNode node2=new DefaultMutableTreeNode("jsp"); DefaultTreeModel treeModel=new DefaultTreeModel(root); treeModel.insertNodeInto(node1, root, root.getChildCount()); treeModel.insertNodeInto(node2, root, root.getChildCount()); DefaultMutableTreeNode leafnode=new DefaultMutableTreeNode("aa"); treeModel.insertNodeInto(leafnode, node1, node1.getChildCount()); leafnode=new DefaultMutableTreeNode("bb"); treeModel.insertNodeInto(leafnode, node1, node1.getChildCount()); leafnode=new DefaultMutableTreeNode("cc"); treeModel.insertNodeInto(leafnode, node1, node1.getChildCount()); leafnode=new DefaultMutableTreeNode("dd"); treeModel.insertNodeInto(leafnode, node1, node1.getChildCount()); leafnode=new DefaultMutableTreeNode("dd"); treeModel.insertNodeInto(leafnode, node2, node2.getChildCount()); leafnode=new DefaultMutableTreeNode("ee"); treeModel.insertNodeInto(leafnode, node2, node2.getChildCount()); JTree jTree1=new JTree(treeModel); jTree1.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION); jTree1.setRowHeight(20); jTree1.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() { public void valueChanged(javax.swing.event.TreeSelectionEvent evt) { jTree1ValueChanged(evt); } }); DefaultTreeCellRenderer cellRenderer=(DefaultTreeCellRenderer)jTree1.getCellRenderer(); cellRenderer.setTextNonSelectionColor(Color.black); cellRenderer.setTextSelectionColor(Color.blue); jTree1.setBounds(10,20,150,600); jp.add(jTree1); for(int i=0;i0;i--){ mode.removeRow(0); } model= new DefaultTableModel(new Object[]{"书籍编号","图书类别","书名","作者","价钱","ISBN"}, 0); JTree jTree1=(JTree)e.getSource(); jTable1.setModel(model); jTable1=new JTable(model); DefaultMutableTreeNode node=(DefaultMutableTreeNode)jTree1.getLastSelectedPathComponent(); String nodeName=node.toString(); Vector ve = this.GetBookByCname(nodeName.toString()); for (int i = 0; i < ve.size(); i++) { String[] row = (String[]) ve.get(i); model.addRow(row); } // }else if(nodeName.toString().equals("bb")){ // System.out.print("bb"); // }else{ // System.out.print("cc"); // } } public Vector GetBookByCname(String cname){ Vector v=this.All(); Vector vector=new Vector(); for(int i=0;i0){ JOptionPane.showMessageDialog(this.tf,"请选择您要准备删除的数据行!"); this.tf.data.remove(index);//在TableFrame中Vector data 加入删除该行数据 }else if(index==0){ this.tf.data.remove(index);//在TableFrame中Vector data 加入删除该行数据 this.tf.btn[1].setEnabled(false);//表格中的数据删除,"删除"按钮失效 }else{ JOptionPane.showMessageDialog(this.tf,"请选择您要准备删除的数据行!"); } refreshTable(); } public void update(){ Vector v=this.setVector();//从文本框中取得需要修改的内容 int index=this.tf.jTable1.getSelectedRow();//返回选中行的索引 if(index<0){ JOptionPane.showMessageDialog(this.tf,"请选择您要准备删除的数据行!"); }else{ this.tf.data.set(index,v); refreshTable();//刷新表格模型 } } public void select(){ for(int i=0;i


网友评论

  • 例子太简单了,不过还是有点帮助的
  • 很简单的例子,过于简单,不值这么多分
  • 还是不够完全 啊。
  • 很好,很有用,其中的案例可以运行而且和描述的一样,实现了table的增删改查,谢谢楼主