我有一个JTable。我已经在这样的方法中添加了列。
private void createSearchResultTable() {
DefaultTableColumnModel columnModel = new DefaultTableColumnModel();
String columnNames[] = {"Title", "Author", "Edition", "Availability", "Reserve"};
for (int i = 0; i < columnNames.length; i++) {
TableColumn column = new TableColumn();
column.setHeaderValue(columnNames[i]);
columnModel.addColumn(column);
}
tblBookSearchResults.setColumnModel(columnModel);
ButtonColumn buttonColumn = new ButtonColumn(tblBookSearchResults, reserveBook, 4);
buttonColumn.setMnemonic(KeyEvent.VK_ENTER);
}
现在我用从MySQL数据库中检索的数据填充JTable。
private boolean populateSearchResultTable(String title, String author, String publisher) {
con = DatabaseHandler.connectToDb();
try {
if (title.trim().length() != 0) {
pst = con.prepareStatement("SELECT title, author, edition, status FROM book WHERE title LIKE ? ");
pst.setString(1, "%" + title + "%");
}
else if (author.trim().length() != 0) {
// Say, this query is getting executed
pst = con.prepareStatement("SELECT title, author, edition, status FROM book WHERE author LIKE ? ");
//pst.setString(1, "%" + author + "%");
pst.setString(1, "Dan");
}
else if (publisher.trim().length() != 0) {
pst = con.prepareStatement("SELECT title, author, edition, status FROM book WHERE publisher LIKE ? ");
pst.setString(1, "%" + publisher + "%");
}
rs = pst.executeQuery();
int rowNum = 0;
while (rs.next()) {
tblBookSearchResults.setValueAt(rs.getString(1), rowNum, 1);
}
return true;
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e.getLocalizedMessage());
}
finally {
}
return false;
}
检索数据集没有问题但是当我将值设置为JTable时,它看起来像这样。
第一个值在所有列中重复出现。我无法弄清楚为什么会这样?任何关于如何纠正这一点的建议都将不胜感激。
谢谢。
答案 0 :(得分:2)
更新JTable#setValue
时请勿使用JTable
,而是通过模型添加新行或修改现有行。
此外,您没有递增rowNum
值,因此您始终与表格的第一行进行交互
简单示例
使用Swing Timer
向模型添加新行的简单示例...
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;
public class TestTableModel01 {
public static void main(String[] args) {
new TestTableModel01();
}
public TestTableModel01() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
final DefaultTableModel model = new DefaultTableModel(new Object[]{"A", "B", "C", "D", "E"}, 0);
JTable table = new JTable(model);
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new JScrollPane(table));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
Timer timer = new Timer(500, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (model.getRowCount() < 100) {
int row = model.getRowCount();
model.addRow(new Object[]{
row + "x" + 0,
row + "x" + 1,
row + "x" + 2,
row + "x" + 3,
row + "x" + 4
});
} else {
((Timer)(e.getSource())).stop();
}
}
});
timer.start();
}
});
}
}