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.
Related
how can I turn off the first jframe after clicking a jbutton to open a second jframe? just like this.
this is what I want to happen in my GUI:
It sounds like you are talking about implementing a 'GlassPane'
A GlassPane is a technique where you place a new RootFrame layer over the existing components and use it to
Absorb all of the mouse events to prevent them from interacting with the components
Shade the UI to draw more attention to the other modal window or other frame
You can read about creating a glasspane/rootpane
and there are plenty of examples of its usage
A Swing application should only contain a single JFrame.
For the child window you can use:
a modal JDialog for a complex window when you want full control over the components on the dialog.
a JOptionPane for a easy to use pre configured "confirm" dialog. See: How to Make Dialogs for examples.
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 ...)
I'm using a custom JFrame to implement a simple dialog in a Java application I'm working on.
After the user pushes the 'Apply' button in the window, it should close.
What would be the most conventional way to do this? Is setVisible(false) inside the class the best way? Is there any way more recommended?
To close a window in Swing like JFrame or JDialog you have two options.
Call dispose()
Just call dispose() method:
public void dispose()
Releases all of the native screen resources used by this Window, its
subcomponents, and all of its owned children. That is, the resources
for these Components will be destroyed, any memory they consume will
be returned to the OS, and they will be marked as undisplayable.
For instance:
frame.dispose();
dialog.dispose();
Dispatch a WINDOW_CLOSING event
You can dispatch a new WindowEvent like this:
frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));
dialog.dispatchEvent(new WindowEvent(dialog, WindowEvent.WINDOW_CLOSING));
If the window has attached a WindowListener it will be notified. If the frame's (or dialog's) default close operation is set then this action will be performed. The possible close operations are:
DO_NOTHING_ON_CLOSE (defined in WindowConstants): Don't do anything;
require the program to handle the operation in the windowClosing
method of a registered WindowListener object.
HIDE_ON_CLOSE (defined in WindowConstants): Automatically hide the
frame after invoking any registered WindowListener objects.
DISPOSE_ON_CLOSE (defined in WindowConstants): Automatically hide and
dispose the frame after invoking any registered WindowListener
objects.
EXIT_ON_CLOSE (defined in JFrame and not available for JDialog): Exit the application using the System exit method. Use this only in applications.
It depends on what you're trying to achieve. Setting the window to not visible will simply hide it but it will still be running in the background (JFrame/InternalFrame). You can use JDialog (See JOptionPane as an example) to create temporary frames which are truly closed when clicking on one of the buttons. You can also retrieve the selected option when the user closed the window (here : Javadoc). You can also forcibly close a window by firing an event to close it, like so:
Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(
new WindowEvent(frame, WindowEvent.WINDOW_CLOSING)
)
Inside an actionlistener for example.
There are multiple operations which can be performed when you close a JFrame.
Suppose you have a JFrame
JFrame frame = new JFrame();
This one exits the JVM when closed.
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
This one just hides the JFrame.
frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
This one disposes the JFrame.
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
When the last displayable window within the Java virtual machine (VM) is disposed of, the VM may terminate
And the default is do nothing.
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
On javadoc, the HIDE_ON_CLOSE default option says that
Automatically hide the frame after invoking any registered WindowListener objects.
Now what it means "HIDE"? the realtive object is destroyed or just hide and continue using resources?
On javadoc, the HIDE_ON_CLOSE default option says that
Automatically hide the frame after invoking any registered WindowListener objects.
Now what it means "HIDE"? the realtive object is destroyed or just hide and continue using resources?
HIDE_ON_CLOSE is the same as JFrame.setVisible(false),
then JFrame in only hiden, invisible, isn't destroyed somehow (the same for JFrame.dispose()), by JFrame.setVisible(true) is again visible on the sceen and without any changes
EDIT
#giozh wrote and if i want to destroy the jframe (without close the
entire application)?
by default there isn't any reason, because by default there no reason to create another JFrame, don't do that, use CardLayout (with JFrame.pack() if is neccessary to change JFrames size on the screen)
and all those Object stays and increasing JVM memory, never will be CG'ed, then there isn't siginficant difference for JVM memory between JFrame.HIDE_ON_CLOSE, JFrame.DISPOSE_ON_CLOSE or JFrame.setVisible(false)
(in the casse that you hate CardLayout) you can to remove all JComponents from JFrames ContentPane, then to add new JComponents, set LayoutManager and last code lines (after all changes to the already visible JFrame is done) would be JFrame.(re)validate();, JFrame.repaint(); and JFrame.pack();
hide it in background without destroying it, keep it in memory
HIDE_ON_CLOSE means that it will just disppear but will be running in the background and consuming resources ,though not visible on the screen.
But using EXIT_ON_CLOSE,instead,makes it disappear as well as kills the application (Use this if you want to really close the application)
I have a JApplet which is used for chat. I would like to make it possible that when the applet is minimised and a chat message is received by the user, the minimised window becomes orange (and thus shows the user that something has occurred).
How is it possible to make the applet do this?
Thanks,
Tim
You may have access to the system tray in an applet (I'm not sure). Have a look at the java.awt.SystemTray class - the in-tray lets you pop up messages to the user.
Alternatively you could attempt to cause the Window's toFront method to be called or to "maximize" using the setSize methods (again, I'm not sure what effect this has in an applet). I suspect that the toFront method will be a good bet
Another option I'd look at is raising a JDialog. The presence of this may cause the OS to draw attention to the minimized applet. You could listen to window events representing the screen un-minimizing to clear the dialog so that the user never knew it was there.