I have this piece of code:
listTable.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 1) {
JTable target = (JTable)e.getSource();
int row = target.getSelectedRow();
int column = target.getSelectedColumn();
String title = listTable.getValueAt(row, column).toString();
String duration = listTable.getValueAt(row, column+1).toString();
String genre = listTable.getValueAt(row, column+2).toString();
String actor = listTable.getValueAt(row, column+3).toString();
String director = listTable.getValueAt(row, column+4).toString();
//String rentable = listTable.getValueAt(row, column+5).toString();
//String synopsis = listTable.getValueAt(row, column+6).toString();
txtTitle.setText(title);
txtDuration.setText(duration);
txtGenre.setText(genre);
}
});
Which enables me:
- When a row is selected it passes the values in the columns to JTextBoxes.
However, when I don’t click the first column by default, the selection gets garbled up. For example, if I don’t click on the “Title” column first and click on the “Duration” column, it puts the duration in txtTitle and the others are also mixed up.
How do I add a piece of code that when a row is selected, it default-selects the first column ?
This is a screenshot of what happens:

Thanks,
Brian
Rather than forcing selection of column why not always extract values based on known columns?
For example you can replace:
with:
If you provide ability to reorder columns then you can instead try
table.getModel().getValueAt(convertRowIndextoModel(row), 0);