Programmatically select a row in JTable

后端 未结 3 1015
予麋鹿
予麋鹿 2020-12-24 10:37

When the application is started, none of the rows is selected. But I would like to show that the first row is already selected.

How to do this? Do I need to set the

相关标签:
3条回答
  • 2020-12-24 11:05

    You can do it calling setRowSelectionInterval :

    table.setRowSelectionInterval(0, 0);
    

    to select the first row.

    0 讨论(0)
  • 2020-12-24 11:15

    You use the available API of JTable and do not try to mess with the colors.

    Some selection methods are available directly on the JTable (like the setRowSelectionInterval). If you want to have access to all selection-related logic, the selection model is the place to start looking

    0 讨论(0)
  • 2020-12-24 11:21

    It is an old post, but I came across this recently

    Selecting a specific interval

    As @aleroot already mentioned, by using

    table.setRowSelectionInterval(index0, index1);
    

    You can specify an interval, which should be selected.

    Adding an interval to the existing selection

    You can also keep the current selection, and simply add additional rows by using this here

    table.getSelectionModel().addSelectionInterval(index0, index1);
    

    This line of code additionally selects the specified interval. It doesn't matter if that interval already is selected, of parts of it are selected.

    0 讨论(0)
提交回复
热议问题