Java Applet adding buttons later.

ok heres what I want to do..

I want to have an applet that shows an animated image that says "LOADING..." on the screen.. Then when the server sends back a message I want to display my buttons and textAreas..

Unfortunately so far as my knowledge is concerned buttons and TextAreas must be added in the init method of the applet..

And the image isnt drawn until AFTER the init method..

I tried using the setVisible method to my buttons, however that works very poorly.. For example I might set a button to be not visible and when I set it to be visible again the button doesnt come back (even after using repaint)..

I thought of loading a new applet inside an old applet but I dont know how this is done or if it can be done..

So thats my question. How to do this?

[806 byte] By [Conquerana] at [2007-10-3 8:10:09]
# 1
I think you can do this with CardLayout. Have one card for "loading" and another for the buttons and stuff. So you set it all up in init, but switch to the card with the buttons later.
paulcwa at 2007-7-15 3:14:30 > top of Java-index,Java Essentials,Java Programming...
# 2
Just curious, is that really true that you have to add components in the init() method?This tutorial http://java.sun.com/docs/books/tutorial/deployment/applet/containerMethods.html adds components in an invokeLater which surely runs after the init
kindofbluea at 2007-7-15 3:14:30 > top of Java-index,Java Essentials,Java Programming...
# 3
I just said init() because you have to do them somewhere.I have no idea where else you can call them. I suspect a CardLayout would make it work better because you switch to a new layout all at once.But then I've never tried it.
paulcwa at 2007-7-15 3:14:30 > top of Java-index,Java Essentials,Java Programming...
# 4
Oh, sorry I wasn't saying your solution wouldn't work--I'm sure it would too. I was just wondering if you really couldn't add an "initializing..." label in the init(), and then remove it and add the other components later because I didn't see any reason why not
kindofbluea at 2007-7-15 3:14:30 > top of Java-index,Java Essentials,Java Programming...
# 5

I haven't tried it. Maybe you can. But maybe as you added and removed components, and then did a validate or pack to make the stuff all fit, it would be ugly. But I could be remembering the early days of Java, when computer hardware was about 10% of what it is now (thanks Moore's law). A CardLayout seems like a convenient way to switch between stuff like that, kind of like double buffering for widgets. But again I haven't tried it, so who knows. Just a suggestion.

paulcwa at 2007-7-15 3:14:30 > top of Java-index,Java Essentials,Java Programming...
# 6

I don't use AWT, but I believe you can add/remove components from a panel at any time. But once the GUI is visible you need to use:

panel.validate();

to invoke the LayoutManager so the components can be painted in their correct positions.

Forget my ramblings, paulcw only suggested the above an hour ago.

Message was edited by:

camickr

camickra at 2007-7-15 3:14:30 > top of Java-index,Java Essentials,Java Programming...
# 7
Sometimes invalidate() works better than validate().For Swing JComponents, you should call revalidate().
TimRyanNZa at 2007-7-15 3:14:30 > top of Java-index,Java Essentials,Java Programming...
# 8

If you add components in Swing and then call revalidate(), while the window or panel or whatever is visible, are the components only made visible after their correct positions have been, or do you see artifacts of its size and position being adjusted?

I don't do GUI programming much, but I dimly recall this being a problem a long time ago.

Java is a pretty mature language, isn't it? Not compared to C, of course, but it's been ten years I think. Geez...

paulcwa at 2007-7-15 3:14:30 > top of Java-index,Java Essentials,Java Programming...