我想在每次添加行时显示JTable。 我在下面写的代码向表添加一个新行,然后显示jtable 2秒,然后添加另一行,然后显示表。但jtable没有显示, 它仅在添加所有行
后显示 Vector<Object> rowVector = null;
while(rs.next()){
rowVector = new Vector<Object>();
for(int i=1;i<=columnCount;i++){
rowVector.add(rs.getString(i));
//System.out.print(rs.getString(i));
}
System.out.println(rowVector.toString());
//data.add(rowVector);
model.addRow(rowVector);
JTable table1 = new JTable(model);
JTableHeader header1 = table1.getTableHeader();
table1.setEnabled(false);
header1.setResizingAllowed(false);
header1.setReorderingAllowed(false);
header1.setForeground(Color.white);
header1.setBackground(Color.black);
jpanedisplay.setViewportView(table1);
jpanedisplay.getViewport().setBackground(Color.white);
jpanedisplay.setBorder(BorderFactory.createLineBorder(Color.black));
Thread.sleep(3000);
}
答案 0 :(得分:1)
您在每次迭代中创建一个新的JTable
,您不需要这样做。在循环之前将模型添加到表中。只需在循环中向模型添加行
DefaultTableModel model = new DefaultTableModel(columnNames, 0);
JTable table = new JTable(model);
table.setEnabled(false);
JTableHeader header1 = table.getTableHeader();
header1.setResizingAllowed(false);
header1.setReorderingAllowed(false);
header1.setForeground(Color.white);
header1.setBackground(Color.black);
jpanedisplay.setViewportView(table1);
jpanedisplay.getViewport().setBackground(Color.white);
jpanedisplay.setBorder(BorderFactory.createLineBorder(Color.black));
...
while(rs.next()){
Vector<Object> rowVector = new Vector<Object>();
for(int i=1;i<=columnCount;i++){
rowVector.add(rs.getString(i));
//System.out.print(rs.getString(i));
}
model.addRow(rowVector);
// DON'T CALL Thread.sleep();
}
<强>更新强>
如果您不想在数据库任务完成之前添加表,只需使用方法
private JTable createTable(ResultSet rs) {
DefaultTableModel model = new DefaultTableModel(columnNames, 0);
JTable table = new JTable(model);
table.setEnabled(false);
JTableHeader header1 = table.getTableHeader();
header1.setResizingAllowed(false);
header1.setReorderingAllowed(false);
header1.setForeground(Color.white);
header1.setBackground(Color.black);
while(rs.next()){
Vector<Object> rowVector = new Vector<Object>();
for(int i=1;i<=columnCount;i++){
rowVector.add(rs.getString(i));
}
model.addRow(rowVector);
}
return table;
}
然后你可以将它添加到任何面板
panel.add(new JScrollPane(createTable(rs));
我使用java.swing.Timer
添加行。
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import javax.swing.table.DefaultTableModel;
public class TestTimerTable {
String[] cols = {"ID", "Name", "Country Code", "District", "Population"};
DefaultTableModel model = new DefaultTableModel(cols, 0);
JTable table = new JTable(model);
ResultSet rs = null;
JButton start = new JButton("Start");
Timer timer = new Timer(0, null);
public TestTimerTable() {
initDB();
JScrollPane scroll = new JScrollPane(table);
scroll.setViewportView(table);
JFrame frame = new JFrame("Test Timer Table");
frame.add(scroll);
frame.add(start, BorderLayout.SOUTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
timer = new Timer(200, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
if (rs.next()) {
String s1 = rs.getString(1);
String s2 = rs.getString(2);
String s3 = rs.getString(3);
String s4 = rs.getString(4);
String s5 = rs.getString(5);
model.addRow(new Object[]{s1, s2, s3, s4, s5});
} else {
((Timer) e.getSource()).stop();
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}
});
start.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
timer.start();
}
});
}
private void initDB() {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost/...", "...", "...");
PreparedStatement ps = conn.prepareStatement("SELECT * FROM city");
rs = ps.executeQuery();
} catch (Exception ex) {
ex.printStackTrace();
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new TestTimerTable();
}
});
}
}