Solved

Accessing data of a selected row in a JTable - urgent!

Posted on 1998-07-02
5
420 Views
Last Modified: 2011-10-03
How can I read out the data of a selected row in a JTable?

Usually you want to do something with the data of a selected row. For instance, if you have a table which shows the name of a person, his age, his address and you select say the third entry/row in the table because you want to change the address. How can I do this?
0
Comment
Question by:dirku
  • 4
5 Comments
 
LVL 5

Accepted Solution

by:
fontaine earned 30 total points
ID: 1224396
Get the TableModel used to build up the JTable using JTable.getModel(). The table model
allows you to manipulate the rows and columns of the table. There are methods such as
setValueAt() and getValueAt(). Please refer to the doc of com.sun.java.swing.table.TableModel.java for the full API.

You can identify the row that has been selected via JTable.getSelectedRow(). This returns the index of the last row selected or added to the selection.

Ex:

JTable table = ...
TableModel model = table.getModel();
int row = table.getSelectedRow();
model.setValueAt("Hello!",row,0 /* first column */);

You will maybe have to invalidate()/validate() to see the changes appear on screen.
0
 
LVL 5

Expert Comment

by:fontaine
ID: 1224397
To read the content of the first column of the selected row:

String data = (String)model.getValueAt(row,0);
0
 
LVL 5

Expert Comment

by:fontaine
ID: 1224398
If you want to use setValueAt(), the cell has to be editable, i.e. the table model isCellEditable()
method has to return true for that particular cell.
0
 

Author Comment

by:dirku
ID: 1224399
...and how can I deselect a selected row after clicking on the selected row again (or anywhere else but not on a table's row)?

Will grade you after this answer, okay?
For now I'd give you an A (although I found out what you suggested by myself -> I simply asked to quick. However, good answer.)
0
 
LVL 5

Expert Comment

by:fontaine
ID: 1224400
There is a clearSelection() method in JTable that unselects what is currently selected. You
can invoke this method as a menu call-back for example, or in the mouseClicked() method
of a panel implementing the MouseListener interface.
0

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
maven project error 5 55
fibonacci ten numbers 4 38
How to convert String matching to regex in java 4 33
JAVA API design with micro service cloud in mind 1 16
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

772 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