Home
Categories
Dictionary
Download
Project Details
Changes Log
Tutorials
FAQ
License

Message area tutorial



Overview

The message area tutorial is a simple application derived from the basic tutorial which uses the Message area. With this example, we will learn to:
  • Remove the default text when performing an action
  • Use the message area with simple text
  • Use the message area with styled text

Creation of the Application

You only need to copy your basic tutorial project, as we will do very few modifications on its code. We don't need to change anything from the basic tutorial project for the Menu factory.

Modifying the Plugin

We will do very few change from the initial code for our Plugin.

Initial behavior

Let's see the initial code:
      public class ImportImageAction extends AbstractMDIAction {
        public ImportImageAction(String name) {
          super(app, name);
          this.setDescription("Open Image", "Open Image");
        }

        public void run() throws Exception {
          JFileChooser chooser = new JFileChooser("Open Image");
          chooser.setDialogTitle("Open Image");
          chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
          if (chooser.showOpenDialog(app.getApplicationWindow()) == JFileChooser.APPROVE_OPTION) {
            File file = chooser.getSelectedFile();
            BufferedImage image = ImageIO.read(file);
            if (image == null) throw new Exception("Bad File type");
            JScrollPane pane = new JScrollPane(new ImagePanel(image));
            app.addTab(pane, image, file.getName());
          }
        }
      }
In this example, when we open an Image file, we have the automatic message in the Message area:
defaultactiontext
The text of the message uses the description of the action.

If the file is invalid (for example, not an image file), we just throw an exception, and the default Exception panel pops up:
exceptionpanel

Modified behavior

We will change two things:
  • We won't use the default action message anymore when opening an image, but we will show a custom message in the Message area, with a green color
  • If we have an exception, we won't rely on the default error panel, but we will show a red error in the area
The modified code is:
      public class ImportImageAction extends AbstractMDIAction {
        public ImportImageAction(String name) {
          super(app, name);
          this.setDescription("Open Image", "Open Image");
        }

        public void run() throws Exception {
          ((GUIApplication) app).noWriteMessages();
          JFileChooser chooser = new JFileChooser("Open Image");
          chooser.setDialogTitle("Open Image");
          chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
          if (chooser.showOpenDialog(app.getApplicationWindow()) == JFileChooser.APPROVE_OPTION) {
            File file = chooser.getSelectedFile();
            BufferedImage image = ImageIO.read(file);
            if (image == null) {
                 ((GUIApplication) app).getMessageArea().append("Error when Opening " + file.getName(), "red", MessageLogger.DEFAULT_FONT_SIZE, MessageLogger.BOLD);
               } else {
                 JScrollPane pane = new JScrollPane(new ImagePanel(image));
                 gui.addTab(pane, image, file.getName());
                 ((GUIApplication) app).getMessageArea().append("Opened " + file.getName(), "green");
               }
          }
        }
      }
In this code:
  • The (GUIApplication) app).noWriteMessages(); code ensures that we won't have the default message for the action
  • The ((GUIApplication) app).getMessageArea().append("Opened " + file.getName(), "green"); code shows the message in green whe we open an Image
  • The ((GUIApplication) app).getMessageArea().append("Error when Opening " + file.getName(), "red", MessageLogger.DEFAULT_FONT_SIZE, MessageLogger.BOLD); code shows a red message in Bold if we can't open the image
Now when we open an image, we have:
greenareatext
And when we have an error when trying to open an image, we have:
redareatext

See also


Categories: swing | tutorials

Copyright 2006-2023 Herve Girod. All Rights Reserved. Documentation and source under the LGPL v2 and Apache 2.0 licences