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).
getValueAtwill 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
convertRowIndexToModelandconvertColumnIndexToModelUPDATE
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…Now,
MyRowDatais what ever implementation of the table data you’ve created. It could be (preferably) a singleObjector in the case of theDefaultTableModelan array of objects.This is all dependent on how you’ve implemented your
TableModeland how you’ve implemented your row data, but that’s the general jist