最简单的JTable案例
package com.test; import javax.swing.*; import java.util.*; import java.sql.*; import java.awt.*; import java.awt.Event; public class Test1 extends JFrame{ //rowData用来存放行数据 //columnNames用来存放列名 Vector rowData,columnNames; JTable jt=null; JScrollPane jsp=null; public static void main(String[] args) { Test1 test=new Test1(); } public Test1(){ columnNames=new Vector(); //设置列名 columnNames.add("学号"); columnNames.add("名字"); columnNames.add("性别"); columnNames.add("年龄"); columnNames.add("籍贯"); columnNames.add("系别"); // rowData=new Vector();//可以存放多行 Vector hang=new Vector(); hang.add("sp0001"); hang.add("孙悟空"); hang.add("男"); hang.add("500"); hang.add("花果山"); hang.add("少林派"); //加入到rowData rowData.add(hang); // jt=new JTable(rowData,columnNames); //初始化 jsp=new JScrollPane(jt); //把jsp放入到JFrame this.add(jsp); this.setSize(400,300); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); } }
创建数据库;
create table stu( stuId varchar(20) primary key, stuName varchar(30), stuSex enum('男','女')default '男', stuAge int check(stuAge>1), stuJg varchar(50), stuDept varchar(30) )
插入数据:
insert into stu values('sp001','孙悟空','男',20,'花果山','少林派'); insert into stu values('sp002','猪八戒','男',21,'高老庄','天上的'); insert into stu values('sp003','沙悟净','男',22,'流沙河','水里的'); insert into stu values('sp004','唐三藏','男',23,'长安','庙里的');
数据库调取数据;
package com.test; import javax.swing.*; import java.util.*; import java.sql.*; import java.awt.*; import java.awt.Event; public class Test1 extends JFrame{ //rowData用来存放行数据 //columnNames用来存放列名 Vector rowData,columnNames; JTable jt=null; JScrollPane jsp=null; Connection ct=null; ResultSet rs=null; PreparedStatement ps=null; public static void main(String[] args) { Test1 test=new Test1(); } public Test1(){ columnNames=new Vector(); columnNames.add("学号"); columnNames.add("名字"); columnNames.add("性别"); columnNames.add("年龄"); columnNames.add("籍贯"); columnNames.add("系别"); // rowData=new Vector();//可以存放多行 try {//1.加载驱动 Class.forName("com.mysql.jdbc.Driver"); //2.、 ct = DriverManager.getConnection( "jdbc:mysql://localhost:3306/hello?useUnicode=true&characterEncoding=utf-8", "root", "123456"); ps = ct.prepareStatement("select* from stu"); rs=ps.executeQuery(); while(rs.next()){ Vector hang=new Vector(); hang.add(rs.getString(1)); hang.add(rs.getString(2)); hang.add(rs.getString(3)); hang.add(rs.getInt(4)); hang.add(rs.getString(5)); hang.add(rs.getString(6)); //加入到rowData rowData.add(hang); } jt=new JTable(rowData,columnNames); //初始化 jsp=new JScrollPane(jt); //把jsp放入到JFrame this.add(jsp); this.setSize(400,300); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); } catch (Exception e) { e.printStackTrace(); }finally{ try { if(rs!=null) rs.close(); if(ps!=null) ps.close(); if(ct!=null) ct.close(); } catch (Exception e2) { e2.printStackTrace(); } } } }
实例;
package com.test; import javax.swing.*; import java.util.*; import java.sql.*; import java.awt.*; import java.awt.Event; public class Test1 extends JFrame{ //rowData用来存放行数据 //columnNames用来存放列名 Vector rowData,columnNames; JTable jt=null; JScrollPane jsp=null; Connection ct=null; ResultSet rs=null; PreparedStatement ps=null; JPanel jp1,jp2; JLabel jl1; JButton jb1,jb2,jb3,jb4; JTextField jtf; public static void main(String[] args) { Test1 test=new Test1(); } public Test1(){ columnNames=new Vector(); columnNames.add("学号"); columnNames.add("名字"); columnNames.add("性别"); columnNames.add("年龄"); columnNames.add("籍贯"); columnNames.add("系别"); // rowData=new Vector();//可以存放多行 try {//1.加载驱动 Class.forName("com.mysql.jdbc.Driver"); //2.、 ct = DriverManager.getConnection( "jdbc:mysql://localhost:3306/hello?useUnicode=true&characterEncoding=utf-8", "root", "123456"); ps = ct.prepareStatement("select* from stu"); rs=ps.executeQuery(); while(rs.next()){ Vector hang=new Vector(); hang.add(rs.getString(1)); hang.add(rs.getString(2)); hang.add(rs.getString(3)); hang.add(rs.getInt(4)); hang.add(rs.getString(5)); hang.add(rs.getString(6)); //加入到rowData rowData.add(hang); } jt=new JTable(rowData,columnNames); //初始化 jp1=new JPanel(); jtf=new JTextField(10); jb1=new JButton("查询"); jl1=new JLabel("请输入名字"); jp1.add(jl1); jp1.add(jtf); jp1.add(jb1); //初始化下面的 jp2=new JPanel(); jb2=new JButton("添加"); jb3=new JButton("修改"); jb4=new JButton("查询"); jp2.add(jb2); jp2.add(jb3); jp2.add(jb3); //初始化 jsp=new JScrollPane(jt); //把jsp放入到JFrame this.add(jsp); this.add(jp1,"North"); this.add(jp2,"South"); this.setSize(400,300); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); } catch (Exception e) { e.printStackTrace(); }finally{ try { if(rs!=null) rs.close(); if(ps!=null) ps.close(); if(ct!=null) ct.close(); } catch (Exception e2) { e2.printStackTrace(); } } } }
创建抽象类的model
clss stuModel:
package com.test; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.Vector; import javax.swing.table.*; public class stuModel extends AbstractTableModel { Vector rowData, columnNames; Connection ct = null; ResultSet rs = null; PreparedStatement ps = null; public stuModel() { columnNames = new Vector(); columnNames.add("学号"); columnNames.add("名字"); columnNames.add("性别"); columnNames.add("年龄"); columnNames.add("籍贯"); columnNames.add("系别"); // rowData = new Vector();// 可以存放多行 try {// 1.加载驱动 Class.forName("com.mysql.jdbc.Driver"); // 2.、 ct = DriverManager.getConnection( "jdbc:mysql://localhost:3306/hello?useUnicode=true&characterEncoding=utf-8", "root", "123456"); ps = ct.prepareStatement("select* from stu"); rs = ps.executeQuery(); while (rs.next()) { Vector hang = new Vector(); hang.add(rs.getString(1)); hang.add(rs.getString(2)); hang.add(rs.getString(3)); hang.add(rs.getInt(4)); hang.add(rs.getString(5)); hang.add(rs.getString(6)); // 加入到rowData rowData.add(hang); } } catch (Exception e) { e.printStackTrace(); } finally { try { if (rs != null) rs.close(); if (ps != null) ps.close(); if (ct != null) ct.close(); } catch (Exception e2) { e2.printStackTrace(); } } } @Override public int getRowCount() {// 多少行 return this.rowData.size(); } @Override public int getColumnCount() {// 多少列 // TODO Auto-generated method stub return this.columnNames.size(); } @Override public Object getValueAt(int row, int column) { return ((Vector) this.rowData.get(row)).get(column); } }
test;
package com.test; import javax.swing.*; import java.util.*; import java.sql.*; import java.awt.*; import java.awt.Event; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class Test1 extends JFrame implements ActionListener { // rowData用来存放行数据 // columnNames用来存放列名 JTable jt = null; JScrollPane jsp = null; JPanel jp1, jp2; JLabel jl1; JButton jb1, jb2, jb3, jb4; JTextField jtf; public static void main(String[] args) { Test1 test = new Test1(); } public Test1() { // jt=new JTable(rowData,columnNames); // 初始化 jp1 = new JPanel(); jtf = new JTextField(10); jb1 = new JButton("查询"); jb1.addActionListener(this); jl1 = new JLabel("请输入名字"); jp1.add(jl1); jp1.add(jtf); jp1.add(jb1); // 初始化下面的 jp2 = new JPanel(); jb2 = new JButton("添加"); jb3 = new JButton("修改"); jb4 = new JButton("删除"); jp2.add(jb2); jp2.add(jb3); jp2.add(jb4); // 创建一个数据模型对象 stuModel sm = new stuModel(); // 初始化 jt = new JTable(sm); jsp = new JScrollPane(jt); // 把jsp放入到JFrame this.add(jsp); this.add(jp1, "North"); this.add(jp2, "South"); this.setSize(400, 300); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); } @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub if (e.getSource() == jb1) { System.out.println("用户希望被查询"); } } }
调用:public String getColumnName(int arg0) 来命名列
@Override public String getColumnName(int arg0) { return (String)this.columnNames.get(arg0); }