jtabel 遍历_Java获取JTable值(每行)

该博客讨论了如何从JTable中获取选中行的所有数据,而非仅获取单个单元格的值。作者指出,使用`getValueAt`方法只能获取指定行列的单一值。在没有方便的内置方法获取整行的情况下,需要根据表格模型自定义实现。如果表格模型提供获取整行的方法(如`getRow`),则可以通过该方法获取选中行的数据。示例代码展示了如何结合自定义表格模型来实现这一功能。
摘要由CSDN通过智能技术生成

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

}

}

}

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.

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

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 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

UPDATE

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) { ... }

}

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

}

}

}

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值