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.
Java JTable getting the data of the selected row
Asked Answered
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 ManyQuestions –
Hibben
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.
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();
this one is the exact answer. +1 –
Leonaleonanie
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 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
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 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) {
}
});
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
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);
}
});
© 2022 - 2024 — McMap. All rights reserved.