Reputation: 171
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).
Upvotes: 2
Views: 66339
Reputation: 4153
If you want all the values from selected row then try this code.
int row = jTable1.getSelectedRow();
int column = jTable1.getColumnCount();
for(int i = 0; i < column; i++) {
System.out.println(jTable1.getValueAt(row, i));
}
You get the all values for selected row, no matter how much columns are there in jtable
If you want all the values from jtable then try:
int row = jTable1.getRowCount();
int column = jTable1.getColumnCount();
for (int j = 0; j < row; j++) {
for (int i = 0; i < column; i++) {
System.out.println(jTable1.getValueAt(j, i));
}
}
Yes you can use Object[]
to store the values. For example:
Object[] val = new Object[column];
for (int k = 0; k < val.length - 1; k++) {
for (int j = 0; j < row; j++) {
for (int i = 0; i < column; i++) {
val[k] = jTable1.getValueAt(j, i);
System.out.println(val[k]);
}
}
}
Upvotes: 12
Reputation: 347184
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
Upvotes: 7
Reputation: 1860
private void jTable1MousePressed(java.awt.event.MouseEvent evt) {
int selectedRow;
ListSelectionModel rowSM = jTable1.getSelectionModel();
rowSM.addListSelectionListener(new ListSelectionListener()
{
@Override
public void valueChanged(ListSelectionEvent e)
{
ListSelectionModel lsm = (ListSelectionModel) e.getSource();
selectedRow = lsm.getMinSelectionIndex();
int numCols = jTable1.getColumnCount();
model = (DefaultTableModel) jTable1.getModel();
System.out.print(" \n row " + selectedRow + ":");
for (int j = 0; j < numCols; j++)
{
System.out.print(" " + model.getValueAt(selectedRow, j));
}
}
});
}
Using this you can get value of whole row where u click on particular row.
Upvotes: 2
Reputation: 4151
you can try the below code to get selected row value:
int selectedRow = jTableName.getSelectedRow();
selectedRow = jTableName.convertRowIndexToModel(selectedRow);
String val1 = (String)jTableName.getModel().getValueAt(selectedRow, 0);
String val2 = (String)jTableName.getModel().getValueAt(selectedRow, 1);
Upvotes: 1