Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

order of args in add() method contradicts Sun

 
Bret Waldow
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13071
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic