The Java Developers Almanac 1.4


Order this book from Amazon.

   
Home > List of Packages > javax.swing.table  [62 examples] > Selection  [6 examples]

e941. Programmatically Making Selections in a JTable Component

To select columns, setColumnSelectionInterval(), addColumnSelectionInterval(), and removeColumnSelectionInterval() are available. However, these only work if columnSelectionAllowed is true and rowSelectionAllowed is false. This also applies to rows.

To select individual cells or blocks of cells, use changeSelection(). However, both columnSelectionAllowed and rowSelectionAllowed must be false.

These selection methods observe the setting of the selection mode. For example, if the selection mode is SINGLE_SELECTION, only a single row, column, or cell can be made. See e940 Enabling Single or Multiple Selections in a JTable Component for more information on selection modes.

    int rows = 10;
    int cols = 5;
    JTable table = new JTable(rows, cols);
    
    // Use this mode to demonstrate the following examples
    table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
    
    // The following column selection methods work only if these
    // properties are set this way
    table.setColumnSelectionAllowed(true);
    table.setRowSelectionAllowed(false);
    
    // Select a column - column 0
    table.setColumnSelectionInterval(0, 0);
    
    // Select an additional range of columns - columns 1 to 2
    table.addColumnSelectionInterval(1, 2);
    
    // Deselect a range of columns - columns 0 to 1
    table.removeColumnSelectionInterval(0, 1);
    
    
    // The following row selection methods work only if these
    // properties are set this way
    table.setColumnSelectionAllowed(false);
    table.setRowSelectionAllowed(true);
    
    // Select a row - row 0
    table.setRowSelectionInterval(0, 0);
    
    // Select an additional range of rows - rows 1 to 2
    table.addRowSelectionInterval(1, 2);
    
    // Deselect a range of rows - rows 0 to 1
    table.removeRowSelectionInterval(0, 1);
    
    
    // The following cell selection methods work only if these
    // properties are set this way
    table.setColumnSelectionAllowed(true);
    table.setRowSelectionAllowed(true);
    
    // Select a cell: cell (2,1)
    int row = 2;
    int col = 1;
    boolean toggle = false;
    boolean extend = false;
    table.changeSelection(row, col, toggle, extend);
    
    // Extend the selection to include all cells between (2,1) to (5,3)
    row = 5;
    col = 3;
    toggle = false;
    extend = true;
    table.changeSelection(row, col, toggle, extend);
    
    // Deselect a cell: cell (3,2)
    // All cells in the row and column containing (3,2) are deselected.
    row = 3;
    col = 2;
    toggle = true;
    extend = false;
    table.changeSelection(row, col, toggle, extend);
    
    // This method actually toggles the selection state so that
    // if it were called again, it exactly reverses the first call.
    // Select cell (3,2) as well as the other cells that
    // were deselected in the first call.
    toggle = true;
    extend = false;
    table.changeSelection(row, col, toggle, extend);
    
    // Select all cells
    table.selectAll();
    
    // Deselect all cells
    table.clearSelection();

 Related Examples
e939. Enabling Row, Column, or Cell Selections in a JTable Component
e940. Enabling Single or Multiple Selections in a JTable Component
e942. Getting the Selected Cells in a JTable Component
e943. Disabling Selections in a JTable Component
e944. Getting the Anchor Cell in a JTable Component

See also: Cells    Column Heads    Columns    Editing    Events    Layout    Rows    Scrolling    Sorting    Table Model    Tool Tips   


© 2002 Addison-Wesley.