Selecting multiple rows of JTable
You need to allow multiple selection:
table.setRowSelectionAllowed(true);
table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
Then you need to write appropriate selection listener. It's a bit harder, try to find in google related solutions. You can look at an example of selection listener.
How to select multiple rows in JTable?
you can allow multiple selection by jTable.setRowSelectionAllowed(true);
jTable.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
and you can get the values by
if (jTable.getSelectedRows() > -1) {
int[] selectedrows = jTable.getSelectedRows();
for (int i = 0; i < selectedrows.length; i++)
{
System.out.println(jTable.getValueAt(selectedrows[i], 0).toString());
}
}
Java Swing JTable select programmatically multiple rows
To select just one row, pass it as both the start and end index:
table.setRowSelectionInterval(18, 18);
Or, if you want to select multiple, non-contiguous indices:
ListSelectionModel model = table.getSelectionModel();
model.clearSelection();
model.addSelectionInterval(1, 1);
model.addSelectionInterval(18, 18);
model.addSelectionInterval(23, 23);
Alternately, you may find that implementing your own subclass of ListSelectionModel
and using it to track selection on both the table and the scatterplot is a cleaner solution, rather than listening on the scatterplot and forcing the table to match.
Using CTRL+CLICK to select multiple rows in JTable
One of my mouse listeners was clearing the selections which made it appear that the control+click was not working. Thanks for reviewing my issue.
Multiple row selection in JTable
Using @Hovercraft's example and @camickr's advice, the example below shows a suitable user interface. Although it uses buttons, the SelectionAction
would also be suitable for a menu or popup.
import java.awt.*;
import java.awt.event.ActionEvent;
import javax.swing.*;
import javax.swing.DefaultListSelectionModel;
import javax.swing.table.DefaultTableModel;
/** @see http://stackoverflow.com/questions/4526779 */
public class CheckABunch extends JPanel {
private static final int CHECK_COL = 1;
private static final Object[][] DATA = {
{"One", Boolean.TRUE}, {"Two", Boolean.FALSE},
{"Three", Boolean.TRUE}, {"Four", Boolean.FALSE},
{"Five", Boolean.TRUE}, {"Six", Boolean.FALSE},
{"Seven", Boolean.TRUE}, {"Eight", Boolean.FALSE},
{"Nine", Boolean.TRUE}, {"Ten", Boolean.FALSE}};
private static final String[] COLUMNS = {"Number", "CheckBox"};
private DataModel dataModel = new DataModel(DATA, COLUMNS);
private JTable table = new JTable(dataModel);
private DefaultListSelectionModel selectionModel;
public CheckABunch() {
super(new BorderLayout());
this.add(new JScrollPane(table));
this.add(new ControlPanel(), BorderLayout.SOUTH);
table.setPreferredScrollableViewportSize(new Dimension(250, 175));
selectionModel = (DefaultListSelectionModel) table.getSelectionModel();
}
private class DataModel extends DefaultTableModel {
public DataModel(Object[][] data, Object[] columnNames) {
super(data, columnNames);
}
@Override
public Class<?> getColumnClass(int columnIndex) {
if (columnIndex == CHECK_COL) {
return getValueAt(0, CHECK_COL).getClass();
}
return super.getColumnClass(columnIndex);
}
@Override
public boolean isCellEditable(int row, int column) {
return column == CHECK_COL;
}
}
private class ControlPanel extends JPanel {
public ControlPanel() {
this.add(new JLabel("Selection:"));
this.add(new JButton(new SelectionAction("Clear", false)));
this.add(new JButton(new SelectionAction("Check", true)));
}
}
private class SelectionAction extends AbstractAction {
boolean value;
public SelectionAction(String name, boolean value) {
super(name);
this.value = value;
}
@Override
public void actionPerformed(ActionEvent e) {
for (int i = 0; i < dataModel.getRowCount(); i++) {
if (selectionModel.isSelectedIndex(i)) {
dataModel.setValueAt(value, i, CHECK_COL);
}
}
}
}
private static void createAndShowUI() {
JFrame frame = new JFrame("CheckABunch");
frame.add(new CheckABunch());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
createAndShowUI();
}
});
}
}
Related Topics
How to Schedule a Periodic Task in Java
Get Battery Level and State in Android
Firebase:Recycler View No Adater Attached , Skipping Layout
Problems with Java3D Lib Configuration
Jfilechooser and Browsing Networked MAChines
Concurrentmodificationexception for Arraylist
How to Pass an Array as Arguments to a Method With Variable Arguments in Java
Compound Class Names Are Not Supported Error in Webdriver
Sending Arrays with Intent.Putextra
Using Singleton Design Pattern for SQLitedatabase
Java Is Installed, in Listing, But Execution Produces "./Java: No Such File or Directory"
Environmental Variables Not Seen in a Cron
How to Solve the 'Lock Obtain Timed Out' When Using Solr Plainly
How to Build Netty-Transport-Native-Epoll-4.0.32.Final-Linux-X86_64.Jar