Key Ideas
[Part 8 of 11]


There are three key ideas in this part:

  1. The first is that we make GUI containers, then use their add methods to add other containers and objects to them (note, in below, we make the Button labels as instance variables so we can access them in all methods).

    import java.awt.*;

    public class PanelGUI extends Frame {

       private Button b1 = null;
       private Button b2 = null;

       public PanelGUI() {
          setSize(300,300);
          Panel buttonPanel = new Panel();
          b1 = new Button("Push Me");
          b1.setSize(50,100);
          b2 = new Button("Push Me");
          b2.setSize(50,100);
          buttonPanel.add(b1);
          buttonPanel.add(b2);
          add(buttonPanel, BorderLayout.SOUTH);
          setVisible(true);
       }

       public static void main (String args[]) {
          new PanelGUI();
       }
    }

  2. The second is that if we want to add actions to components, we need to register them with listeners. This can be the same class or another class (though if you use another class, you have to think about what objects it needs to access and how to get them there):

    import java.awt.*;
    import java.awt.event.*;

    public class PanelGUI extends Frame implements ActionListener {

       private Button b1 = null;
       private Button b2 = null;

       public PanelGUI() {
          setSize(300,300);
          Panel buttonPanel = new Panel();
          b1 = new Button("Push Me");
          b1.addActionListener(this);
          b1.setSize(50,100);
          b2 = new Button("Push Me");
          ButtonListener buLis = new ButtonListener(b1);
          b2.addActionListener(buLis);
          b2.setSize(50,100);
          buttonPanel.add(b1);
          buttonPanel.add(b2);
          add(buttonPanel, BorderLayout.SOUTH);
          setVisible(true);
       }

       public void actionPerformed(ActionEvent ae) {
          ((Button)ae.getSource()).setLabel("Thanks!");
          b2.setLabel("No! Me!!");
       }

       public static void main (String args[]) {
          new PanelGUI();
       }
    }

    ----------------------------------------

    import java.awt.*;
    import java.awt.event.*;

    public class ButtonListener implements ActionListener {

       private Button otherButton = null;

       public ButtonListener(Button buttonIn) {
          otherButton = buttonIn;
       }

       public void actionPerformed(ActionEvent ae) {
          ((Button)ae.getSource()).setLabel("Thanks!");
          otherButton.setLabel("No! Me!!");
       }
    }

    or another class:

    import java.awt.*;

  3. The third is that good software development starts with the users and follows them through the development process.

Here's some code which uses these ideas. Note the use of setName() and getName() to identify the component. We could use the label, but using the name (which is avalable for all components) allows us to set the label to different things for different languages and still use switch. Note also the use of setEnabled() to prevent the user saving until they've opened something.

import java.awt.*;
import java.awt.event.*;
import java.io.*;

public class MenuGUI extends Frame implements ActionListener {

   private MenuItem fileOpen = null;
   private MenuItem fileSave = null;

   public MenuGUI() {
      setSize(300,300);
      MenuBar menubar = new MenuBar();
      Menu fileMenu = new Menu("File");
      fileOpen = new MenuItem("Open");
      fileOpen.setName("Open");
      fileOpen.addActionListener(this);
      fileMenu.add(fileOpen);
      fileSave = new MenuItem("Save");
      fileSave.setName("Save");
      fileSave.setEnabled(false);
      fileSave.addActionListener(this);
      fileMenu.add(fileSave);
      menubar.add(fileMenu);
      setMenuBar(menubar);

      addWindowListener(new WindowAdapter(){
         public void windowClosing(WindowEvent e){
            System.exit(0);
         }
      });

      setVisible(true);
   }

   public void actionPerformed(ActionEvent ae) {

      String name = ((MenuItem)ae.getSource()).getName();
      FileDialog fd = null;
      File f = null;

      switch (name) {
         case "Open":
            fd = new FileDialog(this, "Open", FileDialog.LOAD);
            fd.setVisible(true);
            f = null;
            if((fd.getDirectory() != null)||( fd.getFile() != null)) {
               f = new File(fd.getDirectory() + fd.getFile());
               fileSave.setEnabled(true);
            }
            // Etc.
            break;
         case "Save":
            fd = new FileDialog(this, "Save", FileDialog.SAVE);
            fd.setVisible(true);
            f = null;
            if((fd.getDirectory() != null)||( fd.getFile() != null)) {
               f = new File(fd.getDirectory() + fd.getFile());
            }
            // Etc.
            break;
      }
   }

   public static void main (String args[]) {
      new MenuGUI();
   }
}