Menus

  • Redone in Swing to increase flexability
  • New Features:
    • including icons in menu items
    • increased support for selectable menu items (CheckBoxMenuItem, RadioButtonMenuItem)
  • Most standard swing components can be used as menu items
  • Index-accessable
//FileMenu.java
import java.awt.event.*;
import javax.swing.*;

public class FileMenu extends JMenu {
  public FileMenu(MiniActions actions) {
    super("File");

    add(actions.getAction("new"));
    add(actions.getAction("open"));
    addSeparator();
    add(actions.getAction("quit"));
  }
}

Popup Menus

  • First introduced in the 1.1 version of the AWT
  • Activated by a platform-dependant trigger:
public void processMouseEvent(MouseEvent e) {
  if (e.isPopupTrigger()) {
    popup.show(this, e.getX(), e.getY());
  } else {
    super.processMouseEvent(e);
  }
}
  • One for our MiniEditor:
//MiniPopup.java
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.event.*;

public class MiniPopup extends JPopup implements MouseListener {
  MiniEdit theApp;

  public MiniPopup(MiniEdit theApp) {
    this.theApp = theApp;
    MiniActions actions = theApp.getActions();

    add(actions.get("open"));
    add(actions.get("new"));
  }

  public void mousePressed(MouseEvent e) { checkPopup(e); }
  public void mouseClicked(MouseEvent e) { checkPopup(e); }
  public void mouseEntered(MouseEvent e) { }
  public void mouseExited(MouseEvent e) { }
  public void mouseReleased(MouseEvent e) { checkPopup(e); }

  private void checkPopup(MouseEvent e) {
    if (e.isPopupTrigger()) {
      show(theApp, e.getX(), e.getY());
    }
  }
}

Toolbars

  • "A container for various components to be added"
  • Can be "floated" from its container
  • You can add Components other than buttons to a toolbar (labels, lists, etc)
//MiniToolBar.java
import java.awt.event.*;
import javax.swing.*;

public class MiniToolBar extends JToolBar {
  public MiniToolBar(MiniActions actions) {
    add(actions.getAction("new"));
    add(actions.getAction("open"));
  }
}

Documents >>