Question

I have a JTable with contact names in. On selecting a row from this table a second table is filled with that person's primary phone number and email address. At the moment I am using a custom MouseListener to check when people are clicking on a row in the first table, but I also want them to be able to scroll through the list with their keyboard.

I have used ListSelectionListener on JLists in the past and vaguely remember someone saying that ListSelectionListener could be used on a JTable, but have been unable to find anywhere explaining how.

So, my question is.. Can ListSelectionListener (or similar) be used on a JTable? If so, how?

Image below - The image's Table is blurred so it probably doesn't help much;

enter image description here

Was it helpful?

Solution

A JTable works like a JList because it also has a ListSelectionModel. So you would just use:

table.getSelectionModel().addListSelectionListener(...);

OTHER TIPS

ListSelectionListener works on SelectionModel and since our table also has a selectionModel therefore we cann apply it on JTable as well.

tablename.getSelectionModel().addListSelectionListener(argument);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top