FileDocCategorySizeDatePackage
SelectionExample.javaAPI DocExample4748Mon Nov 09 12:45:50 GMT 1998None

SelectionExample

public class SelectionExample extends JFrame

Fields Summary
Constructors Summary
public SelectionExample()

    super("Selection Model Test");
    setSize(450, 350);
    addWindowListener(new BasicWindowMonitor());

    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();
    JCheckBox cellBox, columnBox, rowBox;
    cellBox = new JCheckBox("Cells", jt.getCellSelectionEnabled());
    cellBox.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent ae) {
        jt.setCellSelectionEnabled(!jt.getCellSelectionEnabled());
      }
    } );

    columnBox = new JCheckBox("Columns", jt.getColumnSelectionAllowed());
    columnBox.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent ae) {
        jt.setColumnSelectionAllowed(!jt.getColumnSelectionAllowed());
      }
    } );

    rowBox = new JCheckBox("Rows", jt.getRowSelectionAllowed());
    rowBox.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent ae) {
        jt.setRowSelectionAllowed(!jt.getRowSelectionAllowed());
      }
    } );

    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);
  
Methods Summary
public static voidmain(java.lang.String[] args)

    SelectionExample se = new SelectionExample();
    se.setVisible(true);