0
我不知道我在做什麼錯,但我從MySQL數據庫獲取數據並使用DefaultTableModel填充JTable。然後我在屏幕上顯示它。數據從文本框添加到數據庫後,JTable不會刷新。我檢查了數據庫,並插入數據在那裏。我試着用table.repaint()
或dTableModel.fireTableDataChanged()
在事件監聽器中添加按鈕,但仍然沒有。下面是代碼:JTable不會重新刷新/刷新數據插入到mysql
public class DbTable {
private JLabel lFirstName, lLastName, lState, lBirthDate;
private JTextField fFirstName, tLastName, tState, tBirthDate;
private JButton btnAdd;
private Object[][] databaseResults;
private Object[] columns = { "Col 1", "Col 2", "Col 3", "Col 4",
"Col 5", "ЈCol 6", "Col 7" };
private ResultSet rows;
private ResultSet rowsCb;
private JComboBox combobox;
private DefaultTableModel dTableModel = new DefaultTableModel(
databaseResults, columns) {
public Class getColumnClass(int column) {
Class returnValue;
if ((column >= 0) && (column < getColumnCount())) {
returnValue = getValueAt(0, column).getClass();
} else {
returnValue = Object.class;
}
return returnValue;
}
};
private JTable table = new JTable(dTableModel);
Connection conn = null;
public static void main(String[] args) {
DbTable dbTable = new DbTable();
dbTable.getTable();
}
public void getTable() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(
"jdbc:mysql://localhost/database", "root", "");
Statement sqlState = conn.createStatement(
ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
String selectStuff = "SELECT `invoice`.`id` , `client`, `date`, `project`, `amount`,`unitPrice`, `total`"
+ "FROM `invoice` , `clients`"
+ "WHERE `invoice`.`clientID` = `clients`.`id`"
+ "ORDER BY `invoice`.`id` ASC ";
rows = sqlState.executeQuery(selectStuff);
Object[] tempRow;
while (rows.next()) {
tempRow = new Object[] { rows.getInt(1), rows.getString(2),
rows.getString(3), rows.getString(4), rows.getInt(5),
rows.getInt(6), rows.getDouble(7) };
dTableModel.addRow(tempRow);
}
} catch (SQLException e) {
e.getMessage();
} catch (ClassNotFoundException e) {
e.getMessage();
}
table.setFont(new Font("Tahoma", Font.PLAIN, 14));
table.setAutoCreateRowSorter(true);
JScrollPane scrollPane = new JScrollPane(table);
// frame.add(combobox, BorderLayout.NORTH);
frame.add(scrollPane, BorderLayout.NORTH);
// Define values for my labels
lFirstName = new JLabel("Klijent ID: ");
lLastName = new JLabel("Datum: ");
lState = new JLabel("Projekat: ");
lBirthDate = new JLabel("Ukupno"); // Define the size of text fields
fFirstName = new JTextField(10);
tLastName = new JTextField(10);
tBirthDate = new JTextField(10);
tState = new JTextField(10);
btnAdd = new JButton("Add");
btnAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String sDatum = "", sProjekat = "";
int sKlijenId;
double sUkupno = 0.00;
sKlijenId = Integer.parseInt(fFirstName.getText());
sDatum = tLastName.getText();
sProjekat = tState.getText();
sUkupno = Double.parseDouble(tBirthDate.getText());
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(
"jdbc:mysql://localhost/sjajplusz", "root", "");
Statement sqlState = conn.createStatement(
ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
String selectStuff = "SELECT * FROM invoice";
rows = sqlState.executeQuery(selectStuff);
rows.moveToInsertRow();
rows.updateInt("clientID", sKlijenId);
rows.updateString("date", sDatum);
rows.updateString("project", sProjekat);
rows.updateDouble("total", sUkupno);
rows.insertRow();
rows.updateRow();
} catch (SQLException e1) {
e1.getMessage();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int invoiceId = 0;
try {
rows.last();
invoiceId = rows.getInt(1);
} catch (SQLException e) {
e.getMessage();
}
// Object[] invoice = {invoiceId, sKlijenId, sDatum, sProjekat, "", "", sUkupno};
//
// dTableModel.addRow(invoice);
dTableModel.fireTableDataChanged();
}
});
JPanel inputPanel = new JPanel(); // Put components in the panel
inputPanel.add(lFirstName);
inputPanel.add(fFirstName);
inputPanel.add(lLastName);
inputPanel.add(tLastName);
inputPanel.add(lState);
inputPanel.add(tState);
inputPanel.add(lBirthDate);
inputPanel.add(tBirthDate);
inputPanel.add(btnAdd);
frame.add(inputPanel, BorderLayout.SOUTH);
// frame.add(scrollPane, BorderLayout.CENTER);
frame.setSize(500, 400);
frame.setVisible(true);
try {
conn.close();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
基本上這是跟進此YouTube系列Java JTable 38
該代碼只是yt系列教程的後續內容。我如何刷新'DefaultTableModel'? –
'DefaultTableModel'提供'setDataVector()'方法,您可以在其中傳遞新數據。 – Kai
好吧,我已經得到它的工作是這樣的: 'dTableModel.setNumRows(0); table.setModel(dTableModel); table.setModel(getData()); ' 和'getData()'是從db中獲取新鮮數據的獨立方法。 有沒有更好的方法來做到這一點? –