How to to call JInternalFrame from another JInternalFrame? - java

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

Related

Is there anything logically wrong with creating a JFrame then using dispose() and then another JFrame and so on

Ok so, whilst my code works. I am beginning to question the logic of my code, and how I can improve it, before adding yet more code.
In short, I am creating a JFrame using Borderlayout as the layout manager. I have added a JPanel, which then has buttons, textfields etc added. Once the user has done everything they need to do, I get the user to click "ok" and I basically call the dispose() method.
I then paint a new JFrame, and do exactly the same thing repeatedly.
Whilst this is easy to code, and I'm a self taught newb, I am concerned about my lack of code re-use and I am convinced I should only be using one JFrame.
My main question is this : Is the dispose() method destroying the window, or just hiding it?
Any advice on this topic, would be much appreciated :)

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 make JFrame exist independently without parent?

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.

Is it possible to show a JPanel without putting it in a JFrame? Or full screen GUI

Okay this is probably a rookie question, but I have never done GUI programming in Java before. Is it possible to use a JPanel that is not in a JFrame? or essentially create a GUI that does not have the system Max, Min, Close buttons or a border?
If that seems confusing please let me know and I will be more clear with what I am trying to accomplish (I might be going about it all wrong);
You can use a JWindow instead of a JFrame.
You can use jFrame.setUndecorated(true);
Yes, you don't need a JPanel inside of a JFrame. You should take a look at this article: http://download.oracle.com/javase/tutorial/uiswing/components/panel.html

Categories

Resources