Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 1998-07-02
5
Medium Priority
?
524 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

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

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…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This video teaches viewers about errors in exception handling.
Suggested Courses

618 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