I am developing a Java Swing-based application with different perspectives. For the "main menu" perspective I do not want the window (JFrame) to be decorated, while in other perspective I do want the window to be decorated. In other words, I need want to change the decoration attribute dynamically.
I have tried to use setUndecorated(false) and setUndecorated(true), but I seems I can only set this once, before actually showing the window.
Is there a way to achieve this?
From the javadoc:
Disables or enables decorations for this frame. This method can only be called while the frame is not displayable.
Therefore, once the JFrame is packed and/or displayed, you can no longer change that value. If you want to change the undecorated state of a JFrame you will need to dispose() it first, then change the state and eventually make it visible again.
After all, I had to take a different approach.
The former solution did work, as I stated in my last comment.
However, it was showing the default LAF window decoration, while I was using a different LAF.
So the result was graphically inconsistent with the rest of the LAF. Finally, I came with the right solution, I used setUndecorate(true) for my frame. Then, when I had to change my perspective to one using decorations I simply had to use the following code
contentPane.getRootPane().setWindowDecorationStyle(JRootPane.FRAME);
And when I had to revert to the non decorate perspective, I use
contentPane.getRootPane().setWindowDecorationStyle(JRootPane.NONE);
This approach didn't need to dispose the window and show it again (which actually produced a brief but still visible hide/show of the window)
Related
I am developing a Java Swing-based application with different perspectives. For the "main menu" perspective I do not want the window (JFrame) to be decorated, while in other perspective I do want the window to be decorated. In other words, I need want to change the decoration attribute dynamically.
I have tried to use setUndecorated(false) and setUndecorated(true), but I seems I can only set this once, before actually showing the window.
Is there a way to achieve this?
From the javadoc:
Disables or enables decorations for this frame. This method can only be called while the frame is not displayable.
Therefore, once the JFrame is packed and/or displayed, you can no longer change that value. If you want to change the undecorated state of a JFrame you will need to dispose() it first, then change the state and eventually make it visible again.
After all, I had to take a different approach.
The former solution did work, as I stated in my last comment.
However, it was showing the default LAF window decoration, while I was using a different LAF.
So the result was graphically inconsistent with the rest of the LAF. Finally, I came with the right solution, I used setUndecorate(true) for my frame. Then, when I had to change my perspective to one using decorations I simply had to use the following code
contentPane.getRootPane().setWindowDecorationStyle(JRootPane.FRAME);
And when I had to revert to the non decorate perspective, I use
contentPane.getRootPane().setWindowDecorationStyle(JRootPane.NONE);
This approach didn't need to dispose the window and show it again (which actually produced a brief but still visible hide/show of the window)
My Java program has a number of JFrames and a main frame with some buttons. When the user clicks on each button the related frame is showed. What is the correct way to show and hide these frames?
Just setVisible(true); and setVisible(false);?
For showing a JFrame, setVisible(true) is the correct (and besides the deprecated show()-method) also the only way of making it visible.
For hiding a JFrame, setVisible(false) is correct (and again besides the deprecated hide() again the only way).
Depending on if you plan to eventually reuse the frame (show it again in future) you may additionally want to additionally call dispose() if you will not show the frame again. This is escpecially important if you expect the JVM to exit automatically after the last window has been closed.
Yes, this the correct way of showing and hiding frames. It is worth bearing in mind however that, assuming a reference to the JFrame still exists, the object remains in memory. Hiding it is therefore not the same as unloading it completely.
Using this JDialog constructor, where I specify the owning JFrame instance, I find that the JDialog is not centered over it's owner component. Instead, it appears in the top-left corner. In order to get this to work, I must specify the owner component in the setLocationRelativeTo method.
Why is this?
Work Environment:
Dual monitors
Windows XP OS
JDK 1.6.0_29
Note that for the JFrame instance, I use setLocationRelativeTo(null).
JDialog is very general I think. If you want quick ways to pop a general dialog box then look at JOptionPane. It has methods to easily create a centred JDialog component or immediately pop up a blocking dialog window.
e.g.
JDialog dialog = new JOptionPane("message", JOptionPane.INFORMATION_MESSAGE)
.createDialog(jFrameOwner, "window title");
Though you probably really want to look at the JOptionPane.showXxxDialog static methods. Very useful and convenient.
And you may wish to take a look at the dialog tutorial. All the dialogs produced by the java web start application can be produced using the JOptionPane class.
Sounds like a design decision. Sometimes you want to give the dialog a reference to it's parent, without to center the location over it.
I would like to implement what follows:
when my application perform some critics operations or produce some errors, i want to display an alert JDialog telling the user what is happening.
Now, because some errors may put my application into an inconsistent status, until they are resolved, i would like temporarily disable mouse event dispatch to all components (including JMenu, JToolbar, .. )except the showed JDialog.
Is there anyway to do that? Or I have to manually removed all mouse listeners from all of components of my application, and re-add them later?
Make the dialog "modal" with setModal(true).
Simplest way is to call
frame.setEnabled(false);
where frame is your top-level window.
Note that the above soln may change the look of frame until its enabled again. For real control people play around with EventQueue's.
I can set the title bar background when maximised using InternalFrame.activeTitleBackground and InternalFrame.inactiveTitleBackground but how do I set it when the internal frame is minimised?
There is no easy solution. The frame colors are part of the UI, so you will need to create a custom UI to do what you want. You can start by searching the Java source code to see where the "activeTitleBackground" and "inactiveTitleBackground" properties are used. That will give you an idea of the class and method that needs to be customized.