22

Are there any methods that are used to get the data of the selected row? I just want to simply click a specific row with data on it and click a button that will print the data in the Console.

enter image description here

2
  • I tried google but it shows only to getValueAt() mostly. I just narrowed down my question because this JTable is part of our CaseStudy. Commented Mar 30, 2015 at 12:32
  • 1
    try to think not of a whole big solution, instead use small pieces. 1. get the selected row. 2. get the content of the object you gained from the row. Also see the answer from ManyQuestions Commented Mar 30, 2015 at 12:35

5 Answers 5

49

http://docs.oracle.com/javase/7/docs/api/javax/swing/JTable.html

You will find these methods in it:

getValueAt(int row, int column) getSelectedRow() getSelectedColumn() 

Use a mix of these to achieve your result.

Sign up to request clarification or add additional context in comments.

Comments

44

You can use the following code to get the value of the first column of the selected row of your table.

int column = 0; int row = table.getSelectedRow(); String value = table.getModel().getValueAt(row, column).toString(); 

2 Comments

this one is the exact answer. +1
It is not @5377037, as the code fails to convert coordinates from view to model before indexing in the model. It will return incorrect results if the table is sorted. Not just that, the return of table.getSelectedRow() can be -1 if there is no selected row.
13

if you want to get the data in the entire row, you can use this combination below

tableModel.getDataVector().elementAt(jTable.convertRowIndexToModel(jTable.getSelectedRow())); 

Where "tableModel" is the model for the table that can be accessed like so

(DefaultTableModel) jTable.getModel(); 

this will return the entire row data.

I hope this helps somebody

2 Comments

Hi Damilola. Your sample does not convert the row index from a view index to a model index before using it as an index in the data vector (which is a model vector). Your sample will return incorrect results when the table is sorted. This sample also fails to check whether a row is effectively selected.
You need to convert it with JTable method convertRowIndexToModel. Like this: int modelIndex = table.convertRowIndexToModel(selectedRow);
2

Just simple like this:

 tbl.addMouseListener(new MouseListener() { @Override public void mouseReleased(MouseEvent e) { } @Override public void mousePressed(MouseEvent e) { String selectedCellValue = (String) tbl.getValueAt(tbl.getSelectedRow() , tbl.getSelectedColumn()); System.out.println(selectedCellValue); } @Override public void mouseExited(MouseEvent e) { } @Override public void mouseEntered(MouseEvent e) { } @Override public void mouseClicked(MouseEvent e) { } }); 

1 Comment

While this code snippet may solve the problem, it doesn't explain why or how it answers the question. Please include an explanation for your code, as that really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. You can use the edit button to improve this answer to get more votes and reputation!
1

using from ListSelectionModel:

ListSelectionModel cellSelectionModel = table.getSelectionModel(); cellSelectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); cellSelectionModel.addListSelectionListener(new ListSelectionListener() { public void valueChanged(ListSelectionEvent e) { String selectedData = null; int[] selectedRow = table.getSelectedRows(); int[] selectedColumns = table.getSelectedColumns(); for (int i = 0; i < selectedRow.length; i++) { for (int j = 0; j < selectedColumns.length; j++) { selectedData = (String) table.getValueAt(selectedRow[i], selectedColumns[j]); } } System.out.println("Selected: " + selectedData); } }); 

see here.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.