Hi I am trying to update a specific cell from my JTable. I am doing a reservation where once the selected reservation has been approved, it will be changed from No to Yes. However, after clicking on Approve, All the column in ReservationStatus has been changed from No to Yes, although i want only a specific cell to change(Eg row 0 col 6). This is my code for the 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: I declared row and col as public static so both can be passed to another JPanel.
And this is the Approval Form codes:
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);
Another Question is, when the ReservationStatus is "No", the user is unable to click onto the next column, AccessStatus. As AccessStatus is only clickable after the ReservationStatus has been approved, from No to Yes. How do i go about doing that? As of now both cells inside the columns can be clicked.
setModel(...)
method to refresh the table, instead of creating a whole new table. – camickr