• Post Reply Bookmark Topic Watch Topic
  • New Topic

Passing values between two JPanels

 
Tai Yo
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have 2 jPanels: Data1 and Data2. These jPanels contains jTextFields and jComboBoxes. The purpose of program to input data to jTextFields and save it in text file.
Every jPanel have a saveButton. When the saveButton1 of Data1 jPanel is hit it will save in Data1.txt file.
When saveButton2 of Data2 is pressed it will save to Data2.txt file.
Fot this moment, I can get data from jTextFields and save them to Data1.txt and Data2.txt files.
The contents of Data1.txt file is: Name, Affiliation, Phone;
Data2.txt file is: Birth, Address, Title.

The question is: how can I get inputted value of Data2 jPanel and save it to Data1.txt file?
For example, when the saveButton2 of Data2 is pressed, then the value of "Address" must appear in Data1.txt file after "Affiliation", i.e. Data1.txt->Name, Affiliation, Address, Phone.

Thank you for help.
 
Campbell Ritchie
Marshal
Posts: 52581
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can your app save information passed via the command line? You ought to design the app like that, rather than having logic in the display classes. You might have getText methods for each of the text fields which can be used to provide the data to the actual app.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In pseudo code
1) Create 2 panels with required controls
2) Add the panels to the JFrame
3) Add the Save button to the JFRAME
4) When the save button is clicked, it can access the data from inside the JPanels
5) Save data as required.

This way, the panels can exist without being aware of the data saving or the parent JFrame. They just display given data or accept user input.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!