[英]Java JTable getting the data of the selected row
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.
我只想简单地单击带有数据的特定行,然后单击将在控制台中打印数据的按钮。
http://docs.oracle.com/javase/7/docs/api/javax/swing/JTable.html 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();
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其中“tableModel”是可以像这样访问的表的模型
(DefaultTableModel) jTable.getModel();
this will return the entire row data.这将返回整个行数据。
I hope this helps somebody我希望这可以帮助某人
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) {
}
});
using from ListSelectionModel
:使用
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);
}
});
To get the table entire row data, add the below addTableModelListener for JTable.要获取表的整行数据,请为 JTable 添加以下 addTableModelListener。
table.getModel().addTableModelListener(new TableModelListener() {
@Override
public void tableChanged(TableModelEvent e) {
for (int i = 0; i < table.getModel().getRowCount(); i++) {
if (table.getSelectedRow() != -1) {
int column = 1;
int row = table.getSelectedRow();
System.out.println("to get one cell Data:\t"
+ table.getModel().getValueAt(row, column).toString());
System.out.println("To get entire row data:\t"
+ model.getDataVector().elementAt(table.getSelectedRow()));
break;
}
}
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.