[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

How to select a row in a JTable?

Posted on 2007-03-29
3
Medium Priority
?
1,030 Views
Last Modified: 2008-01-09
Hello

I(d like to know How to select a row in a JTable?
is it possible? if yes, can I have an exemple?

thanks

regards
0
Comment
Question by:pofpof98
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 24

Accepted Solution

by:
sciuriware earned 1800 total points
ID: 18814737
From JGuru:
In the Model-View-Controller (MVC) world of Swing, the JTable is just a display mechanism. Instead of selecting rows in the table (view), you select rows in the selection model:

ListSelectionModel selectionModel =
  table.getSelectionModel();
selectionModel.setSelectionInterval(start, end);

You can also select multiple blocks of non-contiguous rows, using the addSelectionInterval() method.
The first row selection should be done with setRowSelectionInterval(i,j)

The subsequent row selections (in the same JTable) should be done with addRowSelectionInterval(i,j)

(Columns are selected similarly.)

;JOOP!
0
 
LVL 24

Assisted Solution

by:sciuriware
sciuriware earned 1800 total points
ID: 18814742
From Java Almanac:

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

;JOOP!
0
 
LVL 30

Assisted Solution

by:Mayank S
Mayank S earned 200 total points
ID: 18818912
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question