Java JTable getting the data of the selected row
Asked Answered
K

5

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

Kiloliter answered 30/3, 2015 at 12:4 Comment(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.Kiloliter
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 ManyQuestionsHibben
M
48

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.

Maurilia answered 30/3, 2015 at 12:16 Comment(0)
W
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();
Walker answered 16/8, 2016 at 18:4 Comment(2)
this one is the exact answer. +1Leonaleonanie
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.Primordial
C
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

Cakewalk answered 2/6, 2016 at 11:25 Comment(2)
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.Primordial
You need to convert it with JTable method convertRowIndexToModel. Like this: int modelIndex = table.convertRowIndexToModel(selectedRow);Vancouver
H
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) {
        }
    });
Hero answered 8/2, 2021 at 8:27 Comment(1)
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!Fictional
M
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.

Midas answered 8/6, 2019 at 12:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.