JTable: getColumnSelectionAllowed() : JTable « javax.swing « Java by API






JTable: getColumnSelectionAllowed()

 
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;

public class MainClass extends JFrame {

  public MainClass() {
    super("Selection Model Test");
    setSize(450, 350);
    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);
  }

  public static void main(String args[]) {
    MainClass se = new MainClass();
    se.setVisible(true);
  }

  public class SelectionDebugger implements ListSelectionListener {
    JLabel debugger;

    ListSelectionModel model;

    public SelectionDebugger(JLabel target, ListSelectionModel lsm) {
      debugger = target;
      model = lsm;
    }

    public void valueChanged(ListSelectionEvent lse) {
      if (!lse.getValueIsAdjusting()) {
        StringBuffer buf = new StringBuffer();
        int[] selection = getSelectedIndices(model.getMinSelectionIndex(), model
            .getMaxSelectionIndex());
        if (selection.length == 0) {
          buf.append("none");
        } else {
          for (int i = 0; i < selection.length - 1; i++) {
            buf.append(selection[i]);
            buf.append(", ");
          }
          buf.append(selection[selection.length - 1]);
        }
        debugger.setText(buf.toString());
      }
    }

    protected int[] getSelectedIndices(int start, int stop) {
      if ((start == -1) || (stop == -1)) {
        return new int[0];
      }

      int guesses[] = new int[stop - start + 1];
      int index = 0;
      for (int i = start; i <= stop; i++) {
        if (model.isSelectedIndex(i)) {
          guesses[index++] = i;
        }
      }

      int realthing[] = new int[index];
      System.arraycopy(guesses, 0, realthing, 0, index);
      return realthing;
    }
  }
}

           
         
  








Related examples in the same category

1.TableHeader.cellBorder
2.Table.scrollPaneBorder
3.JTable.AUTO_RESIZE_ALL_COLUMNS
4.JTable.AUTO_RESIZE_LAST_COLUMN
5.JTable.AUTO_RESIZE_NEXT_COLUMN
6.JTable.AUTO_RESIZE_OFF
7.JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS
8.new JTable(Object name, Object[][] data)
9.new JTable(TableModel dm)
10.new JTable(Vector rowData, Vector columnNames)
11.JTable: addColumnSelectionInterval(int index0, int index1)
12.JTable: changeSelection(int rowIndex, int columnIndex, boolean toggle, boolean extend)
13.JTable: clearSelection()
14.JTable: configureEnclosingScrollPane() 'Headerless Table'
15.JTable: createDefaultColumnsFromModel()
16.JTable: getCellSelectionEnabled()
17.JTable: getColumnCount()
18.JTable: getColumnModel()
19.JTable: getIntercellSpacing()
20.JTable: getRowCount()
21.JTable: getRowSelectionAllowed()
22.JTable: getSelectedColumns()
23.JTable: getSelectedRows()
24.JTable: getSelectionModel()
25.JTable: getTableHeader()
26.JTable: getValueAt(int row, int column)
27.JTable: isCellSelected(int row, int column)
28.JTable: moveColumn(int column, int targetColumn)
29.JTable: print()
30.JTable: print(PrintMode p, MessageFormat hFormat, MessageFormat fFormat)
31.JTable: removeColumn(TableColumn aColumn)
32.JTable: removeColumnSelectionInterval(int index0, int index1)
33.JTable: removeRowSelectionInterval(int index0, int index1)
34.JTable: selectAll()
35.JTable: setAutoCreateColumnsFromModel(boolean autoCreateColumnsFromModel)
36.JTable: setAutoResizeMode(int m)
37.JTable: setCellSelectionEnabled(boolean cellSelectionEnabled)
38.JTable: setColumnSelectionAllowed(boolean columnSelectionAllowed)
39.JTable: setDefaultEditor(Class columnClass, TableCellEditor editor)
40.JTable: setDefaultRenderer(Class columnClass, TableCellRenderer renderer)
41.JTable: setFocusable(boolean focusable)
42.JTable: setGridColor(Color gridColor)
43.JTable: setIntercellSpacing(Dimension intercellSpacing)
44.JTable: setPreferredScrollableViewportSize(Dimension size)
45.JTable: setRowHeight(int rowHeight)
46.JTable: setRowSelectionAllowed(boolean rowSelectionAllowed)
47.JTable: setRowSelectionInterval(int index0, int index1)
48.JTable: setRowSorter(RowSorter arg0)
49.JTable: setSelectionMode(int m)
50.JTable: setShowGrid(boolean showGrid)
51.JTable: setShowHorizontalLines(boolean showHorizontalLines)
52.JTable: setTableHeader(JTableHeader tableHeader)
53.JTable: setShowVerticalLines(boolean showVerticalLines)
54.JTable: setValueAt(Object aValue, int row, int column)