gpt4 book ai didi

java - 如何获取JTable中最后一个单元格的值?

转载 作者:行者123 更新时间:2023-12-01 17:25:46 28 4
gpt4 key购买 nike

我有一个查询...代码运行良好,但无法获取最后一行和最后一列的最后一个单元格的值。下面是代码...请指导

使用此代码将行动态添加到 JTable : if(e.getSource()==addb) {

        model.addRow(new Object[3]);
repaint();

}

下面是从 JTable 逐行获取值的代码,稍后将把数据发送到数据库,而不是 System.out.println()...

if(e.getSource()==submit)
{
int j = table.getRowCount();
for(int row=1;row<j;row++)
{
for(int column=0;column<3;column++)
{
System.out.println("row "+row+" Column is "+column);
System.out.println(model.getValueAt(row, column));
}
}

}

最佳答案

类似这样的事情:

int i= table1.getRowCount()-1;
int j= table1.getColumnCount();
Object [] value = new Object[j];
for(int k = 0 ; k<j ; k++)
{
value[k] = model.getValueAt(i,k);
}

另请参阅这个小示例

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class TableTest extends JFrame implements ActionListener{

JTable table ;
JButton button;
public TableTest(){
String []colNames = {"Subject","lecturer"};
String [][] rowDatas = { {"Java Programming","Jon"},
{"C++ Programming","Nuhara"},
{"Mathematicz","Mike"},
{"Database","Saran"}
};
table = new JTable(rowDatas,colNames);

button = new JButton("Show Last Record");
button.addActionListener(this);

this.add(table);
this.add(button);
this.setVisible(true);
this.setSize(300,200);
this.setDefaultCloseOperation(3);
this.setLayout(new FlowLayout());
}
@Override
public void actionPerformed(ActionEvent e) {
int i= table.getRowCount()-1;
int j= table.getColumnCount();
Object [] value = new Object[j];
for(int k = 0 ; k<j ; k++)
{
//value[k] = table.getValueAt(i,k);
System.out.println(table.getValueAt(i, k));
}
}


public static void main(String...ag){
new TableTest();
}
}

关于java - 如何获取JTable中最后一个单元格的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15042147/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com