Aqua Phoenix
     >>  Lectures >>  Java 4  
 

Navigator
   
 
       
   

4.5 Examples

4.5.1 Example 1

Let's assign a BorderLayout to an applet, and add a few buttons to the layout:

public void init() {

  BorderLayout bl = new BorderLayout();
  setLayout(bl);

  Button b1 = new Button("Hello");
  add(b1, BorderLayout.EAST);

  Button b2 = new Button("World");
  add(b2, BorderLayout.SOUTH);
}
Let's look at each line individually. The very first line creates a particular Layout, namely the BorderLayout. The second line then assigns the layout to the applet. The method "setLayout" is part of the applet object.

In the next line, we create a Button with the label "Hello". The next line then assigns the button object to the layout. Since the layout has already been set for the applet, we merely need to call the add method, and the applet object knows to what type of layout the button is added. In order to correctly place the button, we need to specify one of the 5 regions (NORTH, SOUTH, EAST, WEST, or CENTER). We then repeat the exercise for a second button.

Note, that you cannot place two different objects in the same region. In order to do that, you would need to create another, separate layout for that region.

4.5.2 Example 2

Continuing Example 1, let's look at an example that nests layouts, specifically a GridLayout. Our goal is to create two buttons that are in the same region, one on top of the other, and a TextField that is located in a different region:

public void init() {

  BorderLayout bl = new BorderLayout();
  setLayout(bl);

  Panel p = new Panel();
  p.setLayout(new GridLayout(2, 1));

    Button b1 = new Button("Hello");
    p.add(b1);

    Button b2 = new Button("Ciao");
    p.add(b2);

  add(p, BorderLayout.EAST);

  Button b3 = new Button("World");
  add(b3, BorderLayout.SOUTH);
}
The assignment of objects follows a very systematic approach. Let's analyze it in more detail. Again, we assign a BorderLayout to the applet itself. We then create a Panel. A Panel is an empty area, which can serve several purposes. For one thing, you can draw on it; something that we will discuss in a future lecture. For the other thing, you can assign layouts and objects to it. Panels are very useful, and are oftentimes extended. For example, if you were to create a personal information component that asks for Name, Address, etc. inputs, you may wish to make a specific panel, that you can then use as an object in all sorts of applets. In our case, the panel will hold 2 buttons. But first, we need to assign a Layout Manager to the applet. Because we want both buttons to use up the same amount of space, as well as being located in a vertical alignment, we decide to use a GridLayout. GridLayout, unlike BorderLayout, takes two arguments, i.e. number of rows, and number of columns. In our case, we are creating a GridLayout with 2 rows and 1 column. We now proceed with creating our first button. This button is then added to the panel via the add() method. Notice that the add() method is the same for BorderLayouts and GridLayouts. You may wonder how the applet knows to place the objects in their respective layouts. Since we have declared the layout to be of a specific type beforehand, the objects are now placed depending on our declaration. Instead of adding the button to the applet background, we want to add it to the panel, which is why we write p.add(..), as opposed to just add(..). Note, that whenever we do not specifically reference an object, we refer to the current object. Taking the example from above, p.add(..) references the object p, which is a Panel; however, by writing add(..) we do not reference an object explicitely, but rather reference the current object implicitely. We have learned that the keyword this references the current object as well. For that reason, add(..) is equivalent to writing this.add(..).

After setting up the panel with a layout and objects, we need to add the panel to the bigger layout. Just as we have added a button to the BorderLayout before, we can now add the panel to the BorderLayout. Both panels and buttons share the notion of being graphical objects of some sort, which means that both share the feature of being able to add themselves to layouts. There is no specific "add" feature that adds buttons differently to layouts than panels. This key concept of inheritance in Object Oriented Programming is so inherent that we will see it in almost everything we learn about OOP.