简体   繁体   中英

JTable Selection with the help of a combobox?

I have a combobox with values of 1 to 5 and a JTable of 5X5... Whenever a select a value from combobox corresponding entire column of the JTable has to get selected... how do I proceed with this...

First you need to configure your table to allow for column selection:

table.setColumnSelectionAllowed( true );
table.setRowSelectionAllowed( false );

Then for the combo box you need to add an ActionListener to select the column based on the selected item index:

table.setColumnSelectionInverval(...);

Get value of selected item in combobox as comboBox.getSelectedItem() and parse it to integer and then call following method:

public void getSelected(int comboBoxValue){
    table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

    // The following column selection method works 
    // only if these properties are set
    table.setColumnSelectionAllowed(true);
    table.setRowSelectionAllowed(false);

    table.setColumnSelectionInterval(comboBoxValue, comboBoxValue);
}

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