Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to "refresh" a JFrame?

 
Adam Krupczynski
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2068
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5132
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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()?
 
Adam Krupczynski
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2068
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
a different way of doing it

 
Adam Krupczynski
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot! I modified it a bit so it would do exactly what I need and it works perfectly fine!
 
Darryl Burke
Bartender
Posts: 5132
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael, there's no need for the boolean literals or ternary expressions.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ahh! of course - never even thought of it that way.
looks so simple (and obvious) now.

thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic