Example usage for javax.swing JTable getRowSelectionAllowed

List of usage examples for javax.swing JTable getRowSelectionAllowed

Introduction

In this page you can find the example usage for javax.swing JTable getRowSelectionAllowed.

Prototype

public boolean getRowSelectionAllowed() 

Source Link

Document

Returns true if rows can be selected.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    JTable table = new JTable();

    if (table.getColumnSelectionAllowed() && !table.getRowSelectionAllowed()) {

        int[] vColIndices = table.getSelectedColumns();
    }/*from w  w  w. j  av  a2s . c om*/
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    JTable table = new JTable();

    if (!table.getColumnSelectionAllowed() && table.getRowSelectionAllowed()) {
        // //from   w w  w.  j  a  v a2 s .  c om
        int[] rowIndices = table.getSelectedRows();
    }
}

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);/*from  www.  j  a  v  a 2 s .  co m*/

    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();
}

From source file:MainClass.java

public MainClass() {
    super("Selection Model Test");
    setSize(450, 350);/* w  ww  .  j  a v a 2  s  . c  o  m*/
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    TableModel tm = new AbstractTableModel() {
        public int getRowCount() {
            return 10;
        }

        public int getColumnCount() {
            return 10;
        }

        public Object getValueAt(int r, int c) {
            return "0";
        }
    };

    final JTable jt = new JTable(tm);

    JScrollPane jsp = new JScrollPane(jt);
    getContentPane().add(jsp, BorderLayout.CENTER);

    JPanel controlPanel, buttonPanel, columnPanel, rowPanel;

    buttonPanel = new JPanel();
    final JCheckBox cellBox, columnBox, rowBox;
    cellBox = new JCheckBox("Cells", jt.getCellSelectionEnabled());
    columnBox = new JCheckBox("Columns", jt.getColumnSelectionAllowed());
    rowBox = new JCheckBox("Rows", jt.getRowSelectionAllowed());

    cellBox.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            jt.setCellSelectionEnabled(cellBox.isSelected());
            columnBox.setSelected(jt.getColumnSelectionAllowed());
            rowBox.setSelected(jt.getRowSelectionAllowed());
        }
    });

    columnBox.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            jt.setColumnSelectionAllowed(columnBox.isSelected());
            cellBox.setSelected(jt.getCellSelectionEnabled());
        }
    });

    rowBox.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            jt.setRowSelectionAllowed(rowBox.isSelected());
            cellBox.setSelected(jt.getCellSelectionEnabled());
        }
    });

    buttonPanel.add(new JLabel("Selections allowed:"));
    buttonPanel.add(cellBox);
    buttonPanel.add(columnBox);
    buttonPanel.add(rowBox);

    columnPanel = new JPanel();
    ListSelectionModel csm = jt.getColumnModel().getSelectionModel();
    JLabel columnCounter = new JLabel("Selected Column Indices:");
    csm.addListSelectionListener(new SelectionDebugger(columnCounter, csm));
    columnPanel.add(new JLabel("Selected columns:"));
    columnPanel.add(columnCounter);

    rowPanel = new JPanel();
    ListSelectionModel rsm = jt.getSelectionModel();
    JLabel rowCounter = new JLabel("Selected Row Indices:");
    rsm.addListSelectionListener(new SelectionDebugger(rowCounter, rsm));
    rowPanel.add(new JLabel("Selected rows:"));
    rowPanel.add(rowCounter);

    controlPanel = new JPanel(new GridLayout(0, 1));
    controlPanel.add(buttonPanel);
    controlPanel.add(columnPanel);
    controlPanel.add(rowPanel);

    getContentPane().add(controlPanel, BorderLayout.SOUTH);
}

From source file:SelectionExample.java

public SelectionExample() {
    super("Selection Model Test");
    setSize(450, 350);//from  w  w  w.j a va  2s.  co  m
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    TableModel tm = new AbstractTableModel() {
        // We'll create a simple multiplication table to serve as a
        // noneditable
        // table with several rows and columns
        public int getRowCount() {
            return 10;
        }

        public int getColumnCount() {
            return 10;
        }

        public Object getValueAt(int r, int c) {
            return "" + (r + 1) * (c + 1);
        }
    };

    final JTable jt = new JTable(tm);

    JScrollPane jsp = new JScrollPane(jt);
    getContentPane().add(jsp, BorderLayout.CENTER);

    // Now set up our selection controls
    JPanel controlPanel, buttonPanel, columnPanel, rowPanel;

    buttonPanel = new JPanel();
    final JCheckBox cellBox, columnBox, rowBox;
    cellBox = new JCheckBox("Cells", jt.getCellSelectionEnabled());
    columnBox = new JCheckBox("Columns", jt.getColumnSelectionAllowed());
    rowBox = new JCheckBox("Rows", jt.getRowSelectionAllowed());
    cellBox.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            jt.setCellSelectionEnabled(cellBox.isSelected());
            columnBox.setSelected(jt.getColumnSelectionAllowed());
            rowBox.setSelected(jt.getRowSelectionAllowed());
        }
    });

    columnBox.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            jt.setColumnSelectionAllowed(columnBox.isSelected());
            cellBox.setSelected(jt.getCellSelectionEnabled());
        }
    });

    rowBox.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            jt.setRowSelectionAllowed(rowBox.isSelected());
            cellBox.setSelected(jt.getCellSelectionEnabled());
        }
    });

    buttonPanel.add(new JLabel("Selections allowed:"));
    buttonPanel.add(cellBox);
    buttonPanel.add(columnBox);
    buttonPanel.add(rowBox);

    columnPanel = new JPanel();
    ListSelectionModel csm = jt.getColumnModel().getSelectionModel();
    JLabel columnCounter = new JLabel("(Selected Column Indices Go Here)");
    csm.addListSelectionListener(new SelectionDebugger(columnCounter, csm));
    columnPanel.add(new JLabel("Selected columns:"));
    columnPanel.add(columnCounter);

    rowPanel = new JPanel();
    ListSelectionModel rsm = jt.getSelectionModel();
    JLabel rowCounter = new JLabel("(Selected Row Indices Go Here)");
    rsm.addListSelectionListener(new SelectionDebugger(rowCounter, rsm));
    rowPanel.add(new JLabel("Selected rows:"));
    rowPanel.add(rowCounter);

    controlPanel = new JPanel(new GridLayout(0, 1));
    controlPanel.add(buttonPanel);
    controlPanel.add(columnPanel);
    controlPanel.add(rowPanel);

    getContentPane().add(controlPanel, BorderLayout.SOUTH);
}