6

I have a general question that is Java related.

I am writing an application that has a GUI menu. I am trying to change one part of the GUI menu based on the selection of a Radio button.

Do I need to:

  1. Redraw the whole window or just update that part with:

    setVisible(true)?
    
  2. If I just use the statement from #1 above .. the GUI is fine -- until I move the mouse over it and then I see the previous button choice. What am I doing wrong?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ironmantis7x
  • 807
  • 2
  • 23
  • 58
  • i think there is a paint() method – amphibient Oct 12 '12 at 19:29
  • repaint() is probably what you wnat – daniel gratzer Oct 12 '12 at 19:29
  • I tried just the repaint() method and it did not work at all. I tried setVisible(true) along with that and that did not work either. – ironmantis7x Oct 12 '12 at 19:37
  • 2
    Explain what *"change one part of the GUI menu"* actually entails? Tell us **WHY** you want to do this, now you are asking *how*, which comes after *why*! In Swing you should never have to do what you are asking manually, you just update the models and the screen gets updated automatically. –  Oct 12 '12 at 19:38
  • You should not have to repaint any component manually. How are you updating the menu? – mkhelif Oct 12 '12 at 19:50
  • The reason I want to change it is -- in my application the user select which device platform type they want top test (that choice is a set of two radio buttons on the left). When the user selects either Android or iOS, the center grouping of check boxes changes to reflect a group of android devices they can test or a group of iOS devices that they can test. So the only part pf the JFrame that changes is the group of check boxes in the middle. I hope this helps clarify things. Please forgive my ignorance. I am new to Java. – ironmantis7x Oct 12 '12 at 19:51
  • mkhelif: I am updating the menu by clicking on a refresh button that re-adds the grouping back onto the panel in the JFrame (if that makes sense??). – ironmantis7x Oct 12 '12 at 19:59

3 Answers3

8

Swing components have a repaint(), revalidate(), and doLayout() method. One of those should probably be able to redraw whichever pieces you want. However, doLayout is not something that you should be taking responsibility for, that's the layout engines responsibility.

You may also want to check out this post, the first response has a pretty good explanation, and links to an article with more detail.

In terms of the second part of your question, I'm not sure, but we may need to see some code to get an idea. Is the 'replaced area' actually being removed from the view?

Community
  • 1
  • 1
shortstuffsushi
  • 2,271
  • 19
  • 33
  • 1
    what do I need to import to use repaint() or doLayout()? – ironmantis7x Oct 12 '12 at 19:45
  • You wouldn't need to import anything extra, these should be methods on the Swing Components you are using -- see the docs http://docs.oracle.com/javase/6/docs/api/java/awt/Component.html. Any subclass of Component should have those methods available. – shortstuffsushi Oct 12 '12 at 20:17
  • You avoid calling doLayout directly, if you find yourself in a position where this is the only thing that works then you've done one thing wrong. doLayout should effect more the just your current container. Instead, use invalidate instead – MadProgrammer Oct 12 '12 at 20:20
  • I thought that I had added revalidate to my answer... my mistake. The question linked talks about it, though. Updating my answer now. – shortstuffsushi Oct 12 '12 at 20:21
  • shortstuffsushi: looking at my cod e-- no I have not removed the replaced area from the GUI. this is all starting to make sense somewhat now .... do I use a remove(component) to get rid of what I am trying to replace?? – ironmantis7x Oct 12 '12 at 20:35
  • Well -- turns out if I remove the old stuff then the new stuff works great!! Thanks for all your help people!!! – ironmantis7x Oct 12 '12 at 20:38
  • 1
    No problem. However, if this is something that is likely to be toggled back and forth, you might want to consider Andrew Thompson's answer as well -- using the CardLayout would keep both "screens" in memory, and remove a lot of your manual work to flip between them. – shortstuffsushi Oct 12 '12 at 20:40
4

..in my application the user select which device platform type they want top test (that choice is a set of two radio buttons on the left). When the user selects either Android or iOS, the center grouping of check boxes changes to reflect a group of android devices they can test or a group of iOS devices that they can test.

  1. Put a panel in the 'center grouping'.
  2. Use a CardLayout for the panel.
  3. Add both iOS & Android controls to the panel with the card layout.
  4. Flip between them as needed.
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
3

Call revalidate() on the top level component.

BillRobertson42
  • 12,602
  • 4
  • 40
  • 57
  • I am new to Java so please be patient with me .... exactly how do I call revalidate() on the top level component? every time I try to insert it in after a repaint(), it get a compiler error. – ironmantis7x Oct 12 '12 at 19:48