sam
sam

Reputation: 95

JTable multiple selection without using keyboard only using mouse

Here is my code. I want to select multiple rows in JTable, I am using the following line:

table.getColumnModel().getSelectionModel().setSelectionMode(
    javax.swing.ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

With the above line I am able to select multiple rows using keyboard, but requirement is to select only using mouse.

Is there any thing other than this, that Java provides for multiple selection only using mouse without using keyboard?

Upvotes: 1

Views: 2822

Answers (4)

Guttilyn
Guttilyn

Reputation: 1

I finally used this code:

JTable table = new JTable(){
    @Override
    public void changeSelection(int rowIndex, int columnIndex, boolean toggle, boolean extend) {
        super.changeSelection(rowIndex, columnIndex, true, extend);
    }
};

This way CTRL(toogle) is always pushed(true).

Upvotes: 0

SSC
SSC

Reputation: 3008

Yes, you can select multiple rows without using keyboard by overriding the changeSelection function, like this:

@Override
        public void changeSelection(int rowIndex, int columnIndex, boolean toggle, boolean extend) {
            latestClickedRowIndex = rowIndex;
            ListSelectionModel selectionModel = getSelectionModel();
            boolean selected = selectionModel.isSelectedIndex(rowIndex);
            //throw new UnsupportedOperationException("Paila.");
            if (selected) {
                selectionModel.removeSelectionInterval(rowIndex, rowIndex);
                getValueAt(rowIndex, columnIndex);
            } else {
                selectionModel.addSelectionInterval(rowIndex, rowIndex);
            }
        }

Upvotes: 2

Doszi89
Doszi89

Reputation: 357

If you have this code, you need only yo push ctrl + click multiple.

Edit: But if you don't want to use keyboard it's possible i think, try this:

Select Multiple Items In JList Without Using The Ctrl/Command Key

Upvotes: 2

JB Nizet
JB Nizet

Reputation: 691635

I don't think it's possible. I suggest adding an additional column to the table, containing a checkbox allowing to mark the row as selected. Of course, you won't be able to use the table selection model to know which rows are selected.

Upvotes: 2

Related Questions