I have a JTable which only allows row selection using a ListSelectionListener as shown in the below code snippet. There are two problems in the JTable:
Q1) Although I set the first row selected by default using the setSelectionRow method, the related listSelection event is not fired when the program is started. The event is only fired if I click on another row but not the the first row – What do you think I should do to fix this considering the below code?
Q2) When I select a row, it isn’t colored in blue in JTable. I noticed this started to happen after I introduced ListSelectionListener to the JTable’s selection model. Before adding the listener, the table was making this color business when I selected a row – What do you think I should do to fix this considering the below code snippet? Thanks a lot.
jTableBookings = new javax.swing.JTable();
jTableBookings.setModel(new MyBookingTableModel(bookingTableData));
jTableBookings.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
jTableBookings.setRowSelectionAllowed(true);
jTableBookings.setCellSelectionEnabled(false);
jTableBookings.setColumnSelectionAllowed(false);
initColumnSizesForMinaBokBokningarJTable(jTableBookings);
//QUESTION 1
//Set selected row to first row for inital load.
if (jTableBookings.getModel().getRowCount() > 0) {
jTableBookings.setRowSelectionInterval(0, 0);
}
//QUESTION 2
jTableBookings.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
int sel = jTableBookings.getSelectedRow();
fillBookingRecordFields(sel); //Here we do some business logic based on the selected row
}
});
jScrollPane9.setViewportView(jTableBookings);
1) myTable.changeSelection(row, column, false, false);
2) disable setCellSelectionEnabled() and setColumnSelectionAllowed(), then works