您好我正在尝试更新JTable中的特定单元格。我正在预订,一旦选定的预订被批准,它将从否改为是。 但是,单击Approve后,ReservationStatus中的所有列都已从No更改为Yes,尽管我只想更改特定单元格(例如,第0行第6列)。 这是我的Jtable代码:
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声明为public static,因此两者都可以传递给另一个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为&#34; No&#34;时,用户无法点击下一列AccessStatus。由于AccessStatus仅在ReservationStatus被批准后可以点击,因此从No到Yes。我该怎么做呢?到目前为止,可以单击列中的两个单元格。
答案 0 :(得分:0)
String query = "Update approval set reservationStatus = 'Yes' where reservationStatus = '" + approvalMain.data + "'";
假设approvalMain.data
等于'No'
,此查询会说明更新当前状态为'No'
的每一行的状态。如果您只想更改一行,则需要在where
子句中指定它,最好使用表格的主键列。