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!
Related
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.
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.
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
In my program I have two JFrame instances. When I click next button I want to show next frame and hide current frame. So I use this.setVisible(false) and new Next().setVisible(true). But in Next window if I click back button I want to set previous frame to be visible again and next frame must be ended (which means it must be exited).
Is there any special method(s) to do this? How can I do it?
Consider using CardLayout instead of hunting for how many JFrames there are. Then..
only one JFrame would be needed
any of Next/Back Actions will be only switching between cards
There are lots of examples in this forum - e.g. as shown here.
That is an odd & quirky GUI. I suggest instead to run a JFrame for the main GUI, and when the user wants to search, pop a JOptionPane (or modal JDialog) to accept the details to search for. This will not have the effect described above, but will follow the 'path of least surprise' for the end user.
If you want to destroy a JFrame releasing all associated resources you shold call dispose() method on it.
You may place your JFrames on a list data structure and keep a reference to current position according to the window you are displaying. In that way it will be easy to move to next and previous. But note that each frame added to the list will use memory and will have its state as you placed it in to the list.
If you are trying to create a wizard like UI, you should look up Sun(oracle)tutorial here.
create the instance of your main window in next() window.. and use same method which you chosed befoe to hide your main window, for example if your main window is named as gui then what we have to do is.
gui obj = new gui();
and if you click on back button now than do these also
this.setVisibility(false);
obj.setVisibility(true);
that's all you need.
good luck.
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.