Object[][] cellData = {{"11", "12"},{"21", "22"}}//定义表格数据字段
JTable table = new JTable(cellData, columnNames)
然后可以直接将这个table添加到指定的容器上,例如有一个JFrame对象f,可以通过f.add(table)的方式来添加。
一个类似的例子,从数据库里取数据放table里,请参考。
package Libaryimport java.awt.Cursor
import java.sql.ResultSet
import java.sql.SQLException
import java.sql.Statement
import java.util.Vector
import javax.swing.*
import javax.swing.table.DefaultTableModel
public class BorrowBookLog extends JFrame{
/**
*
*/
private static final long serialVersionUID = 5922888622610809963L
String BBookName, BBookId
JButton Borrow, Cancel
Statement SearchStmt
@SuppressWarnings({ "rawtypes", "unchecked" })
BorrowBookLog(){
ConDB CB3 = new ConDB()
CB3.connectionDB()
try {
SearchStmt = CB3.dbConn.createStatement()
} catch (SQLException e1) {
// TODO 自动生成的 catch 块
e1.printStackTrace()
}
DefaultTableModel tableModel = new DefaultTableModel()
String[] tableHeads= {"图书编号","图书名称","借阅人", "借阅时间"}
Vector cell
Vector row = new Vector()
Vector tableHeadName = new Vector()
for(int i = 0i<tableHeads.lengthi++){
tableHeadName.add(tableHeads[i])
}
try {
ResultSet s =SearchStmt.executeQuery("select * from BorrowedBooks where StudentId ="+ "'"+ StudentMainFrame.Name+"'")
while(s.next()){
cell = new Vector()
cell.add(s.getString("BookId"))
cell.add(s.getString("BookName"))
cell.add(s.getString("StudentId"))
cell.add(s.getString("BorrowedDate"))
row.add(cell)
BBookId = s.getString("BookId")
BBookName = s.getString("BookName")
}
} catch (SQLException e1) {
// TODO 自动生成的 catch 块
e1.printStackTrace()
}
setTitle("查询结果")
tableModel.setDataVector(row, tableHeadName)
JTable table = new JTable(tableModel)
table.setRowHeight(20)
table.setCursor(new Cursor(12))
getContentPane().setLayout(null)
JScrollPane scrollPane = new JScrollPane(table)
scrollPane.setBounds(10, 10, 420, 200)
scrollPane.setCursor(new Cursor(12))
this.getContentPane().add(scrollPane)
setLocation(450,220)
setSize(450,300)
setVisible(true)
}
public static void mian(String args[]){
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)