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)
I'm writing a program that uses a few components in a GUI. However, I don't need all of them showing at the same time. In addition, depending on the user's input, there may be times once a component is not in use anymore, that it may or may not be needed again.
So far I have simply just invoked setVisible(false) for each component that I do want to show on the screen. If they are needed again, i simply make them visible.
My question is this. Does setting a component's visibility to false have major implications on performance of a program (generally speaking)? Does the paintComponent method paint a component that is not visible and then just does not show it, or does it ignore it all together?
Also, is it better to just to remove the component from a container instead?
If it is not visible it not being drawn. Lets say I made a button and then made it so it prints "Hello" when I press it. If the button setVisible() is false I won't be able to click on it, its not their.
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 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.