The Java Developers Almanac 1.4


Order this book from Amazon.

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

e940. Enabling Single or Multiple Selections in a JTable Component

By default, a table component allows multiple selections. To control the selection behavior, see e939 Enabling Row, Column, or Cell Selections in a JTable Component.
    JTable table = new JTable();
    
    // Get default selection mode
    int selMode = table.getSelectionModel().getSelectionMode();
    // MULTIPLE_INTERVAL_SELECTION
    
    // Allow only single a selection
    table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    
    // Allow selection to span one contiguous set of rows,
    // visible columns, or block of cells
    table.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
    
    // Allow multiple selections of rows, visible columns, or cell blocks (default)
    table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

 Related Examples
e939. Enabling Row, Column, or Cell Selections in a JTable Component
e941. Programmatically Making 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.