java swing 怎么把数据放进table

java swing 怎么把数据放进table,第1张

直接用tablemodel就可以

JTable table = new JTable()

String[] tableHeads = new String[] { "姓名", "年龄", "是否通过", "性别" }

DefaultTableModel dtm = (DefaultTableModel) table.getModel()//创建model

dtm.setColumnIdentifiers(tableHeads)//创建表头,表头的类型可以是vector 或者Object[]

List arraylist1 = new ArrayList()

arraylist1.add("张三")

arraylist1.add("13")

arraylist1.add(new Boolean(false))

arraylist1.add("男")

Vector v1 = new Vector()

v1.add(arraylist1.get(0))

v1.add(arraylist1.get(1))

v1.add(arraylist1.get(2))

v1.add(arraylist1.get(3))

dtm.addRow(v1)//添加行数据,类型可以是vector 或者 object[]

这样就可以了··也可以最开始直接创建TableModel 在通过 JTable.setModel(model)方法把model放到table中··同样也建议自己多查查API

http://docs.oracle.com/javase/1.5.0/docs/api/overview-summary.html#overview_description

这个可以帮助你···或者直接看编译环境中的API 是一样的

一个类似的例子,从数据库里取数据放table里,请参考。

package Libary

import 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[]){

 

   }

}


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/bake/7987773.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-12
下一篇2023-04-12

发表评论

登录后才能评论

评论列表(0条)

    保存