I have a Java swing program consisting of many JFrames, I know not the best design, but it does the job.
If I open another program, for example Firefox, and maximize that window, my JFrames are not visible.
Now when I click the button on the window taskbar of one of my JFrames the JFrame popups up.
When this JFrame popups up I need check if one of my other JFrames are visible, I mean are in front of Firefox window.
I tried isVisible(), isShowing() and isDisplayable(), but they always return true even when the JFrame is below the Firefox window. That makes sense when reading the Java documentation.
But I can't seem to find a method to tells me if my JFrame is really visible in front of the Firefox window.
It puzzles me.
A JFrame covered by Firefox has the following properties:
14:50:08,129 DEBUG DrawFrame - isVisible: true
14:50:08,129 DEBUG DrawFrame - isDisplayable: true
14:50:08,129 DEBUG DrawFrame - isShowing: true
Just to explain my goal: I have multiple JFrames and when I click one specific JFrame button on the window taskbar I want to check if another JFrame is visible for the user. If it is not visible for the user (for example when covered by another program) than I want to make it visible. Making it visible is simple: WindowListener windowActivated and call toFront on the not visible frame, but of course this causes a loop when the other JFrame is activated again (calling windowActivated > toFront > windowActivated > toFront, and so on). That is why I need to check the visibility before calling the toFront method.
I'm not aware of a function in Java to check if a window is visible for the user, so going native may be the only solution.
use windowGainedFocus/LostFocus from WindowFocusListener
loop in an array of Window[] wins = Window.getWindows();, returns all Top-Level Containers in the current JVM
be sure that in loop by test if (wins[i] instanceof JFrame) { (or JDialog/JWindow ...)
Related
I always wondered on how can I make a program with multiple JFrames. I mean I just want one class to handle all the GUIs and stuff but how can I effectively do this? A lot of tutorials say that we make JFrame by inheriting from JFrame. But what If I want many frames?
Ex:
Title of Application in one frame with some options
Menu is one frame
Main working application is one frame
Like in a game.
But I am not sure if I am pertaining to JPanel? I am completely puzzled with the 2. I just want one un-moving frame but basically the content of the frame is changing.
When I click START for example, it will change to the gaming style of frame.
you are looking for a JFrame with a CardLayout. Some background:
A JFrame is the physical window. It comes with a title bar and three buttons: minimize, maximize, and close. Think of this as a picture frame.
A JPanel is a "content holder" of sorts. Typically, you put your other components (buttons, animations, whatever) on a JPanel, and then slap that JPanel into a JFrame. Using our picture frame example, a JPanel would be the photo paper you put in the picture frame. The other components would then be the actual contents of the picture itself, and what you have at the end is a nice picture...or in your case, an application.
Setting the JFrame to utilize a CardLayout essentially lets you have multiple JPanels inside the same JFrame at once, while still only showing one at a time. So for your application, you would have (at least) two JPanels: one for the menu, and one for the game. When the app starts, you show the MenuPanel. When the user clicks "start", you switch to the GamePanel. The MenuPanel will be put in the background and will be inaccessible until you call it to the foreground again.
If, on the other hand, you create multiple JFrames, you will have two or more physically separate windows that can be dealt with individually. This can actually be kind of cool for game development. Although it takes more time to build and link the GUI for the second window, you can then have that window affect game settings in realtime (rate of fire, bullet strength, player speed, etc.)
I think that what you are after is the Card Layout:
A CardLayout object is a layout manager for a container. It treats
each component in the container as a card. Only one card is visible at
a time, and the container acts as a stack of cards. The first
component added to a CardLayout object is the visible component when
the container is first displayed.
You can see how it is used here.
This layout manager allows you to manage situations where your frame needs to be shared across various functions. In your case for instance, you could have a functionality to handle the settings section of the game and another one to handle the actual game itself.
You could then use the manager to switch between these particular items.
you can also use Desktopane() and InternalFrame() for multiple frame.
Internalframe quite similar to Jframe but it need to setVisible(true) or show() everytime.
Which ever IDE you are using, you can create multiple JFrames in the same package, and have separate codes for each of them.
If you want to link each frame, you will have to create instances from each JFrame. for example, if when the button is pressed, we need to invoke a new Frame (that we have already created)
NewJFrame1 frame1=new NewJFrame1();
frame1.setVisible(true);
then you can decide what to with your current JFrame.
eg : (Hide, Close)
I'm not sure if this has been answered or the correct way of phrasing it, but have had no luck in my search. I have 4 JFrame guis all in their own classes: a main gui and 3 others. I want to know if it is possible to display the other guis inside the same window without opening a new window and setting the first window to a false visibility? I'm able to call the other JFrames and make them display through a series of actionlisteners, but they open another window, making me have to setVisible(false) the gui window. I want to be able to have all the guis display in the same window without opening/closing windows. Thanks
You should not be creating separate frames. Just create separate panels and swap the panels.
See the Swing tutorial on How to Use Card Layout for more information.
Also, if you ever do need more than one window, you should be using a JDialog for the child windows. An application should only have a single JFrame.
Similar to start method in applet which gets called when applet window is minimized and maximized, is there some similar method which gets called when we switch control back and forth between JFrame and some other windows (such as notepad) ??
I believe your looking for WindowListener#windowActivated and WindowListener#windowdeactivated
You need to attach a listener to the frame via JFrame#addWindowListener
In Java Swing every kind of Window, including JFrame which itself extends the Window class, can listen to focus events and so can be notified when gaining or losing focus. Just call the addWindowFocusListener on your JFrame object, passing a WindowFocusListener object that will receive and deal with FocusEvents.
You can refer to the JFrame documentation for more detailed explanation.
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.
I am running Windows. When you run an application on Windows, you get a button task bar where you can click it to maximize and minimize it. Is it possible to create a JFrame without this or some other component that has the functionality of a JFrame but without adding it to the task bar.
Use a JDialog instead of a JFrame. On a JDialog, you can set the 'modal' property, which means no 'upper bar' or anything is displayed.
Do make sure the JDialog has no parent frame or anything though: a modal JDialog will block the GUI of any parent GUI component. But if you just use it as your main component there is no problem :)