How to pass variables from one JFrame to multiple JFrame in Java? - java

I have been trying to do this a while back but failed to find a way.
My Java program contains four JFrames. JFrame1 contains a table and a button that on clicking opens JFrame2. In JFrame2 there is a table and two buttons that on clicking opens JFrame3 and JFrame4 which contain a table and Save button each.
Now what I want to do is that when a user enters data in JFrame3 and clicks the save button the data is saved in the table of JFrame2 and JFrame1 repectively. Same thing for JFrame4 but the data should be appended not deleted.
The data that is saved in the JFrame1 from JFrame3 and JFrame4 is to be used to insert into the database.
These are my requirements. Can anyone help me out with this problem? Thanks in advance.

What you are doing is usually achieved through two approaches:
You can use the Card Layout to avoid creating a new JFrame each time, but rather have one JFrame and replace its components. This would allow you to have one class which stores this data and then the logic to show it on your different screens.
If (1) does not do it for you, the other option would be to pass on a reference of the parent frame to the child. Thus, when you create an instance of JFrame2, you also pass it along an instance of JFrame1. Each JFrame class will have methods which are responsible for accepting information and displaying it according to your requirements.
Option 1 is usually the most recommended.

You can use parameterized constructor or public variables or even public methods to pass values from 1 JFrame to another also using public static variables(not preferred) will be okay.

Related

Displaying a JOption Pane from a functions class

I have two classes one that I have in my program. One I have drawn my Graphical user interface and another that has methods to be executed by buttons in GUI class. Basically it's a program to add records to a database. When I click button register it calls the functions class to execute add to database method. In the add to database method I need to add a jOption pane to display successful or not.. How do I ho about it .
I have tried
JOptionPane.showMessageDialog(this,"successful"); the code is not working since it is not in the GUI class
the code is not working since it is not in the GUI class
You can pass the "frame" as a parameter to your "functions" class.
Then the "functions" class can use that value as the "frame" for the JOptionPane.

get data from jframe into previous jframe in java?

From jframe1 I have a table containing data from sql. I press the search button on jframe1 and it will appear jframe2 for me to enter the information to search. When I click the OK button on jframe2, the jframe1 table will change to only the data stream to search. I have to do. My problem is transferring data from jframe2 to jframe1
In some class, or a new one if you want to make one, set your current instance of jframe1 so you can access it when ever. Where ever jframe1 is stored, pass an instance of that class to jframe2 when initializing it.
From here you could get the jframe1 from jframe2, and I would suggest creating a method in jframe1 to set/send this "data" to it when jframe2 is read to do so.
If you are doing this with more than 1 Jframe I would suggest doing something similar to what I said above, but for any frame which can have data sent to it, implement an interface you could create to make your code more neat and structured. Then make a field in some class all frames have access to and this field could be a List or array of JFrames. Then if you want to send this "data" to all these frames, loop through the frames and check if the frame implements your interface, or something like this:
for(JFrame frame : objectWhereFramesAre.getFrames()){
if(frame instanceof YourInterface){
frame.setData(/*send data here*/);
}
}

Java: JButton that creates JTextField everytime it is pressed

Hy everyone,
I was just learning some new things with Java GUI, and I got stuck with a problem. This is what it looks like:
I want a button that adds the data from the second JPanel in to the first one.But not only once. I want it to go to the first JPanel, everytime it is pressed.
So, the button should create a new JTextField in the first JPanel, below their respective categories "name" and "age" everytime it is pressed. It means that I have to modify the "Y" field everytime, so all the new JTextFields created by the button "add data" dont get pilled up.
I don't know how to make the button "add data" Works, the other things, I know how to do. I know how to make the button creates a JTextField, with the data I want to store, only once (using getText() and setText() ), but not how to create new JTetFields with diferente "y" field everytime it is pressed.
Thank you for the help.
When you click on the button to add people you should not display a new JPanel. Instead you should display a modal JDialog. A JDialog is just like a frame but generally is only displayed for a specific function and then closed.
On this dialog you will probably want two buttons: 1) Save Data and 2) Close. This will allow you to enter information for multiple people before closing the dialog.
To display the "person" information you should probably be using a JTable. A table allows you to display data in a row/column format. See the section from the Swing tutorial on How to Use Tables for basic information to get you stated. Note, in this cause you will want to use a DefaultTableModel for your table and then you can just use the addRow(...) method to save the "person" information.
When you create the class for the dialog to get the "person" information you will want to pass the DefaultTableModel to this class as a parameter. Then when you click the "Save Data" button you get the information from the two text fields and update the TableModel. The table will be updated automatically.
So your first step is to create the main frame with the JTable with the two columns. Then you create a simple "Add Person" button. This button will then add hard coded data to the table each time it is clicked by using the addRow(...) method. Once you get this working then you change the logic of the button to display the dialog and then you can enter the "person" information on the dialog and save it.

Unable to get values from another jFrame

I want to fill values of multiple jTextBox from a jFrame into another, using accessor methods like
String getNameVal()
{
return jTextBox1.getText();
}
How to call these methods from another jFrame?
Suggestions:
It sounds like your GUI code is geared towards making JFrames, and if so, you will want to avoid this. You are painting yourself in a corner by having your class extend JFrame, forcing you to create and display JFrames, when often more flexibility is called for. In fact, I would venture that most of the Swing GUI code that I've created and that I've seen does not extend JFrame, and in fact it is rare that you'll ever want to do this.
More commonly your GUI classes will be geared towards creating JPanels, which can then be placed into JFrames or JDialogs, or JTabbedPanes, or swapped via CardLayouts, wherever needed. This will greatly increase the flexibility of your GUI coding.
This question has direct bearing on your problem. I will guess that your main problem isn't how to give classes getter methods, and how to have other classes call the getter methods. More often then not, when faced with the issue of extracting information from one GUI view to another, the issue is one of when to extract the information. If you displayed your second window as a non-modal JFrame, and then had the calling class immediately extract the data from that second JFrame, you'd get nonsense data, because you'd be extracting data before the user would have time to interact with the 2nd window and enter data.
One possible solution to this when using non-modal windows to get information from the user is to use a WindowListener so you can be notified when the user has completed his dealing with the second window, and so now data can be safely extracted.
Often better is for the 2nd window not be non-modal, as JFrames are, but instead to be a modal window such as a modal JDialog. When the calling code displays a modal dialog, all code flow in the calling code stops until the dialog is no longer visible. In this situation, no WindowListener is needed since you will know exactly when the dialog has been dealt with -- on the code line immediately after you set it visible -- and so can extract your data from it with ease.
A nice variant on this has already been mentioned in by Andrew Thompson in comments -- use a JOptionPane. Don't poo-poo this option since JOptionPanes are powerful tools, likely much more powerful than you realize as they can hold fully formed complex JPanel views, and behave just as described above, as modal dialogs.
If you need more specific help, then please don't hesitate to comment to this answer. Also if so, then consider creating and posting a Minimal, Complete, and Verifiable Example Program where you condense your code into the smallest bit that still compiles and runs, has no outside dependencies (such as need to link to a database or images), has no extra code that's not relevant to your problem, but still demonstrates your problem.
Edit
For my mcve code examples of the above suggestions, please my answers to the following StackOverflow Questions:
Using a modal JDialog to extract information
Using a JOptonPane to extract information
I assume the textfields are present in frame1 and you want to access them in frame2. The following can be a way to achieve this:
First create getters for all JTextFields that you have in your frame1. Alternatively you can have them in a panel and call getComponents() method.
Create a private variable of JFrame type in your frame2.
Modify the constructor of frame2 to receive the frame1 object and assign it to the private variable of JFrame type.
Now you can create a close() method in frame2 which disposes the frame2 and sets frame1 to visible.
But in my opinion you should create a class which handles the data in these textfields. Initialize the class object in any button click of frame1 and check for any inconsistency in the input. I can guess there is something wrong with your design.

AUTOMATICALLY FILL IN SECOND JFRAME

I have a question, im making a application where you can first select a name from a jList in a jFrame. After you've selected a name and pressed the proceed button, a second jFrame pops up.
On this jFrame there are a couple of textfield which i want to automatically fill up with information about the selected name selected in the first jFrame.
And this information which is automatically filled in is different depending on the chosen name.
I already have the first jFrame with 4 names in a arraylist but now im stuck on the second part where the chosen name is transfered to the second frame along with the extra information which must be done automatically
I hope someone can help me, i would really appreciate it.
THank you.
Well, heres how I would do it:
Exit the current jframe
Pass the data in the arrayList to a function that creates a new JFrame
Fill the new JFrame
Creating a a Swing application with multiple JFrames can be problematic.
A better and simpler approach would be to use CardLayout and to have panels within a single frame. Your ArrayList could be a member variable in the main JFrame class and could be easily be updated with data as you progress.
See: How to Use CardLayout

Categories

Resources