把读出来的数据封装在一个集合里面,然后把集合绑定到JTable上面去。
这是我写的一个填充Table的方法,调用它就可以把数据填充到Jtabl控件上去
public void fillTable() {
DefaultTableModel model = (DefaultTableModel) jTable1.getModel()
ArrayList<User>users = biz.getAllUser()
model.setColumnIdentifiers(new Object[]{"Id","UserName","Password"})
model.setRowCount(0)
for (User user : users) {
model.addRow(new Object[] { user.getUserId(), user.getUserName(),
user.getUserPwd() })
}
jTable1.revalidate()
}
我有代码可以发给你
效果图
面板中放入2个表格, 可以有多种实现方式, 比如表格布局1行2列, 或者绝对布局
我使用JSplitPane 分割面板来实现
参考代码如下
import java.util.Arraysimport java.util.Vector
import javax.swing.BorderFactory
import javax.swing.JFrame
import javax.swing.JScrollPane
import javax.swing.JSplitPane
import javax.swing.JTable
import javax.swing.table.DefaultTableModel
public class FrameDemo extends JFrame {
public FrameDemo() {
JSplitPane jsp = new JSplitPane()
jsp.setOrientation(JSplitPane.HORIZONTAL_SPLIT)// 设置分割线方向
// 表格数据模型
DefaultTableModel model = new DefaultTableModel()
model.addColumn("姓名", new Vector<String>())
model.addColumn("分数", new Vector<Integer>())
JTable t1 = new JTable(model)// 创建表格,并添加模型
JScrollPane jp1 = new JScrollPane(t1)
jp1.setBorder(BorderFactory.createTitledBorder("无数据表格"))// 设置标题边框
jsp.setLeftComponent(jp1)
DefaultTableModel model2 = new DefaultTableModel()
model2.addColumn("姓名", new Vector<String>(Arrays.asList("张三", "李四", "王五")))
model2.addColumn("分数", new Vector<Integer>(Arrays.asList(90, 87, 65)))
JTable t2 = new JTable(model2)
JScrollPane jp2 = new JScrollPane(t2)
jp2.setBorder(BorderFactory.createTitledBorder("有数据表格"))
jsp.setRightComponent(jp2)
add(jsp)
setTitle("测试窗口")
setDefaultCloseOperation(EXIT_ON_CLOSE)
setSize(400, 300)
setLocationRelativeTo(null)
setVisible(true)
jsp.setDividerLocation(0.5)// 分隔面板 平均分配
}
public static void main(String[] args) {
new FrameDemo()
}
}
public class Stmessege1 extends JFrame {private static final long serialVersionUID = 1L
private JTable table// 声明表格
void Stmessege11() {
setSize(460, 320)
setVisible(true)
}
public Vector getDataVector() throws SQLException, ClassNotFoundException {
Connection conn = null
java.sql.Statement stmt = null
Class.forName("com.mysql.jdbc.Driver")
String dburl = "jdbc:mysql://localhost:3306/hp"
String dbuser = "root"
String dbpassword = "19920102"
conn = DriverManager.getConnection(dburl, dbuser, dbpassword)
stmt = conn.createStatement()
String sql = "select* from xsgli"
ResultSet rs = stmt.executeQuery(sql)
Vector dataVector = new Vector()
while (rs.next()) {
Vector rowVector = new Vector()
for (int i = 1i <= 7i++) {
if (i == 2 || i == 4) {
rowVector.add(String.valueOf(rs.getInt(i)))
} else {
rowVector.add(rs.getString(i))
}
}
dataVector.add(rowVector)
}
return dataVector
}
public Vector getColumnVector() {
Vector columnVector = new Vector()
columnVector.add("姓名")
columnVector.add("学号")
columnVector.add("性别")
columnVector.add("班级")
columnVector.add("系别")
columnVector.add("成绩")
columnVector.add("专业")
return columnVector
}
public Stmessege1() throws SQLException, ClassNotFoundException {
super()
setTitle("学生学籍数据显示")
setBounds(100, 100, 382, 223)
// setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
Connection conn = null
Statement stmt = null
Class.forName("com.mysql.jdbc.Driver")
String dburl = "jdbc:mysql://localhost:3306/hp"
String dbuser = "root"
String dbpassword = "19920102"
conn = DriverManager.getConnection(dburl, dbuser, dbpassword)
java.sql.Statement st = conn.createStatement()
String sql = "select* from xsgli"
ResultSet rs = st.executeQuery(sql)
Vector columnVector = getColumnVector()
Vector dataVector = getDataVector()
final JScrollPane scrollPane = new JScrollPane()
getContentPane().add(scrollPane, BorderLayout.CENTER)
table = new JTable(dataVector, columnVector)
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF)
scrollPane.setViewportView(table)
}
}
希望能帮到你,谢谢采纳
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)