Java Swing component does not repaint automatically - java

I am debugging a Java Swing application in which a component (in my case a JButton inside a JPanel inside a JPanel) does not get repainted automatically when it should. For example, when the window is first shown, one can see the JButton there, but when another window overlaps my application's window for a while, the JButton can no longer be seen.
Since I have no idea where to begin, I would like to know what are the most common causes of repainting problems in Swing components.

This would be a good place to start at http://docs.oracle.com/javase/tutorial/uiswing/painting/problems.html

what happens if you minimize both the windows and bring them back on?
you might try calling your jpanel.revalidate() at some point in your code. But I don't know where to call that unless I see your code.

Related

What's the difference between setVisible(true), repaint() and validate()?

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

Implications of setVisible() method in Swing

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.

the correct way to show and hide frames in java

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.

Multiple java Applets handling

I'm creating a simple java game Applet that has multiple Panels, The main game Panel has 4 JButtons that lead to the rest of the Panels when they are clicked.
when the program runs, the four Panels are initialized 1st inside the init(), and inside each Panel initialization, I made all the Jcomponents invisible but only the main applet.
lets say there is a JButton in the main Applet Called start, when it's pressed, i need to set all the main JButtons to invisible, and set the sub Panel to visible but it's not working for me, I used everything I could think of, like repaint() or UpdateUI() but still not working.
any suggestions would be much appreciated.
Cheers
First, ensure all creation is performed not in init(), but in the EDT, see the tutorial. If you have an ampty start() method I'd recommend you use invokeLater in the init() (instead of the tutorial recommendation of invokeAndWait).
To hide buttons simply call setVisible on the JButton. There should be no need to ask for a repaint afterwards.
Further analysis is difficult without seeing the code.

Java JPanel redraw issues

I have a Java swing application with a panel that contains three JComboBoxes that do not draw properly.
The combox boxes just show up as the down arrow on the right side, but without the label of the currently selected value.
The boxes will redraw correctly if the window is resized either bigger or smaller by even one pixel.
All of my googling has pointed to calling revalidate() on the JPanel to fix this, but that hasn't worked for me.
Calling updateUI() on the JPanel has changed it from always displaying incorrectly to displaying incorrectly half of the time.
Has anyone else seen this and found a different way to force a redraw of the combo boxes?
Can you give us some more information on how you add the combo boxes to the JPanel? This is a pretty common thing to do in Swing so I doubt that it's a JVM issue but I guess anything is possible.
Specifically, I would double check to make sure you're not accessing the GUI from any background threads. In this case, maybe you're reading the choices from a DB or something and updating the JComboBox from a background thread, which is a big no-no in Swing. See SwingUtils.invokeLater().

Categories