嗨我試圖從我的JTable更新特定的單元格。我正在進行預訂,一旦所選預訂獲得批准,它將從「否」更改爲「是」。 但是,在點擊批准後,ReservationStatus中的所有列都從No更改爲Yes,但我只想要更改特定的單元格(例如,第0行第6列)。 這是我對JTable代碼:從Jtable中更新特定的單元格在SQL中
table = new JTable(){
public boolean isCellEditable(int row, int col) {
return false;
};
};
table.getTableHeader().setReorderingAllowed(false);
table.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
row = table.rowAtPoint(e.getPoint());
col = table.columnAtPoint(e.getPoint());
data = table.getValueAt(row, col);
System.out.println("Row= " + row + "column =" + col + "Data= " + data);
DefaultTableModel model = (DefaultTableModel)table.getModel();
int selectedRowIndex = table.getSelectedRow();
if(e.getClickCount() == 2) {
if(col == 6){
frame.setContentPane(new approvalReservation(frame));
approvalReservation.lblDisplayAdminNo.setText(model.getValueAt(selectedRowIndex, 0).toString());
approvalReservation.lblDisplayMusicalInstrument.setText(model.getValueAt(selectedRowIndex, 1).toString());
approvalReservation.lblDisplayDuration.setText(model.getValueAt(selectedRowIndex, 2).toString());
approvalReservation.lblDisplayExtension.setText(model.getValueAt(selectedRowIndex, 5).toString());
}
else if(col == 7) {
frame.setContentPane(new approvalAccess(frame));
approvalAccess.lblDisplayAdminNo.setText(model.getValueAt(selectedRowIndex, 0).toString());
approvalAccess.lblDisplayLockerNumber.setText(model.getValueAt(selectedRowIndex, 3).toString());
approvalAccess.lblDisplayLocation.setText(model.getValueAt(selectedRowIndex, 4).toString());
}
}
}
});
scrollPane.setViewportView(table);
JButton btnLoadTable = new JButton("Load Table");
btnLoadTable.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
approvalController ac = new approvalController();
ArrayList<String> approvalList = approvalController.displayTableReservation();
for(int i = 0; i < approvalList.size(); i++) {
approvalList.get(i);
}
}
});
PS:我宣佈ROW和COL作爲公共靜態的,所以兩者都可以傳遞到另一個JPanel的。
而這是審批表代碼:
JButton btnApprove = new JButton("Approve");
btnApprove.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Connection connection=null;
connection = sqliteConnection.dbConnector();
try {
String query = "Update approval set reservationStatus = 'Yes' where reservationStatus = '" + approvalMain.data + "'";
PreparedStatement pst = connection.prepareStatement(query);
pst.executeUpdate();
System.out.println("Updated");
JOptionPane.showMessageDialog(null, "Data Updated");
}
catch(Exception e) {
e.printStackTrace();
}
}
});
btnApprove.setBackground(SystemColor.info);
btnApprove.setBounds(650, 361, 135, 50);
panel.add(btnApprove);
另一個問題是,當更改爲reservationStatus是「否」,則用戶無法點擊進入下一欄,AccessStatus。由於只有在ReservationStatus獲得批准後纔可點擊AccessStatus,因此從No到Yes。我該如何去做呢?到目前爲止,可以單擊列中的兩個單元格。
1)您的「批准」按鈕甚至不更新JTable,所以誰知道表中的值如何更改。你所有的代碼都會在你的數據庫上調用SQL。此外,您正在使用PreparedStatement不正確。你應該爲PreparedStatement設置參數。例如:https://stackoverflow.com/questions/15713360/geting-data-from-multiple-table-when-selecting-a-row-then-clicking-a-button/15713391#15713391 – camickr
2)爲什麼你使用MouseListener?看起來這個邏輯可能會完全刷新JTable。如果是這樣,那麼這告訴我你的SQL更新數據將所有的值設置爲「是」。因此,修復SQL並正確使用PreparedStatement,發現錯誤將更容易。不要忘記添加調試代碼來顯示傳遞給PreparedStatement的參數值。 – camickr
3)我甚至不確定爲什麼需要MouseListener或爲什麼需要重置內容窗格?編輯表格應導致表格自行更新。此外,您將使用'setModel(...)'方法來刷新表格,而不是創建一個全新的表格。 – camickr