Creating a superior JFrame over another - java

I have two JFrames. Both are visible at same time.
One JFrame takes the whole screen..its just plain white. (it is acting as a background). And other JFrame is a small box with buttons/texts and other swing components.
The problem I get is when I click the big JFrame area, the JFrame box minimizes. So how do I specify java to make sure the JFrame box is always on top of the JFrame background?

Use a JInternalFrame

Make the JFrame box a JPanel box.
Your application should only have one JFrame.

JFrame is a TopLevel Component and therefore usually you don't put a JFrame into another. If you want to put your smaller jframe into your bigger I would subclass either JDialog or a JPanel.

In general, an application should only have a single JFrame. Other windows should be dialogs.
The problem I get is when I click the big JFrame area, the JFrame box minimizes.
When you use the dialog make sure you specify the frame as the owner of the dialog:
JFrame frame = new JFrame();
JDialog dialog = new JDialog(frame);
If the main frame is ever minimized, the dialog will also be minimized. When the frame is restored the dialog will always display on top of the frame.

use Jdialog with setModal(false) for your small window ,
probably you want something similar to gimp
look at gimp toolbox , only X at title , means its a Dialog.
hope that's help

Related

How to display JinternalFrame in jDisktopPane, but that DisktopPane contains GridLayout?

I not able to getting the proper JinternalFrame. In my application in main JFrame I have one disktopPane that contains GridLayout.
When I call JInternalFrame by click on button I am getting that internalFrame, but that fix to a particular Grid box only, and I am not able to maximize the InternalFrame.
How can I solve the probleam?
below is my GUI:
I am doing Code through NetBeans.
How Can I fix this issue ?
Don't use a jdesktoppane. Use a normal JFrame with a JPanel with your components and the gridbaglayout.
For the windows floating above the main JFrame use new JFrame's with setAlwaysOnTop(true) and make sure they have your main JFrame as root.

Adding content to a JPanel

I'm new to Java and actually designing the GUI for an application.
My main is a JFrame with 5 buttons and 1 panel which will have the "content", for the first button for example, I've designed a Jframe which has a JTabbedPane.
Now I would like to know how can I incorporate the content from that frame to the "content" panel when clicking on the button ?
I tried to use .add but I get:
java.lang.IllegalArgumentException: adding a window to a container
(seems we can't add Jframe to Jpanel).
I also tried the setVisible way but it doesn't meet what I need since it will hide the panel completely and I will get a tiny window with the buttons.
![Jframe content][1]
![Main Jframe with buttons and Jpanel to show the jframe content][2]
The code is generated by netbeans, and I forgot to mention that I did research on adding a Jframe into another Jframe but here isn't my problem at all.
I tried by changing the Jframe by JInternalFrame but clicking on button doesn't do anything.
Button has
contentPanel.add(new GestionUtilisateur());
So basically when you click on the "Gestion Utilisateur" button for example, you get that JTabbedPane that has to appear in the content area (which is blank here)
You should not be putting JFrames inside JPanels. If you have multiple panels you would like to display, depending on something like a button, look in to LAYOUTS.
In particular, it sounds like a CardLayout would work well for your needs. CardLayouts allow you to swap which panel is displayed in a frame by bringing it to the "front" of a list of panels. This would let you display your JTabbedPane on one button click, then click another to change the content pane.
JFrame can not be added in a JPanel.
use JInternalFrame
Make and hold references to JPanels containing your content. A JFrame is really just that, it's a frame (though you can add a single component to it).
You can't add a JFrame to a JPanel. If you want multiple components to be visible use layouts such as BorderLayout, GridBag, etc. Check out some of the Swing layout tutorials here.
Content should be designed as JPanel (you can design it with drag&drop just like JFrame) but if you really have to put a JFrame to JPanel for some reason, you can do it by
myJPanel.add(myJFrame.getContentPane());
however i would suggest modification of your program.

JFrame changing screens

I'm wondering how to change screens in a JFrame. For example, changing from the starting screen to a different screen. So you have an assortment of buttons, labels, trees, etc on one screen, as the user clicks a button a different layout appears.
Would the 'setVisible(false) and setVisible(true)' do the trick?
You've got it! Create separate JFrame instances for each of your frames:
JFrame frame1 = new JFrame();
JFrame frame2 = new JFrame();
//populate your frames with stuff
frame1.setVisible(false);
frame2.setVisible(true);
On a side note, you'll want to make sure to use setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE) on any secondary frames to prevent your application from terminating if a user closes a secondary frame.
All that being said, you can also use multiple JPanel instances inside of the same JFrame instead of creating multiple JFrame instances. This way, all the action of your application will take place in one window.
I would strongly recommend giving this a read through: http://docs.oracle.com/javase/tutorial/uiswing/

Java: Dispose ActionEvent from different JFrame

I want one JFrame to have a method like this:
private void someEvent(java.awt.event.ActionEvent evt){
//initialize another JFrame
//set the new JFrame to be visible
//set this JFrame to be disabled
}
Which is possible, but I also want the main JFrame to perform something when the newly created JFrame is disposed. I don't want to pass in the main JFrame to the new JFrame however. Is this possible?
Instead, use CardLayout to switch between the two desired content panes. There's an example here.
Don't have one JFrame create and display another JFrame. Instead the second window should be a JDialog, either modal if you want the first window frozen until the second has been dealt with or non-modal if otherwise. If modal then the first window's code will resume once the JDialog has been disposed of, and the code flow will start right after the setVisible(true) call on the dialog. If non-Modal, you'll likely want to add a WindowListener to the dialog.
For example, please check out my code here, here, and here.

creating a timer clock with java frame i.e Jframe

I am stuck at a point where I need a Timer which displays some time remaining when i click a button on the Jframe. But the timer frame should be a different frame and should be unclosable. Is there a way to do it?
different frame and should be unclosable.
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
The same can be done for a JDialog.
In both cases the close button will appear on the window, it just won't do anything.
Using a dialog is better than a frame because in general applications should only have a single JFrame and the child dialogs for other windows.
How about creating an undecorated non-modal JDialog?

Categories