21

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.
    – ZeroCool
    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
    – Zorian
    Mar 30, 2015 at 12:35

5 Answers 5

47

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.

0
43

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
  • 2
    this one is the exact answer. +1
    – user1773603
    Jul 8, 2018 at 20:38
  • 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.
    – TT.
    Nov 5, 2021 at 7:17
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
  • 1
    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.
    – TT.
    Nov 5, 2021 at 7:16
  • 1
    You need to convert it with JTable method convertRowIndexToModel. Like this: int modelIndex = table.convertRowIndexToModel(selectedRow);
    – MadMike
    Dec 17, 2021 at 8:57
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
  • 2
    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! Feb 8, 2021 at 9:04
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.

0

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.