Reputation: 643
I have one tab that displays the list of users in a JTable that I have extracted from a database and another tab that displays JTextFields that want to edit the user details then save them to the database.
One of the unique feature of a user is a userID(Integer) that am able to get from the JTable depending on which row is selected.
Now I want use that userID and fill the JTextFields with data from the database to allow editing and hence save the new values below is the UI( can't post images)
Basically is how to share the Integer variable between the two tabs
Upvotes: 0
Views: 838
Reputation: 109813
create JFrame with JTable (in JScrollPane)
add JPopup with JMenuItems to the JTable
override ListSelectionListener.SINGLE... to avoiding multiply selections
create JDialog with JTextComponents
create JDialog
only once time, reuse that for another event from JPopup
,
override JDialog#setDefaultCloseOperation to HIDE_ON_CLOSE, then you'll play only with setVisible(true/false)
get data from selected row and fill required JTextComponents
in the JDialog
, then call JDialog#setVisible(true)
wrapped in invokeLater()
put there JButtons
(cancel and save) with JDBC
statement UPDATE ....
, put there boolean
variable that representing success from JDBC,
on success to refresh XxxTableModel
, then to hide JDialog
all updates (JTable
, XxxTableModel
)to the Swing GUI must be done on EDT
Upvotes: 3
Reputation: 9317
You need to have your own mechanism for communication between the tabs. For instance you can have a reference of second tab in first tab so that when row is selected in table you can call specific method in second component. If you want to decouple them you can add a listener interface which second component implements and adds itself as listener to first one and so on.
Upvotes: 1