aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes How to Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to "refresh" a JFrame?" Watch "How to "refresh" a JFrame?" New topic
Author

How to "refresh" a JFrame?

Adam Krupczynski
Greenhorn

Joined: Apr 18, 2009
Posts: 5
Hi!

I have a problem and I hope you guys can help :-).

In my application I create a JFrame like this:



where MyFrame is something like this (logflag is a global variable, 0 by default):



So - at the start logflag equals 0, which means only 1st tab is active. When user enters his email and password and clicks the "Sign up" button, he sends his data to the server.

The server sends back the logflag. This bit works fine. The problem is when logflag is changed (to 1, 2 or 3) the application should disable the "Sign up" tab and enable "tmis", "sqt" or both.



The problem is, when I try frame.revalidate() or frame.repaint() nothing happens. The only effect I managed to get was when I called createAndShowGUI() again - it does disable "signup" tab and enable the rest, but the only "clickable" tab is still "signup" (and when clicked it resets whole application like when logflag is 0).

So, my question is: how can I refresh my JFrame so it redraw itself based on updated loglag parameter?
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
This won't answer your question, just an alternative.

Have you thought about setting it up as a CardLayout,
which is basically a JTabbedPane, but without tabs, where
you (might) use JMenuItems to navigate (or next/previous
buttons like in an installation wizard).

If JMenuItems, 'sign-up' is enabled, rest disabled, then on
succesful sign-up, the enabled/disabled menuItems are
reversed (you could even do this in the sign-up's overridden
setEnabled())
Jesus Angeles
Ranch Hand

Joined: Feb 26, 2005
Posts: 2061
One of the reason for UI not updating is if another process is occupying the thread. Normally, you should separate the UI processes from other long processes, by having one thread for the UI and another for long processes.

However, I think this may not be your problem.

One workaround I found, is by making certain component invisible, then make them visible again. This sort of refreshed/updated my UI. Which component to make invisible-then-visible? Try the component involved. If it doesnt work, try on its immediate container; and try further up until it works or it doesnt make sense anymore, at which, this technique wouldnt be useful anymore.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4664
    
    5

Jesus Angeles wrote:One workaround I found, is by making certain component invisible, then make them visible again.

Yuck! I can't think of a situation that would require that kind of 'workaround'

Have you tried revalidate() followed by repaint()?


luck, db
There are no new questions, but there may be new answers.
Adam Krupczynski
Greenhorn

Joined: Apr 18, 2009
Posts: 5
Thanks for the replies!

@Darryl: I've already tried that, there was no effect.

@Michael: I'll try this if there's no other way, but the problem is somewhere else in the program I have to do another "refreshing" (well, not the entire jframe but only one jpanel) so I'm looking for the universal method to do this. I'm not sure if my way of thinking is correct but I was hoping there is a function like "refresh the GUI" or "hey, forget and destroy the previous GUI; build another one, but this time do it this way".

@Jesus: At the start I've tried something similar but because of the way rest of my program is built it's inconvenient and I had to change it. I'd prefer to change the components rather than show-or-hide them.

Thanks once again and if someone has other ideas I'd be happy to hear (or read in this case) them!
Jesus Angeles
Ranch Hand

Joined: Feb 26, 2005
Posts: 2061
Yes, it is yuck but it worked.

What I did is something like this:



I thought it might cause a flicker, but so far, it is flawless when it runs; I am not sure though if this is the same on slower client pcs.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
a different way of doing it

Adam Krupczynski
Greenhorn

Joined: Apr 18, 2009
Posts: 5
Thanks a lot! I modified it a bit so it would do exactly what I need and it works perfectly fine!
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4664
    
    5

Michael, there's no need for the boolean literals or ternary expressions.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
ahh! of course - never even thought of it that way.
looks so simple (and obvious) now.

thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to "refresh" a JFrame?