Reputation: 143
I have a JTable
with contents on it which I have read from a CSV file. I am using the following method which when I click on a row it will open up a new JFrame
and close the previous one. It will display things such as ID, co-ordinates, status of what is written on that table and can edit them if wanted. E.G. table below:
|ID |co-ordinates | status |
| 1 | (3,21) | pending |
| 2 | (4,21) | full |
| 3 | (9, 12) | empty |
If I click row 1, it will pop up with frame of the ID(1), co ordinates(3,21) and status in a text field in another frame and is editable. I am able to do the clicking function but am not sure how to take that data onto the next frame when clicking the row.
//in location class
table.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 1) {
int row = table.getSelectedRow();
AddEdit An = new AddEdit(); //goes to next class
An.setVisible(true);
dispose();
}
}
});
How to take that data onto the next frame, when clicking the row?
Upvotes: 0
Views: 64
Reputation: 3109
Without knowing what type of content your JTable
has, I can only offer a generic solution.
int row = table.getSelectedRow();
int userID = (Integer) table.getValueAt(row, 0);
// is co-ordinates [sic] a String or a Point?
// You can do the same as for userID and use (row,1) to get the value
String status = (String) table.getValueAt(row, 2)
With this you can i.e. create an Object[]
and send this to the constructor of AddEdit
or write a method getJTableObject()
or something similar in AddEdit
. This depends on wether you can change AddEdit
or not.
You also should consider Andrews advice and use the cardLayout. With this you could for instance use an ObserverPattern
and send your object around.
Another way is using a JOptionPane
:
Object[] message = { "Please update the information:", newStatusPanel };
int response = JOptionPane.showConfirmDialog(null, message, "Update information",
JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
newStatusPanel
simply is a JPanel
on which you put i.e. JTextFields
. You then fill those fields with the content from the JTable
by the method I have shown earlier and when the user clicks okay, you update the JTable
.
// Do something with the result
if (response == JOptionPane.OK_OPTION) {
model.addRow(new Object[] { txtID.getText(), coordinates.getText(), ... });
This looks like this:
(PS: I will later change the text-based passwords to hash-based. Please ignore this blatant unsecure way of dealing with passwords)
Upvotes: 4