Jerri Kangasniemi
Jerri Kangasniemi

Reputation: 683

disable multi-column selection

Is there any way to disable selection of multiple columns for a Swing JTable? I've disabled selection all together in the "Tid" column by overriding the selection intervals of the selection model:

myTable.getColumnModel().setSelectionModel(new DefaultListSelectionModel() {
            private boolean isSelectable(int index0, int index1) {
                return index1 != 0;
            }

            @Override
            public void setSelectionInterval(int index0, int index1) {
                if(isSelectable(index0, index1)) {
                    super.setSelectionInterval(index0, index1);
                }
            }

            @Override
            public void addSelectionInterval(int index0, int index1) {
                if(isSelectable(index0, index1)) {
                    super.addSelectionInterval(index0, index1);
                }
            }
        });

And my guess is that one can also disallow the selection of multiple columns by overriding methods in the selection model. But I can't really figure out how to accomplish that.

Allowed selection Allowed Selection - the selection spans only one column but multiple rows

Disallowed selection Disallowed Selection - the selection spans multiple columns and multiple rows

Upvotes: 2

Views: 3479

Answers (2)

MadProgrammer
MadProgrammer

Reputation: 347332

First get the TableColumnModel from the JTable

TableColumnModel columnModel = table.getColumnModel();

Next, get the LstSeletionModel for the TableColumnModel

ListSelectionModel selectionModel = columnModel.getSelectionModel();

With this, you could set the selectionMode that the model will use, for example

selectionModel.setSelectionModel(ListSelectionModel.SINGLE_SELECTION)

See the JavaDocs for ListSelectionModel and TableColumnModel for more details

Runnable example....

TableSelection

    import java.awt.BorderLayout;
    import java.awt.EventQueue;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
import javax.swing.JScrollPane;
    import javax.swing.JTable;
import javax.swing.ListSelectionModel;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;

    public class Test {

        public static void main(String[] args) {
            new Test();
        }

        public Test() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                        ex.printStackTrace();
                    }

                    JFrame frame = new JFrame("Testing");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.add(new TestPane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }

        public class TestPane extends JPanel {

            public TestPane() {
                setLayout(new BorderLayout());

                DefaultTableModel model = new DefaultTableModel(0, 10);
                for (int row = 0; row < 10; row++) {
                    String[] data = new String[10];
                    for (int col = 0; col < 10; col++) {
                        data[col] = row + "x" + col;
                    }
                    model.addRow(data);
                }


                JTable table = new JTable(model);
                table.setColumnSelectionAllowed(true);
                table.setRowSelectionAllowed(true);
                table.getColumnModel().getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
                add(new JScrollPane(table));
            }
        }

    }

Upvotes: 5

Jerri Kangasniemi
Jerri Kangasniemi

Reputation: 683

Actually, it was a simple enough addition to my already existing overrides that was needed.

@Override
public void setSelectionInterval(int index0, int index1) {
    if (isSelectable(index0, index1)) {
        if (index0==index1) { //The if condition needed.
            super.setSelectionInterval(index0, index1);             
        }
    }
}

I realised upon reviewing the JavaDoc and the DefaultListSelectionModel that the index0 and index1 were just what I was looking for - the column span. So by doing the call to the superclass if and only if the two column indices are equal, selection of multiple columns is not possible.

Upvotes: 0

Related Questions