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.
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 create a little program that load a frame in which I added some panels.
When I click on some button it should show some panels and hide other.
I'm experiencing some difficult to do it, even because I don't really figure out the diference between setVisible(true), repaint() and validate() (that some friends of mine suggested to me).
I hope you can make me to understand!
Thank you.
Carefully read the API for JComponent. The usages are:
setVisible - it will hide or show your component altogether. If you set it as false, you won't see it at all.
repaint() - is called when the actual pixels need to be redrawn, this is done automatically. It's used, for example, when you move a window on top of your GUI and then move it away. The part that was covered needs to be redrawn.
validate() - you should call this when the layout of your GUI has changed and you need the manager to replace and redraw your GUI.
It's a bit more complicated than that, so again, carefully read the API.
setVisible(true): sets the component so that it's visible.
repaint(): calls the paint method on the component.
revalidate(): updates the component based on the root component
I'm having a problem with Java KeyListener when adding adding another JPanel with 5 JLabels, I've searched around this website, and most solutions to this problem involve switching from KeyListener to KeyBindings. This wont work for me because I need to know exactly when a key is pressed, released and held down. To my knowledge, KeyBindings does not provide all those.
I've tried to use
this.requestFocus();
after creating the new JPanel, but it didn't work, however when I use the same line inside the paintComponent(), it works. Which brings me to my questions: How does this reflect on performance? My paintComponent() is called about 60 times/sec. Is there a way to call it once and still have this working? I see that requestDefaultFocus() from the type JComponent is deprecated...
I've also tried adding same KeyListener to the second JPanel, but that didn't help. Im guessing one of the JLabels is the one that gets focus?
This wont work for me because I need to know exactly when a key is pressed, released and held down. To my knowledge, KeyBindings does not provide all those.
Yes it does. You have an Action for "pressed" and "released". There is no such Action as "held down" (even for a KeyListener), you just get multiple events generated.
this.requestFocus();
That is not the proper method to use for requestion focus on a component. Read the API for that method and it will tell you the proper method to use.
however when I use the same line inside the paintComponent(), it works.
This is because you can't request focus on a component until the frame has been realized, which means you've invoked pack() or setVisible() on the frame.
Is there a way to call it once and still have this working?
See the RequestFocusListener class in Dialog Focus.
The proper solution is to use Key Bindings so you don't need to use these work arounds.
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.