wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes doLayout() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "doLayout()" Watch "doLayout()" New topic
Author

doLayout()

raymond yadao
Ranch Hand

Joined: Jan 29, 2001
Posts: 88
scenario:
i have a program that (at first) has two components: two buttons (labeled text and button);
i used a borderlayout and these components are position at the NORTH. what i would like to do is as a click the text button, a textfield will show from the center (BordeLayout.CENTER) same as i click the button button a button will replace the textfield form the center.i have here my code:
//<applet code=formSelector height=300 width=300>
//</applet>

i compiled and it run but as i click the button it displays nothing.

thanks in advance.....
raymond
[ January 28, 2002: Message edited by: Cindy Glass ]
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9049
    
  10
What if you add the TextField to the panel when you init() rather than in actionPerformed but keep it hidden. Then when you press the button, unhide (setVisible(true)) it.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
I'm also curious about how to add components to an applet after it has been initiated and about how to cause hidden components to become visible.
I would have thought that the following code would accomplish this second task, but it does not.

This code demonstrates the order of automatic method calls that add components: first the constructor, then init(), then start() - are there any others that can add components?
How would it be accomplished to cause a hidden component to become visible?
Thank You
[ January 28, 2002: Message edited by: Dirk Schreckmann ]

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Dirk,
First off you need to add your class as a ActionListener for all the buttons in order for the actionPerformed method to be called at all.
Second, you need to understand how layoutManagers work. Calling repaint does not perform any layout managing it justs invokes redraw on all its know children depending on the clipping rectangle specified inside the Graphics component. If you happen to look at the Container API you will see a method called validate. What it is for is to perform the layout whenever something has changed (i.e., window resize, component shown/hidden, etc.). That seems to be what you want. Your program will work correctly if you just replace your call to repaint with a call to validate at the bottom of your actionPerformed method.
Regards,
Manfred.
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Hello,
Here is the corrected code (with the components registered as action listeners -- must have been tired last night when first posted):

Thanks again Manfred.
raymond yadao
Ranch Hand

Joined: Jan 29, 2001
Posts: 88
thanks for your replies guys!!!
one thing more, what's the difference of doLayout() and validate() method?
thanks in advance!!!
raymond
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9049
    
  10

public void doLayout()
Causes this container to lay out its components. Most programs should not call this method directly, but should invoke the validate method instead.
 
 
subject: doLayout()