How can you retrieve the value of a cell in a JTable in…

In Java, you can retrieve the value of a cell using the getValueAt method of JTable. This method takes two parameters: the index of the row as the first parameter and the index of the column as the second parameter. Here is an example code.

JTable table = new JTable(data, columnNames); // 创建一个JTable,data是表格数据,columnNames是表格列名

int row = 0; // 行索引
int column = 0; // 列索引

Object cellValue = table.getValueAt(row, column); // 获取单元格的值
System.out.println(cellValue); // 打印单元格的值

In the example above, we created a JTable object, set the table’s data and column names. Then, we specified the row and column indexes of the cell to retrieve the value, and used the getValueAt method to get the value of that cell. Finally, we printed out that value.

Please note that indexing starts from 0. So, the index of the first row is 0, and the index of the first column is also 0.

bannerAds