Java获取JTable值(每行) [英] Java Getting JTable Value (Per row)

查看:1362
本文介绍了Java获取JTable值(每行)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从Jtable中获取值,我尝试使用getvalueat进行尝试,但是每当我尝试从JTable中获取值时,它仅从所选行的第一列中获取值,我就需要获取我选择的Jtable中的所有值.你能帮我这个吗

I would like to get the value from the Jtable, and I tried it using the getvalueat however whenever I try to get the value from the JTable it only get the value from the first column of the selected row, I need to get all the value from the Jtable which I selected. Can you please help me with this one

here is my code:
class GetTableValue implements ActionListener{
    public void actionPerformed(ActionEvent e){
        AbstractButton button = (AbstractButton)e.getSource();
        if(e.getActionCommand().equals(button.getActionCommand)){
            int row = table.getSelectedRow();
            int col = table.getSelectedColumn();

            Object data = (Object)table.getValueAt(row, col);
            JOptionPane.showMessageDialog(null, data);
        }
    }
}

这是我的动作事件,不幸的是,所选表的值显示在JOptionPane中,但它仅显示一个值(即您已经选择的值),而不显示整个行.

This is my action event where the value of the selected table is shown in the JOptionPane unfortunately it only display one value(which is the one you already selected) not the whole row.

此代码用于我的Jbutton来调用动作事件(我已经从JTable中排除了我的代码,因为它从我的数据库中获取Jtable值)

This code is for my Jbutton for call the action event(I already excluded my code from the JTable since it fetch the Jtable value from my database)

ActionListener tableAction = new GetTableValue();


buttonEdit = new JButton("EDIT");


buttonEdit.addActionListener(tableAction);

代码简单明了,我还向G(google)先生搜索了一个关于获取行的好教程,但是不幸的是,没有一个关于获取Jtable值(每行)的好教程.

the code is plain and simple, I also search Mr. G(google) about a good tutorial on fetching row, unfortunately there isn't a good tutorial for fetching Jtable value(per row).

推荐答案

getValueAt将为您返回单元格的值(以行/列为单位).除非您的表模型支持它,否则没有简单的方法(超出您的工作范围)在单个请求中获取整行.

getValueAt will return you the value of the cell (at row/col). Unless you're table model supports it, there is no convenient way (beyond what you are doing) to get the whole row in a single request.

此外,请记住,如果表已排序或过滤,则模型索引将与视图不匹配,您需要先使用

Also, remember, if the table is sorted or filtered, the model indices will not match the view, you need to convert them first, using convertRowIndexToModel and convertColumnIndexToModel

更新

唯一的解决方法是使用的表模型具有getRow(或等效方法).不知道如何将数据存储在表模型中,几乎不可能给出准确的答案,但是一般的想法是...

The only way around it is if the table model you're using has a getRow (or equivalent) method. Without know how you are storing the data in the table model it's next to near impossible to give an accurate answer, but a general idea would be...

public class MyAwesomeTableModel extends AbstractTableModel {
    // All the usual stuff...

    public MyRowData getRowAt(int index) { ... }
}

现在,MyRowData是您创建的表数据的任何实现. (最好是)单个Object,或者在DefaultTableModel对象数组的情况下.

Now, MyRowData is what ever implementation of the table data you've created. It could be (preferably) a single Object or in the case of the DefaultTableModel an array of objects.

class GetTableValue implements ActionListener{
    public void actionPerformed(ActionEvent e){
        AbstractButton button = (AbstractButton)e.getSource();
        if(e.getActionCommand().equals(button.getActionCommand)){

            int row = table.convertRowIndexToModel(table.getSelectedRow());
            MyAwesomeTableModel model = (MyAwesomeTableModel)table.getModel();

            MyRowData data = model.getRowAt(row);
            JOptionPane.showMessageDialog(null, data);
        }
    }
}

这一切都取决于您实现TableModel的方式以及实现行数据的方式,但这就是一般的原则

This is all dependent on how you've implemented your TableModel and how you've implemented your row data, but that's the general jist

这篇关于Java获取JTable值(每行)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆