Question

Is there an easy way to limit the total selection of rows in a JTable? I mean I'd like to allow the user to use shift and ctrl to select a maximum of X rows. If he clicks again on a row it would cancel out all the selections. Sort of how it currently behaves, but while limiting the total amount of selected rows.

Here's my current implementation, I'm kind of clueless on how to limit the selection graphically.

public class RoomsListView extends AbstractViewPanel {

public RoomsListView(DefaultController controller)
{
    this.controller = controller;
    initUI();
}

private void initUI() 
{
    tableT = new JTable(RoomsModel.getInstance());
    sorter = new TableRowSorter<RoomsModel>(RoomsModel.getInstance());
    tableT.setRowSorter(sorter);

    tableT.setPreferredScrollableViewportSize(new Dimension(CUSTOM_TABLE_WIDTH, CUSTOM_TABLE_HEIGHT));

    tableT.addMouseListener(new MouseAdapter() {
        public void mouseClicked(MouseEvent e) {
            rowClickedPerformed(e);
        }
    });
}

private void rowClickedPerformed(MouseEvent e) 
{

}

public void modelPropertyChange(PropertyChangeEvent evt) 
{
}

public JTable getTable()
{
    return tableT;
}

private final int CUSTOM_TABLE_WIDTH = -1;
private final int CUSTOM_TABLE_HEIGHT = 150;

private JTable tableT;
private DefaultController controller;
private TableRowSorter<RoomsModel> sorter;

}

Was it helpful?

Solution

For your case

.addMouseListener(new MouseAdapter() {
   public void mouseClicked(MouseEvent e) {
     int row = m_table.getSelectedRow();
     int col = m_table.getSelectedColumn();
  }
});

Just check last - first < X

or like this

SelectionListener listener = new SelectionListener(table);
table.getSelectionModel().addListSelectionListener(listener);
table.getColumnModel().getSelectionModel()
    .addListSelectionListener(listener);

public class SelectionListener implements ListSelectionListener {
    JTable table;

    // It is necessary to keep the table since it is not possible
    // to determine the table from the event's source
    SelectionListener(JTable table) {
        this.table = table;
    }
    public void valueChanged(ListSelectionEvent e) {
        // If cell selection is enabled, both row and column change events are fired
        if (e.getSource() == table.getSelectionModel()
              && table.getRowSelectionAllowed()) {
            // Column selection changed
            int first = e.getFirstIndex();
            int last = e.getLastIndex();
        } else if (e.getSource() == table.getColumnModel().getSelectionModel()
               && table.getColumnSelectionAllowed() ){
            // Row selection changed
            int first = e.getFirstIndex();
            int last = e.getLastIndex();
        }

        if (e.getValueIsAdjusting()) {
            // The mouse button has not yet been released
        }
    }
}

OTHER TIPS

This may help to row selection limit to one selection

table.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top