Java: exchanging data between opened jPanel and jFrame - java

From the already opened jPanel I open the jFrame "FastSearch" search window using the following method:
new FastSearch().setVisible(true);
which lists the search results. When I select one of the search results I want to send the value back to the parent jPanel which opened the child jFrame "FastSearch".
I thought it would look something like this:
ParentWindow.targetVariable = theValueFromTheOpenedForm;
But of course, it doesn't work.
How do I pass data between opened jPanel and jFrame?
Thanks in advance

You'll need to pass the object through to the FastSearch class in some way. Either in the constructor or through some other method. I'm assuming there is probably a listener interface.

Related

java: how can i pass a variable from the starter JFrame to the last without having to pass it through all JFrames?

I have this problem: I have an ArrayList that contains a few items that I want to use in a particular frame, the problem is that the array list gets full by initializing it in the main class of my project. In this class I also launch my starting frame that is chained with other frames (login frame -> middle frame --> last frame). I want to carry this ArrayList without having to carry it on through all frames and get it directly usable from main --> last frame. How can I do this?
EDIT
Wat I did it was like first frame start with this ArrayList as parameter:
Jframe jf = new LoginFrame(arraylistvariable,"Login Window");
Then in all ActionListener calls on the buttons that create new frames, disposing old ones, I set it like:
Jframe jo = new MiddleFrame(arraylistvariable,"Middle Window");
Passing this variable all over the frames but I want this to be like called only by the frame that needs this, because login frame doesn't need this variable. However, it is necessary to start the program by the login frame.
"However, it is necessary to start the program by the login frame."
No it's not.
public class MiddleFrame() {
private LoginFrame;
}
...
public static void main(String[] args){
new MiddleFrame();
}
Make the middle frame not visible upon instantiation, but make the LoginFrame visible. If the login is successfful, but make the MiddleFrame visible.
Note You don't need to use to many frame. Make use of JDialogs. See this answer for How to make a Login with a JDialog
one from the easiest way it to pass it/them trough system properties
You might want to create some class like ArrayListVariableProviderService and make it accessible either to all classes (JFrames) or via static call.
This can act as a container to many things, yet it'll abstract out all peculiarities.
Good luck.

Closing a Frame from a Panel

Let me first explain the situation,
I have a class with a JPanel called panelclass.
It's method getPanel() returns the JPanel.
In a JFrame class called frameclass, I create a new object of panelclass, got its panel and added it to the frame pane.
What I am trying to achieve is, when a button in paneclass is clicked, It should close this JFrame ie.frameclass.
I donot know how a panelclass can communicate back to the frameclass to close.
I tried this.dispose() and super.dispose() but was not successful even after extending JFrame
Is there a simpler way?
Please do help.
There are a few was to achieve this, but the simplest is probably through the use of SwingUtilities.getWindowAncestor(Component)
This will return the Window that the component was added to or null if it has no parent window. From there you can simply call Window#dispose to close the frame.
when a button in paneclass is clicked, It should close this JFrame
See Closing an Application. I prefer using something like the `ExitAction' described there. The reason is that your application will behave just like the user clicked on the close button of the frame which means that if you have any WindowListeners added to the window they will be invoked.

Calling A JFrame method from Another JFrame

I'm looking to find a solution too my issue. Currently I have 2 JFrames and 1 utilities class in my netbeans project. I'm no expert on java so please bear with me. I've tried looking through the java docs and on this site but cannot seem to find a solution to my problem.
Here is the scenario:
My launcher class launches the JFrame called MainForm.java the form then initializes components onto the screen. On this form I have a button that launches a new form called ConfigEditor.java. This form is used to edit a config file. I have a Save button on this form, and what I basically want to do is once I click save get the MainForm.java to call a method to fill in the right components with the new values.
Heres an example, heres some of the code from my Save button on the ConfigEditor.java:
if(reply == JOptionPane.YES_OPTION){
try {
Utilities.writeConfigFileBasic(ExecutionLists.getText(),DefaultResultsFile.getText(),
DefaultTestDir.getText(), Environments.getText(), ResultsViewerFile.getText());
ConfigTextArea.append(Utilities.readConfigFile());
JOptionPane.showMessageDialog(rootPane, "Saved");
Now just after the last line I want to call somthing like MainForm.initMyComponents(); as this method exists in the MainForm JFrame but it wont let me call this. The purpose of that method is to populate some of the fields with data extracted from a config file.
I'm sorry if I haven't explained it very well, I'm fairly new to Java if you need any clarification please let me know and I'll do my best to clarify it.
Can you simply pass a reference of MainForm to ConfigEditor when it is constructed? For instance:
... //Code fired by clicking the button you mentioned which is in class MainFrame
ConfigEditor configEditor = new ConfigEditor(this); //This would be a reference to your MainFrame
With this reference you can then call your desired method in the MainFrame class.

Creating pop up Frame and managing multiple Frames

I'm trying to be creative with a class project in Java.
I have a JFrame which takes in some input from the user, and I want to create a sort of pop up frame that will show a graph from the input.
The second frame is only going to be showing a graph. I'm having trouble realizing this.
I'm using the GUI builder(Netbeans) for the main JFrame and at first I tried to just create an empty JPanel and set it visible from the button, but that didn't work and I soon found out I couldn't.
I had to use a JFrame or some other container apparently(am I wrong?).
So now I'm thinking, how will I pass the information to the second pane for the graphing information?
Anyways, I'm just looking for some input or good practices in order to accomplish this. I'm reading up on CardLayout now but that's not the solution I want since it would require me to create a pane to hold the graph component. I just want the graph button to open up a graph pane(or frame) and close when the user wants to. The main frame is only used to take the input for the graph. Thank you for any input.
There will be a question soon on how I can actually graph something in Java, but ill tackle one problem at a time
You could create another class for the pop-up JFrame which would probably be the best idea. You could do something like:
public class PopupFrame extends JFrame{
//Declare variable you wish to use for your graph here.
public PopupFrame(/* Pass in variable(s) that you will need to display the information. */)
{
super("Graph");
//Set the graph variable equal to the variable to passed into the constructor.
add(/* Put in the variable that you just initialized above and that you declared outside of the constructor. */);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500,500);// Change to the dimension that you want.
setLocation(50,50);// You could also have the constructor give it these values if you want to center it on your parent window.
setVisible(true);
}
}
and in your main class, have something like:
PopupFrame f = new PopupFrame(/* Graph variable(s) passed in here. */);
f.show();
Hopefully this was of some use to you. Good luck!

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