Solved

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

Posted on 1998-07-02
5
398 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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

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…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now