aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes order of args in add() method contradicts Sun Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "order of args in add() method contradicts Sun" Watch "order of args in add() method contradicts Sun" New topic
Author

order of args in add() method contradicts Sun

Bret Waldow
Ranch Hand

Joined: Aug 04, 2000
Posts: 59
I was playing with an example of BorderLayout in my book, and noticed that the order of arguments when adding buttons was the reverse of what the jdk API docs say.
But it works without complaint. Check this out:
import java.awt.*;
public class BorderLayoutExample {
public static void main( String arg[] ) {
Frame f = new Frame( "BorderLayout Example" );
f.setBounds( 0, 0, 300, 300 );
f.setLayout( new BorderLayout() );
Button east = new Button( "East" );
Button center = new Button( "Center" );
f.add( BorderLayout.EAST , east );
f.add( center, BorderLayout.CENTER );
f.setVisible( true );
}
}
This compiles and runs. Why does this work?
regards,
Bret
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12788
    
    5
It works because there are several different add methods, both
add( String name, Component c ) ; // 1
and
add( Component c, Object constraints ) ; // 2
The first example corresponds to 1
f.add( BorderLayout.EAST , east ); // EAST is a String constant
and in the next, CENTER (a String) is treated as a constraint.
f.add( center, BorderLayout.CENTER )
Certain versions of MSIE could not recognize one of these (I cant remember which one)
Sun added style 2 with JDK 1.1 because they anticipated more complex constraints being used with more complex layout managers. See the java.awt.LayoutManager2 interface.
Bill
Deepak M
Ranch Hand

Joined: Jul 10, 2000
Posts: 124
Certain versions of MSIE could not recognize one of these (I cant remember which one)
Bill[/B]

Probably certain versions of MSIE do not recognize the following :
From API -
public Component add(String name,
Component comp)
Adds the specified component to this container. It is strongly advised to use the 1.1 method, add(Component, Object), in place of this method.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: order of args in add() method contradicts Sun