Easy to Learn Java: Programming Articles, Examples and Tips

Start with Java in a few days with Java Lessons or Lectures

Home

Code Examples

Java Tools

More Java Tools!

Java Forum

All Java Tips

Books

Submit News
Search the site here...
Search...
 

Swing Chapter 12 (The basics) Menus, Toolbars, and Actions. Easy for reading, Click here!

Custom Search
Swing Chapter 12 (The basics) Menus, Toolbars, and Actions. Easy for reading, Click here!

[ Return to Swing (Book) ]

Page: 2/5 



Previous Page Previous Page (1/5) - Next Page (3/5) Next Page
Subpages: 1. JMenus, Toolbars, and Actions overview 
2.
Basic text editor: part I - menus 
3.
Basic text editor: part II - Toolbars and Actions 
4. Basic text editor: part III - Custom toolbar components 
5. Basic text editor: part IV- Custom menu components 

12.2  Basic text editor: part I - menus

In this example we begin the construction of a basic text editor application using a menu bar and several menu items. The menu bar contains two JMenus labeled "File" and "Font." The "File" menu contains JMenuItems for creating a new (empty) document, opening a text file, saving the current document as a text file, and exiting the application. The "Font" menu contains JCheckBoxMenuItems for making the document bold and/or italic, as well as JRadioButtonMenuItems organized into a ButtonGroup allowing selection of a single font.

Figure 12.1 JMenu containing JMenuItems with mnemonics and icons.

<<file figure12-1.gif>>

Figure 12.2 JMenu containing JRadioButtonMenuItems and JCheckBoxMenuItems.

<<file figure12-2.gif>>

The Code: BasicTextEditor.java

see \Chapter12\1

import java.awt.*;

import java.awt.event.*;

import java.io.*;

import java.util.*;

import javax.swing.*;

import javax.swing.event.*;

public class BasicTextEditor extends JFrame

{

  public static final String FONTS[] =

    { "Serif", "SansSerif", "Courier" };

  protected Font m_fonts[];

  protected JTextArea m_monitor;

  protected JMenuItem[] m_fontMenus;

  protected JCheckBoxMenuItem m_bold;

  protected JCheckBoxMenuItem m_italic;

  protected JFileChooser m_chooser;

  public BasicTextEditor(){

    super("Basic text editor: part I - Menus");

    setSize(450, 350);

    m_fonts = new Font[FONTS.length];

    for (int k=0; k<FONTS.length; k++)

      m_fonts[k] = new Font(FONTS[k], Font.PLAIN, 12);

    m_monitor = new JTextArea();

    JScrollPane ps = new JScrollPane(m_monitor);

    getContentPane().add(ps, BorderLayout.CENTER);

    m_monitor.append("Basic text editor");

    JMenuBar menuBar = createMenuBar();

    setJMenuBar(menuBar);

    m_chooser = new JFileChooser();

    m_chooser.setCurrentDirectory(new File("."));

    WindowListener wndCloser = new WindowAdapter() {

      public void windowClosing(WindowEvent e) {

        System.exit(0);

      }

    };

    addWindowListener(wndCloser);

    updateMonitor();

    setVisible(true);

  }

  protected JMenuBar createMenuBar() {

    final JMenuBar menuBar = new JMenuBar();

    JMenu mFile = new JMenu("File");

    mFile.setMnemonic('f');

    JMenuItem item = new JMenuItem("New");

    item.setIcon(new ImageIcon("file_new.gif"));

    item.setMnemonic('n');

    ActionListener lst = new ActionListener() {

      public void actionPerformed(ActionEvent e) {

        m_monitor.setText("");

      }

    };

    item.addActionListener(lst);

    mFile.add(item);

    item = new JMenuItem("Open...");

    item.setIcon(new ImageIcon("file_open.gif"));

    item.setMnemonic('o');

    lst = new ActionListener() {

      public void actionPerformed(ActionEvent e) {

        BasicTextEditor.this.repaint();

        if (m_chooser.showOpenDialog(BasicTextEditor.this) !=

         JFileChooser.APPROVE_OPTION)

          return;

        Thread runner = new Thread() {

          public void run() {

            File fChoosen = m_chooser.getSelectedFile();

            try {

              FileReader in = new FileReader(fChoosen);

              m_monitor.read(in, null);

              in.close();

            }

            catch (IOException ex) { ex.printStackTrace(); }

          }

        };

        runner.start();

      }

    };

    item.addActionListener(lst);

    mFile.add(item);

    item = new JMenuItem("Save...");

    item.setIcon(new ImageIcon("file_save.gif"));

    item.setMnemonic('s');

    lst = new ActionListener() {

      public void actionPerformed(ActionEvent e){

        BasicTextEditor.this.repaint();

        if (m_chooser.showSaveDialog(BasicTextEditor.this) !=

         JFileChooser.APPROVE_OPTION)

          return;

        Thread runner = new Thread() {

          public void run() {

            File fChoosen = m_chooser.getSelectedFile();

            try {

              FileWriter out = new FileWriter(fChoosen);

              m_monitor.write(out);

              out.close();

            }

            catch (IOException ex) { ex.printStackTrace(); }

          }

        };

        runner.start();

      }

    };

    item.addActionListener(lst);

    mFile.add(item);

    mFile.addSeparator();

    item = new JMenuItem("Exit");

    item.setMnemonic('x');

    lst = new ActionListener() {

      public void actionPerformed(ActionEvent e) {

        System.exit(0);

      }

    };

    item.addActionListener(lst);

    mFile.add(item);

    menuBar.add(mFile);

    ActionListener fontListener = new ActionListener() {

      public void actionPerformed(ActionEvent e) {

        updateMonitor();

      }

    };

    JMenu mFont = new JMenu("Font");

    mFont.setMnemonic('o');

    ButtonGroup group = new ButtonGroup();

    m_fontMenus = new JMenuItem[FONTS.length];

    for (int k = 0; k < FONTS.length; k++) {

      int m = k+1;

      m_fontMenus[k] = new JRadioButtonMenuItem(

        m + " " + FONTS[k]);

      boolean selected = (k == 0);

      m_fontMenus[k].setSelected(selected);

      m_fontMenus[k].setMnemonic('1' + k);

      m_fontMenus[k].setFont(m_fonts[k]);

      m_fontMenus[k].addActionListener(fontListener);

      group.add(m_fontMenus[k]);

      mFont.add(m_fontMenus[k]);

    }

    mFont.addSeparator();

    m_bold = new JCheckBoxMenuItem("Bold");

    m_bold.setMnemonic('b');

    Font fn = m_fonts[1].deriveFont(Font.BOLD);

    m_bold.setFont(fn);

    m_bold.setSelected(false);

    m_bold.addActionListener(fontListener);

    mFont.add(m_bold);

    m_italic = new JCheckBoxMenuItem("Italic");

    m_italic.setMnemonic('i');

    fn = m_fonts[1].deriveFont(Font.ITALIC);

    m_italic.setFont(fn);

    m_italic.setSelected(false);

    m_italic.addActionListener(fontListener);

    mFont.add(m_italic);

    menuBar.add(mFont);

    return menuBar;

  }

  protected void updateMonitor() {

    int index = -1;

    for (int k = 0; k < m_fontMenus.length; k++) {

      if (m_fontMenus[k].isSelected()) {

        index = k;

        break;

      }

    }

    if (index == -1)

      return;

    if (index == 2) {  // Courier

      m_bold.setSelected(false);

      m_bold.setEnabled(false);

      m_italic.setSelected(false);

      m_italic.setEnabled(false);

    }

    else {

      m_bold.setEnabled(true);

      m_italic.setEnabled(true);

    }

    int style = Font.PLAIN;

    if (m_bold.isSelected())

      style |= Font.BOLD;

    if (m_italic.isSelected())

      style |= Font.ITALIC;

    Font fn = m_fonts[index].deriveFont(style);

    m_monitor.setFont(fn);

    m_monitor.repaint();

  }

  public static void main(String argv[]) {

    new BasicTextEditor();

  }

}

Understanding the Code

Class BasicTextEditor

This class extends JFrame and provides the parent frame for our example. One class variable is declared:

String FONTS[]: an array of font family names.

Instance variables:

Font[] m_fonts: an array of Font instances which can be used to render our JTextArea editor.

JTextArea m_monitor: used as our text editor.

JMenuItem[] m_fontMenus: an array of menu items representing available fonts.

JCheckBoxMenuItem m_bold: menu item which sets/unsets the bold property of the current font.

JCheckBoxMenuItem m_italic: menu item which sets/unsets the italic property of the current font.

JFileChooser m_chooser: used to load and save simple text files.

The Menu1 constructor populates our m_fonts array with Font instances corresponding to the names provided in FONTS[]. The m_monitor JTextArea is then created and placed in a JScrollPane. This scroll pane is added to the center of our frame's content pane and we append some simple text to m_monitor for display at startup. Our createMenuBar() method is called to create the menu bar to manage this application, and this menu bar is then added to our frame using the setJMenuBar() method.

The createMenuBar() method creates and returns a JMenuBar. Each menu item receives an ActionListener to handle it's selection. Two menus are added titled "File" and "Font." The "New" menu item in the  "File" menu is responsible for creating a new (empty) document. It doesn't really replace JTextArea's Document. Instead it simply clears the contents of our editor component. Note that an icon is used in this menu item. Also note that this menu item can be selected with the keyboard by pressing 'n' when the "File" menu's popup is visible, because we assigned it 'n' as a mnemonic. The File menu was also assigned a mnemonic character, 'f,' and pressing ALT-F while the application frame is active, the "File" popup will be displayed allowing navigation with either the mouse or keyboard (all other menus and menu items in this example also receive appropriate mnemonics).

Bug Alert! Even though ALT-F will get processed by the corresponding menu in the menu bar, the key event that is generated is still received by JTextArea. Thus, you will often see an 'f' added to the text area even though ALT was held down when 'f' was pressed. It is possible to work around this problem by extending JTextArea (or any other JTextComponent) as follows:

public class MyTextArea extends JTextArea

{

  protected synchronized void processComponentKeyEvent(

   KeyEvent anEvent) {

    super.processComponentKeyEvent(anEvent);

    ivLastKeyEventWasAlt = anEvent.isAltDown();

  }

  protected synchronized void processInputMethodEvent(

   InputMethodEvent e) {

    if (ivLastKeyEventWasAlt) {

      e.consume();

    }

    super.processInputMethodEvent(e);

  }

  private transient boolean ivLastKeyEventWasAlt = false;

}

Note that this workaround was orginally posted to the JDC Bug Parade and it should only be considered a temporary solution until the problem is actually fixed.

The "Open" menu item brings up our m_chooser JFileChooser component (discussed in chapter 14) to allow selection of a text file to open. Once a text file is selected, we open a FileReader on it and invoke read() on our JTextArea component to read the file's content (which creates a new PlainDocument containing the selected file's content to replace the current JTextArea document--see chapter 11). The "Save" menu item brings up m_chooser to select a destination and file name to save the current text to. Once a text file is selected, we open a FileWriter on it and invoke write() on our JTextArea component to write its content to the destination file. Both of these I/O operations are wrapped in separate threads to avoid clogging up the event dispatching thread.

Bug Alert! We have added code to repaint the whole application before the I/O operations occur. This is because when a Swing menu is hidden by a dialog (in this case a JFileChooser dialog) that pops up immediately in response to a menu selection, the menu will often not perform all necessary repainting of itself and components that lie below its popup. To make sure that no remnants of menu rendering are left around we force a repaint of the whole frame before moving on.

The "Exit" menu item terminates program execution. It is separated from the first three menu items with a menu separator to create a more logical display.

The "Font" menu consists of several menu items used to select the font and font style used in our editor. All of these items receive the same ActionListener which invokes our updateMonitor() method (see below). To give the user an idea of how each font looks, each font is used to render the corresponding menu item text. Since only one font can be selected at any given time, we use JRadioButtonMenuItems for these menu items, and add them all to a ButtonGroup instance which manages a single selection.

To create each menu item we iterate through our FONTS array and create a JRadioButtonMenuItem corresponding to each entry. Each item is set to unselected (except for the first one),  assigned a numerical mnemonic corresponding to the current FONTS array index, assigned the appropriate Font instance for rendering its text, assigned our multi-purpose ActionListener, and added to our ButtonGroup along with the others.

The two other menu items in the "Font" menu manage the bold and italic font properties. They are implemented as JCheckBoxMenuItems since these properties can be selected or unselected independently. These items also are assigned the same ActionListener as the radio button items to process changes in their selected state.

The updateMonitor() method updates the current font used to render the editing component by checking the state of each check box item and determining which radio button item is currently selected. The m_bold and m_italic components are disabled and unselected if the Courier font is selected, and enabled otherwise. The appropriate m_fonts array element is selected and a Font instance is derived from it corresponding to the current state of the check box items using Font's deriveFont() method (see chapter 2).

Note: Surprisingly the ButtonGroup class does not provide a direct way to determine which component is currently selected. So we have to examine m_fontMenus array elements in turn to determine the selected font index. Alternatively we could save the font index in an enhanced version of our ActionListener.

Running the Code

Open a text file, make some changes, and save it as a new file. Change the font options and note how the text area is updated. Select the Courier font and note how it disables the bold and italic check box items (it also unchecks them if they were previously checked). Select another font and note how this re-enables check box items. Figure 12.1 shows BasicTextEditor's "File" menu, and figure 12.2 shows the "Font" menu. Note how the mnemonics are underlined and the images appear to the right of the text by default (just like buttons).

UI Guideline : File oriented applications

This is an example of a menu being used on a file oriented application. Menus were first developed to be used in this fashion. The inclusion of a menu on such an application is essential, as users have come to expect one. There are clearly defined platform standards for the layout of such a menu and it is best that you ahere to these. Almost always the "File" menu comes first (to the LHS).

Note also the use of the elipsis "..." on the "Open..." and "Save..." options. This is a standard technique which gives a visual affordance to the behaviour that a dialog will open when the menu item is selected.

UI Guideline : Correct use of separator and widget overloading This example shows clearly how a menu in a simple application can have selection controls added to speed operation and ease usability. The separator is used to group and separate the selection of the font type from the font style.



[ Return to Swing (Book) ]


Top 10 read Java Articles
 Get free "1000 Java Tips eBook"

 Java Calendar and Date: good to know facts and code examples

 Array vs ArrayList vs LinkedList vs Vector: an excellent overview and examples

 How can I convert any Java Object into byte array? And byte array to file object

 The Java Lesson 1: What is Java?

 How do I compare two dates and times, date between dates, time between times and

 Maven vs Ant or Ant vs Maven?

 How to open, read, write, close file(s) in Java? Examples on move, rename and de

 Java Array

 Java: JLabel font and color


[ More in News Section ]
Java Lessons

The Java Lesson 1:
What is Java?
The Java Lesson 2:
Anatomy of a simple Java program
The Java Lesson 3:
Identifiers and primitive data types
The Java Lesson 4:
Variables, constants, and literals
The Java Lesson 5:
Arithmetic operations, conversions, and casts
The Java Lesson 6:
Boolean expressions and operations
The Java Lesson 7:
Bitwise operations
The Java Lesson 8:
Flow control with if and else
The Java Lesson 9:
switch statements
The Java Lesson 10:
for, while, and do-while statements
The Java Lesson 11:
Using break and continue
The Java Lesson 12:
Class methods and how they are called
The Java Lesson 13:
Using the Math class
The Java Lesson 14:
Creating and calling custom class methods
The Java Lesson 15:
Overloading class methods
The Java Lesson 16:
An introduction to objects and object references
The Java Lesson 17:
The String class
The Java Lesson 18:
The StringBuffer class
The Java Lesson 19:
Initializing and processing arrays of primitives
The Java Lesson 20:
Initializing and processing arrays of objects
The Java Lesson 23:
Inheritance and overriding inherited methods
The Java Lesson 24:
abstract classes and polymorphism
The Java Lesson 25:
Interfaces, instanceof, and object conversion and casting
The Java Lesson 26:
Introduction to graphical programming and the java.awt packa
The Java Lesson 27:
The Component class
The Java Lesson 28:
Containers and simple layout managers
The Java Lesson 29:
The Color and Font classes
The Java Lesson 30:
Drawing geometric shapes
The Java Lesson 31:
Choice, List, and Checkbox controls
The Java Lesson 32:
Using the Scrollbar graphical control
The Java Lesson 33:
Menus and submenus
The Java Lesson 34:
An introduction to applets and the Applet class
The Java Lesson 35:
Essential HTML to launch an applet and pass it parameters
The Java Lesson 36:
Mouse event processing
Java Lesson 37:
Menus and submenus
Java Lesson 38:
The WindowListener interface and the WindowAdapter class
Java Lesson 39:
An introduction to GridBagLayout
Java Lesson 40:
An introduction to the Java Collections API
Java Lesson 41:
Exception handling with try, catch, and finally blocks
Java Lesson 42:
Claiming and throwing exceptions
Java Lesson 43:
Multithreading, the Thread class, and the Runnable interface
Java Lesson 44:
An introduction to I/O and the File and FileDialog classes
Java Lesson 45:
Low-level and high-level stream classes
Java Lesson 46:
Using the RandomAccessFile class
Java Lessons by
Joh Huhtala: Update

Latest articles
 Java Profiler JProbe to Resolve Performance Problems Faster

 SSL with GlassFish v2, page 5

 SSL with GlassFish v2, page 4

 SSL with GlassFish v2, page 3

 SSL with GlassFish v2, page 2

 The Java Lesson 2: Anatomy of a simple Java program, page 2

 New site about Java for robots and robotics: both software and hardware.

 Exceptions -III: What's an exception and why do I care?

 Exceptions -II: What's an exception and why do I care?

 Exceptions: What's an exception and why do I care?

 Double your Java code quality in 10 minutes, here is receipt

 Murach's Java Servlets and JSP

 How to get ascii code from a char in Java?

 Can we just try without catch? Yes!

 Make Tomcat page load faster

 Make your Tomcat More secure - limit network address for certain IP addresses

 New Java book online starts now here...

 Implementing RESTful Web Services in Java

 Firefox trimming from 1 GB to 40 Mb with many tabs opened

 SSL with GlassFish v2

 My request to replublish Tech Tips

 Search JavaFAQ.nu site here

 New Advanced Installer for Java 6.0 brings XML updates and imports 3rd party MSI

 EJB programming restrictions

 Maven vs Ant or Ant vs Maven?

 Why Java does not use default value which it should?

 How to unsign signed bytes in Java - your guide is here

 The Java Lesson 3: Identifiers and primitive data types. Page 2

 The Java Lesson 7: Bitwise operations with good examples, click here! Page 4

 The Java Lesson 7: Bitwise operations with good examples, click here! Page 3


[ More in News Section ]


Home Code Examples Java Forum All Java Tips Books Submit News, Code... Search... Offshore Software Tech Doodling

RSS feed Java FAQ RSS feed Java FAQ News     

    RSS feed Java Forums RSS feed Java Forums

All logos and trademarks in this site are property of their respective owner. The comments are property of their posters, all the rest 1999-2006 by Java FAQs Daily Tips.

Interactive software released under GNU GPL, Code Credits, Privacy Policy