How to make JFrame exist independently without parent? - java

I am making a small application. From one JFrame, I am calling another JFrame. Now, I want this child JFrame to exist independently even after I've closed the parent JFrame. Is there any way I can do this? Or is this just NOT possible? Please help me. Let me know if you need me to post the code.
Thanks.

On each JFrame, set the default close operation with #setDefaultCloseOperation to the following:
DISPOSE_ON_CLOSE
When you press the close-button of the frame, it will only close it and the other will continue its life until it is closed.
Note: I would recommend to avoid multiple Frames and rather try to use Tabs, it provides a better user experience.

Yeah it is definitely possible, just make sure the original frame isn't set to exit the application on close.
The second frame exists independently from the first, and you don't necessarily have to link the two if you don't want to.

Related

GUI - Switching between screens

I got my program, that can connect to a Database working! ( Hooray )
But now, I ran into a new problem. I read that using multiple JFrame windows (and closing the old one) is not user friendly, and a bad learning habit.
So now I am wondering, is there a way to switch between Panels, or something similar?
Example:
JFrame with Login & Password. -- Users logs in, goes to the next 'screen' where he or she can see the Database info, cause he or she logged in!
What should I use, any good methods out there?
you may want to check CardLayout
The simple answer is yes you can. The idea is to have a localized class that contains a method that outside classes can call passing a jPanel then simply add that panel to your jFrames content pane (which in turn will remove the other panel). There are many ways to go about this and I hope you find one that works and I hope this answer helps you as well.
Here is the procedure I generally follow. I create and open a new Frame and make the parent frame invisible. Again when child frame is closed I make the parent frame visible. I am using this procedure for a long time and not facing any problem.
This is the piece of the code executed when login button is clicked.
...
setVisible(false); //Hide the login page
DBPage page=new DBPage(this, value1); // DBPage is another JFrame
page.setVisible(true);
I feel this much of code is enough to understand.
JLayeredPane might work.
http://docs.oracle.com/javase/8/docs/api/javax/swing/JLayeredPane.html
You could have several layers on top of each other, the login screen, etc. and show the layer that is most relevant at the time.
A previous question may prove useful:
Java Swing - how to show a panel on top of another panel?

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.

How to to call JInternalFrame from another JInternalFrame?

it is posible ?? call JInternalFrame from another JInternalFrame ??
if so how?
i've looking for the answer over many hours..
found some question before..
in here How to manage a JInternalFrame calling another JInternalFrame?
I dont know that means getInstance? I think thts a container for a contentPane..
same as this question
is there any way call other JinternalFrame from an JinternalFrame but, in the desktopPane of of main Jframe.
both of that didnt give a right answer. the error always like this..
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
help me, I've spend my day for this sad question...
or for a another way it is posible blind/return a value to the main JFrame???
thanks u :)
This depends. If you only want to interact with the other JInternalFrame itself (and not a specific instance of the frame), you could use JDesktopPane#getAllFrames
If you need to execute a specific/custom method, you would actually be better passing a reference of the frame to the frame that wants to call it...
Or even better, develop some kind of model that can be modified and fire events, separating the logic from user interface

how to repaint multiple JFrames

I have a program that uses 2-3 JFrame objects at the same time and adds components during runtime. I need one JFrame to send a message to the others and get them to repaint themselves. I'm fairly knew to java so please forgive me if I'm being an idiot and missing something simple.
Instead of multiple frames, use a single frame and as many modeless dialogs as needed. You can use a PropertyChangeListener to ensure that updates in one container are propagated to another, as shown here and here.

How can I reuse my JFrame to show several GUIs one after the other, instead of creating a new JFrame for each?

I have developed my Java code in Netbeans, and now I want to develop the GUI for my application.
The application communicates with a server, so it's going to have a login frame for sure. After that there will be a main frame. From the main frame the user can choose where to go and as you can understand there will be a lot of frames.
I have already developed a version of the application where there are a lot of frames and using the "setVisible()", but I want something better looking. I want a stable frame and inside it, changing the panels or something similar.
How would I do this?
You might use JInternalFrames if you like them, or simply use a main panel with a CardLayout, and display the appropriate card depending on the clicked menu item, or the selected JTree node (as it's done in Windows Explorer and similar applications).
Use the Swing tutorial to get you started.
You can, at any time, make any Container object a JFrame's ContentPane. You can also add and remove Containers from any other Container. If you want a user to be able to jump to any of a dozen panels at any time, CardLayout, as suggested in another answer, is easily the best route. If, however, you intend to lead the user along a somewhat controlled path, you can start with a login JPanel. When that's done, you can create the next panel (a JPanel or something else), add it, and dispose of the first one. And so on until the user exits.
If the transition from one panel to another affects nothing else in the program besides the two panels and the parent Container (JFrame or descendant), this is probably the way to go. If a bunch of other places in the program need to know about the change, you'll want a more centralized mechanism, maybe using CardLayout.

Categories

Resources