SagittariusA
SagittariusA

Reputation: 5427

JTable: waiting for row to be selected according user input

I have a JTable (master) to which I added a ListSelectionListener to catch when a row is selected and populate another JTable (detail) according the selected row. Now some requirements have changed and if come conditions are met, when the user clicks into another row, I should show a JOptionPane with YES/NO options, and only if YES is clicked then the new row can be selected. How can I achieve this? Shall I use always ListSelectionListener? I don't think so as it is raised only after the selection is done.

Upvotes: 0

Views: 173

Answers (2)

camickr
camickr

Reputation: 324118

when the user clicks into another row,

What about when the user used the up/down arrow keys to move to another row?

The default behaviour in both cases is to select the row automatically.

You might be able to override the changeSelection(...) method of the JTable. I believe this is the method that is invoked by the mouse or keyboard logic to change row selection.

So you would add your logic to display the option pane and then if "Yes" is selected you would invoke super.changeSelection(...).

However, I would find it frustrating to try and use the keyboard to scroll down the table if this option pane keeps popping up. Remember to design a user interface that support both mouse and keyboard effectively.

Upvotes: 0

Phan Hiếu Nghĩa
Phan Hiếu Nghĩa

Reputation: 49

yes, for sure

table.getSelectionModel().addListSelectionListener(new ListSelectionListener(){
        public void valueChanged(ListSelectionEvent event) {
            // do some actions here, for example
            // print first column value from selected row
            System.out.println(table.getValueAt(table.getSelectedRow(), 0).toString());
        }
    });

Upvotes: 1

Related Questions