简体   繁体   English

如何获取JTable中Last单元的值?

[英]How to get value of Last cell in JTable?

I have a query ... code is running fine, but am not able to get value of last cell of last row and last column. 我有一个查询...代码运行良好,但无法获取最后一行和最后一列的最后一个单元格的值。 Below is the code... pls guide 下面是代码...请指导

with this code am adding rows dynamically to JTable : if(e.getSource()==addb) { 这段代码可以将行动态添加到JTable中:if(e.getSource()== addb){

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

    }

Below is the code for getting values from JTable row wise and later on instead of System.out.println() am going to send data to database... 下面是从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));
            }
        }

    }

Something like this : 像这样的东西:

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);
}   

Also see this little example 也请看这个小例子

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();
    }
}

Making a wild guess that you are editing the last cell when you click on the "Submit" button. 当您单击“提交”按钮时,就大肆猜测您正在编辑最后一个单元格。

If so then see: Table Stop Editing . 如果是这样,请参阅: 表停止编辑

Thanks Azad, 谢谢Azad,
Its working superb now... 现在它的工作精湛...
I have a edited the code little bit to get data from all the rows... 我对代码进行了一点编辑,以从所有行中获取数据...

 int i= table.getRowCount()-1;
        int j= table.getColumnCount();

        for (int d=1;d<i+1;d++){   // will provide row wise data
        for(int k = 0 ; k<j ; k++)  // will provide column wise data
        {
          System.out.println("row num   "+d+"   "+model.getValueAt(d,k));
        }   }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM