简体   繁体   中英

Selecting only one column for multi-row selection JTable

I am new to swing and need help here

I have 3 columns in a JTable Column 1 data is JText with icon Column 2 is JComboBox Column 3 is JButton

I want functionality where only column 1 multi-row select should be allowed. When I multi-select Column 1 and then click any of the JButtons ( Column 3) , I should be able to get the selected Rows. The problem is I lose the selection as soon as I click the JButton.

The problem is I lose the selection as soon as I click the JButton.

Yes, this is a standard UI design on all application. When you click a row you change the selection. So the solution is to change your UI.

Your table should only contain two columns and NOT the button. Then you have two choice:

  1. Add JButton somewhere else on the panel. Then when you click the button the functionality will be invoked on the selected rows. For example check out the solution here: How to read the file, put data in JTable, then modify cell/delete lines and save data to the file?

  2. The option is to add a popup menu to the table so that when you right click the table the menu will display with options to perform on the selected rows. Check out the section from the Swing tutorial o Bringing Up a Popup Menu for a working example to get you started.

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