tags:

views:

74

answers:

5

I have a Frame class (extends JInternalFrame). Inside the class I am instantiating a JPanel and initializing setVisisble(false). After the user clicks a buttom in the frame and does some processing, I call a method inside of the JPanel to update one of its labels. I then do setVisible(true) on the JPanel.

However, the JPanel is not "refreshing" correctly after I call setText() on one of its labels. After the processing and setting the JPanel to visible (and I confirmed that the right data is there etc), the JPanel still is in its initialized form.

In other words, what do I need to do so that calling setText() on a JPanel within a frame actually refreshes the Panel?

Basically i'm wondering: if you update the text of a label inside a swing component which is nested inside of a JFrame, should the update be visible? What needs to be done to refresh if not?

+2  A: 

Try this:

myPanel.invalidate()

If that doesn't work, then maybe try posting some code.

Stargazer712
+1  A: 

You're not giving us much to go on. Here are some common things to check.

Did you override any of the paint methods of the JInternalFrame? If so, be sure to call base class versions when you're done painting. The base class versions handle things like borders around panels and clipping from other GUI objects.

Are you sure the label variable that is receiving your setText() call is the same one you added to the JPanel?

Tansir1
+1  A: 

why are you even creating the JPanel ahead of time? why not create it anew, with the correct text, the first time around?

and every time you change the label, you may need to revalidate the panel; ie:

myPanel.validate()

what does this extra panel need to accomplish? maybe you should be using a dialog; or just adding the JPanel to the existing frame.

Jill Renee
A: 

In order for changes of your objects in the memory to be totally and surely applied on the GUI you need to call validate() and repaint() methods of corresponding objects. validate() does kind of a "commit" operation for the latest changes. So, if you change the text of a label, you may not be able to see the change even if you call repaint().

Mustafa Zengin
+1  A: 

Thanks for all of your answers. The fact that I didn't post any code sort of made it impossible to figure out the problem (I now realize). This class is rather large and since I had no idea where the problem lied I just gave an overview of what I thought was relevant.

In any case, after much digging here is what went wrong in case somebody ever runs into something similar:

The JLabels I am creating inside of the JPanel are being sized using the setBounds() method to which I provide x, y, height and width. These calls to setBounds() were being done initially when the JPanel was being initialized. Furthermore, the height, width parameters were being pulled dynamically from the label (using getMinimumHeight() etc so the label gets sized according to how much text is in it). The problem was that some of the labels were being initialized with no text causing the width to be 0. So later when I called setText(), it was working properly except for the fact that the width was 0 (the text was there, I just couldn't see it!).

Anyway... the solution was simply to call setBounds() on the labels each time setText() was being used (not just initially).

aeq