Swing tutorial part 2

Jan Newmarch

GEOMETRY


Panel


Geometry


Size Calculations


Geometry Calculations

This will force a resize each time the label is reset:
class AutoResizeButton extends Button {
  public void setLabel(String name) {
    super.setLabel(name);
    getParent().layout();
  }
}

Insets and Layout Objects


BorderLayout


BorderLayout (2)

import java.awt.*;

class TestBorderLayout extends Frame {
  TestBorderLayout()
  {
    add(new Button("Push Me W"),
            BorderLayout.WEST);
    add(new Button("Push Me E"),
            BorderLayout.EAST);
    add(new Button("Push Me S"),
            BorderLayout.SOUTH);
    add(new Button("Push Me N"),
            BorderLayout.NORTH);
    setSize(400, 200);
    setVisible(true);
  }
}

BorderLayout (3)


FlowLayout


FlowLayout (2)

TestFlowLayout()
{
  setLayout(new FlowLayout());

  add(new Button("Push Me 1"));
  add(new Button("Push Me 2"));
  add(new Button("Push Me 3"));
  add(new Button("Push Me 4"));
  setSize(400, 100);
  setVisible(true);
}

FlowLayout (3)

The same layout after resizing


GridLayout


GridLayout (2)


GridLayout (3)

    TestGridLayout()
    {	
	setLayout(new GridLayout(2, 2));

	add(new Button("Push Me 1"));
	add(new Button("Push Me 2"));
	add(new Button("Push Me 3"));
	add(new Button("Push Me 4"));
	setSize(400, 200);
	setVisible(true);
    }

GridBagLayout


GridBagLayout: Direction of Addition


GridBagLayout: Direction of Addition (2)


GridBagLayout: Direction of Addition (3)

import java.awt.*;

public class GridBagDirection extends Frame { 
    Button btn1, btn2, btn3, 
	   btn4, btn5, btn6;
    GridBagLayout gridbag;   
    GridBagConstraints c;

    public static void main(String argv[])
    {
	new GridBagDirection();
    }

GridBagLayout: Direction of Addition (4)

void init() {
    gridbag = new GridBagLayout();
    setLayout(gridbag);
    
    c = new GridBagConstraints();
    c.weightx = 1.0;
    c.fill = GridBagConstraints.BOTH;

    makeButtons();
}

GridBagLayout: Direction of Addition (5)

void makeButtons() {
    btn1 = new Button("Push Me 1"); add(btn1);
    btn2 = new Button("Push Me 2"); add(btn2);
    btn3 = new Button("Push Me 3"); add(btn3);
    btn4 = new Button("Push Me 4"); add(btn4);
    btn5 = new Button("Push Me 5"); add(btn5);
    btn6 = new Button("Push Me 6"); add(btn6);
}

GridBagLayout: Direction of Addition (6)

GridBagDirection()
{
   init();

   gridbag.setConstraints(btn1, c);

   // btn2 below btn1
   c.gridx = 0;
   c.gridy = GridBagConstraints.RELATIVE;
   gridbag.setConstraints(btn2, c);

   // more coming ...

GridBagLayout: Direction of Addition (7)

   // btn3 right of btn2
   c.gridx = GridBagConstraints.RELATIVE;
   c.gridy = 1;
   gridbag.setConstraints(btn3, c);

   // btn4 below btn3
   c.gridx = 1;
   c.gridy = GridBagConstraints.RELATIVE;
   gridbag.setConstraints(btn4, c);

   // more coming ...

GridBagLayout: Direction of Addition (8)

   // btn5 right of btn4
   c.gridx = GridBagConstraints.RELATIVE;
   c.gridy = 2;
   gridbag.setConstraints(btn5, c);

   // btn6 below btn5
   c.gridx = 2;
   c.gridy = 3; // absolute, for variety
   gridbag.setConstraints(btn6, c);

   setSize(400, 200);
   setVisible(true);
}

GridBagLayout: Cell Size Requests


GridBagLayout: Multiple Rows


GridBagLayout: Multiple Rows (2)

GridBagMultiRow()
{
   GridBagLayout gridbag = 
                 new GridBagLayout();
   setLayout(gridbag);
   GridBagConstraints c = 
                 new GridBagConstraints();
   c.weightx = 1.0;
   c.fill = GridBagConstraints.BOTH;

   // three in one row
   gridbag.setConstraints(btn1, c);
   gridbag.setConstraints(btn2, c);
   c.gridwidth = GridBagConstraints.REMAINDER;
   gridbag.setConstraints(btn3, c);

GridBagLayout: Multiple Rows (3)

   // two in one row
   c.gridwidth = 1;
   gridbag.setConstraints(btn4, c);
   c.gridwidth = GridBagConstraints.REMAINDER;
   gridbag.setConstraints(btn5, c);

   // row by itself
   c.gridwidth = GridBagConstraints.REMAINDER;
   gridbag.setConstraints(btn6, c);

   setSize(600, 300);
   setVisible(true);
}

GridBagLayout: Cell Size Granting


Denied Request

Only one row, so height can't be greater than one
   GridBagHeightDenied()
   {
      // all height requests ignored
      gridbag.setConstraints(btn1, c);
      c.gridheight = 2;
      gridbag.setConstraints(btn2, c);
      c.gridheight = 3;
      gridbag.setConstraints(btn3, c);
      c.gridheight = 4;
      gridbag.setConstraints(btn4, c);
      c.gridheight = 5;
      gridbag.setConstraints(btn5, c);
      gridbag.setConstraints(btn6, c);
   }
}

Accepted Request


Accepted Request (2)

GridBagHeightAccepted()
{
   gridbag.setConstraints(btn1, c);
   c.gridheight = 2;
   gridbag.setConstraints(btn2, c);
   c.gridheight = 3;
   gridbag.setConstraints(btn3, c);
   // these all end rows
   c.gridheight = 1;
   c.gridwidth = GridBagConstraints.REMAINDER;
   gridbag.setConstraints(btn4, c);
   gridbag.setConstraints(btn5, c);
   gridbag.setConstraints(btn6, c);
}

GridBaglayout: Resizing


GridBaglayout: Fill


GridBaglayout: Anchors


A Complex Layout


A Complex Layout (2)

 GridBagConstraints c = new GridBagConstraints();
 c.weightx = 0.0;
 c.anchor = GridBagConstraints.EAST;

 GridBagConstraints c2 = new GridBagConstraints();
 c2.gridwidth = GridBagConstraints.REMAINDER;
 c2.weightx = 1.0;
 c2.fill = GridBagConstraints.HORIZONTAL;

 gridbag.setConstraints(nameL, c);
 gridbag.setConstraints(nameT, c2);
 gridbag.setConstraints(phoneL, c);
 gridbag.setConstraints(phoneT, c2);
 gridbag.setConstraints(addressL, c);
 gridbag.setConstraints(addressT, c2);

CardLayout


CardLayout (2)

Flip through Btn1 - Btn4 circularly
import java.awt.*;
import java.awt.event.*;

public class TestCardLayout extends Frame
             implements ActionListener {
    public static void main(String argv[])
    {
	new TestCardLayout();
    }

    TestCardLayout()
    {
	setLayout(new CardLayout());

	addButton("1");
	addButton("2");
	addButton("3");
	addButton("4");

	setSize(200, 100);
	show();
    }

    protected void addButton(String key) {
	Button btn = new Button("Btn " + key);
	add(btn, key);
	btn.addActionListener(this);

    }

    public void actionPerformed(ActionEvent e) {
	CardLayout card = (CardLayout) getLayout();

	if (e.getActionCommand().equals("Btn 4")) {
	    card.first(this);
	} else {
	    card.next(this);
	}
    }
}


MENUS


Menus


MenuBar


Menu


MenuItem and CheckboxMenuItem


Menu Example

private void CreateMenu()
{
   MenuBar mb = new MenuBar();
   Menu fileB = new Menu("File");
   mb.add(fileB);

   MenuItem newB = new MenuItem("New");
   MenuItem quitB = new MenuItem("Quit");
   fileB.add(newB); fileB.add(quitB);
   
   Menu editB = new Menu("Edit");
   mb.add(editB);
   
   setMenuBar(mb);
}

Menu selection handling


DIALOGS


Dialogs


About Dialog

public class About extends Frame {
   About() {
      Button btn = new Button("Popup about...");
      add("Center", btn);
      btn.addActionListener(new PopupDialog());
      setSize(200, 200);
      setVisible(true);
   }
}

About Dialog (2)

class PopupDialog implements ActionListener {
   Dialog dialog = null;

   public void actionPerformed(ActionEvent e) {
      if (dialog == null) {
	 Component comp = (Component)
                          e.getSource();
	 Frame frame = (Frame)
                       comp.getParent();
         dialog = new Dialog(frame,
                         "About ...", true);
         Button btn = new Button("Ok");
         btn.addActionListener(new DownDialog());
         Label label = new
                       Label("About: Version 1.0, 1997");
         dialog.add(btn, "South");
         dialog.add(label, "Center");
         dialog.setSize(200, 100);
      }
      dialog.setVisible(true);
   }
}

About Dialog (3)

class DownDialog implements ActionListener {

   public void actionPerformed(ActionEvent e) {
      ((Component) e.getComponent()).
                     getParent().
                     setVisible(false);
   }
}

MISCELLANEOUS


Fonts


Graphics


Lightweight Objects


Conclusion


All material on this site is under the copyright restriction and permissions of the Open Content license, http://www.opencontent.org/opl.shtml