Example usage for javax.swing ListSelectionModel getSelectionMode

List of usage examples for javax.swing ListSelectionModel getSelectionMode

Introduction

In this page you can find the example usage for javax.swing ListSelectionModel getSelectionMode.

Prototype

int getSelectionMode();

Source Link

Document

Returns the current selection mode.

Usage

From source file:TableIt.java

public TableIt() {
    JFrame f = new JFrame();
    TableModel tm = new MyTableModel();
    final JTable table = new JTable(tm);

    TableColumnModel tcm = table.getColumnModel();
    TableColumn column = tcm.getColumn(tcm.getColumnCount() - 1);
    TableCellRenderer renderer = new MyTableCellRenderer();
    column.setCellRenderer(renderer);// www. ja v  a 2 s  .c  om

    JButton selectionType = new JButton("Next Type");
    selectionType.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            boolean rowSet = table.getRowSelectionAllowed();
            boolean colSet = table.getColumnSelectionAllowed();
            boolean cellSet = table.getCellSelectionEnabled();

            boolean setRow = !rowSet;
            boolean setCol = rowSet ^ colSet;
            boolean setCell = rowSet & colSet;

            table.setCellSelectionEnabled(setCell);
            table.setColumnSelectionAllowed(setCol);
            table.setRowSelectionAllowed(setRow);
            System.out.println("Row Selection Allowed? " + setRow);
            System.out.println("Column Selection Allowed? " + setCol);
            System.out.println("Cell Selection Enabled? " + setCell);
            table.repaint();
        }
    });
    JButton selectionMode = new JButton("Next Mode");
    selectionMode.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            ListSelectionModel lsm = table.getSelectionModel();
            int mode = lsm.getSelectionMode();
            int nextMode;
            String nextModeString;
            if (mode == ListSelectionModel.SINGLE_SELECTION) {
                nextMode = ListSelectionModel.SINGLE_INTERVAL_SELECTION;
                nextModeString = "Single Interval Selection";
            } else if (mode == ListSelectionModel.SINGLE_INTERVAL_SELECTION) {
                nextMode = ListSelectionModel.MULTIPLE_INTERVAL_SELECTION;
                nextModeString = "Multiple Interval Selection";
            } else {
                nextMode = ListSelectionModel.SINGLE_SELECTION;
                nextModeString = "Single Selection";
            }
            lsm.setSelectionMode(nextMode);
            System.out.println("Selection Mode: " + nextModeString);
            table.repaint();
        }
    });
    JPanel jp = new JPanel();
    jp.add(selectionType);
    jp.add(selectionMode);
    JScrollPane jsp = new JScrollPane(table);
    Container c = f.getContentPane();
    c.add(jsp, BorderLayout.CENTER);
    c.add(jp, BorderLayout.SOUTH);
    f.setSize(300, 250);
    f.show();
}