Reputation: 63
I'd like to get my values in an array. Later I'd like to use the array in another class. How can this be done?
table.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 1) {
JTable target = (JTable) e.getSource();
int row = target.getSelectedRow();
int column = target.getSelectedColumn();
row = table.convertRowIndexToModel(row);
String val1 = (String) table.getModel().getValueAt(row, 0);
String val2 = (String) table.getModel().getValueAt(row, 1);
String val3 = (String) table.getModel().getValueAt(row, 2);
String val4 = (String) table.getModel().getValueAt(row, 3);
String val5 = (String) table.getModel().getValueAt(row, 4);
System.out.println(val1 + " " + val2 + " " + val3 + " " + val4 + " " + val5);
}
}
});
Upvotes: 1
Views: 60
Reputation: 1682
Another way, with collections, they normally easier to work with:
List<String> values = new ArrayList<>();
values.add((String) table.getModel().getValueAt(row, 0))
..
..
values.add((String) table.getModel().getValueAt(row, 4););
Upvotes: 1
Reputation: 13910
Instantiate a new array with the size of the values you are going to add. If you do not know the size you should look into using ArrayList
String[] arr = new String[5];
Add values to the array. You could probably put this in a loop so the code will be less verbose.
String val1 = (String) table.getModel().getValueAt(row, 0);
arr[0] = val1;
String val2 = (String) table.getModel().getValueAt(row, 1);
arr[1] = val2;
...
then return the array
return arr;
Upvotes: 1