简体   繁体   中英

How to auto select all rows in a JTable and freeze the selection?

I would like to auto select all rows in a JTable and make it impossible to manually change the number of selected rows.

All rows are selected automatically and it should not be possible to alter the selection 在此输入图像描述

Selecting all rows is pretty straight forward

//Auto select all rows in the table
SwingUtilities.invokeLater(new Runnable(){
     public void run(){
        queueTable.selectAll();
     }
});

Next step is to remove any possibilities to set focus on the table

queueTable.setFocusable(false);

Here it stops, how to freeze the selection at this point?

queueTable.setRowSelectionAllowed(false); // unfortunately this will clear the current selection.

You can try to hardcode that in next way:

youeTable.setSelectionModel(new DefaultListSelectionModel(){
    @Override
    public void setSelectionInterval(int arg0, int arg1) {
        super.setSelectionInterval(0, t.getRowCount());
    }
});

in that case in your table always be selected rows from 0 to the end of the table.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM