?
Solved

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

Posted on 1998-07-02
5
Medium Priority
?
472 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
[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
  • 4
5 Comments
 
LVL 5

Accepted Solution

by:
fontaine earned 120 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
In this post we will learn different types of Android Layout and some basics of an Android App.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
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.
Suggested Courses
Course of the Month12 days, 19 hours left to enroll

777 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