0

I have 7 tables on my screen. Since they are independent tables, I'am able to select a cell from each one of them.

Although I want that every time I make a selection of a cell in a table, I want a deselection of cell from the table from which cell was previously selected and so one for other tables as well.

Basically I want selection of a cell from a single table at a time.

Also I would want deselection of the selected cell whenever mouse is clicked anywhere on the screen.

I've tried clearselection(), but it doesn't seem to work.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
shashank
  • 11
  • 2
  • 1
    *"I've tried `clearselection()`, but it doesn't seem to work."* Of course it works if used correctly. The only conclusion we can come to is that the code is not using it correctly. On that subject.. For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). (Include two tables, rather than 7.) – Andrew Thompson Aug 05 '16 at 18:01
  • reducing number of tables doesn't cater to the requiredments – shashank Aug 05 '16 at 18:03
  • 1
    *"reducing number of tables doesn't cater to the requiredments"* The ultimate requirement is irrelevant. If you can manage to do it with just two tables, it should be easy to do with 3, 10 or a 1000. I will point out that 'failing to provide an MCVE' is listed in two separate 'close this question' reasons. I've already added a close vote based on one of those. So make up your mind if you want a solution to the stated problem, and if so, post an MCVE. Because if 4 more people agree with my assessment, this question will be closed.. – Andrew Thompson Aug 05 '16 at 18:07

1 Answers1

1

Create a TableGroup to enforce SINGLE_SELECTION across a group of tables, each having its own ListSelectionModel. The example below contains a List<JTable> that can manage the selection for an arbitrary number of tables.

Usage:

SelectionGroup group = new SelectionGroup();
group.add(new JTable(…));
group.add(new JTable(…));
…

Code:

private static class TableGroup {

    private final List<JTable> list = new ArrayList<>();

    private void add(JTable table) {
        list.add(table);
        ListSelectionModel model = table.getSelectionModel();
        model.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        model.addListSelectionListener(new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent e) {
                if (!e.getValueIsAdjusting()) {
                    for (JTable t : list) {
                        if (!t.equals(table)) {
                            t.clearSelection();
                        }
                    }
                }
            }
        });
    }
}

TableGroup is conceptually similar to a ButtonGroup, seen here and here.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • The code is not working as intended Once i select a table the previously selected table gets deselected but the currently selected table also gets deselected. But i want to retain the selection for the currently selected table. – shashank Aug 07 '16 at 12:38
  • I am unable to reproduce the effect you describe; please edit your question to include a [mcve] that shows your current approach. – trashgod Aug 07 '16 at 12:51