怎样把读取出来的vector集合放到Jtable里面
2个回答
展开全部
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.*;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class lx {
JTable jt;
JScrollPane jsp;
public lx(){
JFrame jf=new JFrame("测试");
jf.setSize(400,400);
String[] s={"姓名","年龄","性别"};
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Vector o2=new Vector();
Vector o3=new Vector();
Vector o4=new Vector();
o2.add(s[0]);
o3.add(s[1]);
o4.add(s[2]);
Vector<Vector> o1=new Vector<Vector>();
o1.add(o2);
o1.add(o3);
o1.add(o4);
Vector<Vector<Object>> date=new Vector<Vector<Object>>();
// o2.add(s3);
try {
Connection con;
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
PreparedStatement ps = null;
con = DriverManager.getConnection(
"jdbc:sqlserver://localhost:1433;DatebaseName=NetBar1",
"sa", "123");
String sql = "select * from a1";
PreparedStatement ps1 = con.prepareStatement(sql);
ResultSet rs = ps1.executeQuery();
while (rs.next()) {
Vector<Object> row = new Vector<Object>();
row.add(rs.getString("name"));
row.add(rs.getString("age"));
row.add(rs.getString("sex"));
date.add(row);
}
} catch (Exception e) {}
jt=new JTable(date,o1);
//jt.setModel(new DefaultTableModel(date,o1));
jsp=new JScrollPane(jt);
jf.add(jsp);
jf.setVisible(true);
}
public static void main(String[]args){
lx s=new lx();
}
}
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.*;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class lx {
JTable jt;
JScrollPane jsp;
public lx(){
JFrame jf=new JFrame("测试");
jf.setSize(400,400);
String[] s={"姓名","年龄","性别"};
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Vector o2=new Vector();
Vector o3=new Vector();
Vector o4=new Vector();
o2.add(s[0]);
o3.add(s[1]);
o4.add(s[2]);
Vector<Vector> o1=new Vector<Vector>();
o1.add(o2);
o1.add(o3);
o1.add(o4);
Vector<Vector<Object>> date=new Vector<Vector<Object>>();
// o2.add(s3);
try {
Connection con;
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
PreparedStatement ps = null;
con = DriverManager.getConnection(
"jdbc:sqlserver://localhost:1433;DatebaseName=NetBar1",
"sa", "123");
String sql = "select * from a1";
PreparedStatement ps1 = con.prepareStatement(sql);
ResultSet rs = ps1.executeQuery();
while (rs.next()) {
Vector<Object> row = new Vector<Object>();
row.add(rs.getString("name"));
row.add(rs.getString("age"));
row.add(rs.getString("sex"));
date.add(row);
}
} catch (Exception e) {}
jt=new JTable(date,o1);
//jt.setModel(new DefaultTableModel(date,o1));
jsp=new JScrollPane(jt);
jf.add(jsp);
jf.setVisible(true);
}
public static void main(String[]args){
lx s=new lx();
}
}
追问
谢谢,我是直接用的vector(object[][],object[])
追答
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.table.TableColumn;
public class table extends JFrame {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
table t = new table();
t.setSize(300, 400);
ArrayList al = new ArrayList();
Object[] columnNames = { "编号", "姓名","是否添加?" };
for(int i=0;i<20;i++){
al.add(i,"姓名"+i);
}
Object[][] rowData = new Object[al.size()][3];
for(int i=0;i<al.size();i++){
rowData[i][0]=i;
rowData[i][1]=al.get(i);
rowData[i][2]=false;
}
JTable ta = new JTable(rowData, columnNames);
JScrollPane jsp = new JScrollPane(ta);
TableColumn tab=ta.getColumn(columnNames[0]);
tab.setMaxWidth(40);
ta.getColumn(columnNames[1]).setPreferredWidth(50);
TableColumn tc3=ta.getColumn(columnNames[2]);
tc3.setCellEditor(ta.getDefaultEditor(Boolean.class));
tc3.setCellRenderer(ta.getDefaultRenderer(Boolean.class));
t.add(jsp);
t.setVisible(true);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询