文件名称: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;i