Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Web Development
Expertise: Beginner
Mar 21, 1997

Creating a Message Window

Question:
I have a note section on my Web page. I would like to be able to click a radio button, and a small window with the note inside it would pop up. Can you help me?

Answer:
There are several ways to do this. At issue is what sort of object the message window should be. Four candidates suggest themselves: frame, dialog, text area, and canvas. The code below shows all four.

Note that the text area and canvas are confined to the viewing area of their parent frame, while the dialog and frame are not. Also, only the dialog can be made modal (i.e. unignorable).

Although the dialog implementation is probably the most standard, getting a dialog box to appear inside an applet is awkward because a dialog must have a frame parent.

import java.awt.*;

////////////////////////////////////////////////////////////////////

class FrameMessage extends Frame {

   private String msg; // message to be displayed

   public FrameMessage(String s) {
      setTitle("A Message in a Frame");
      msg = s;
   }

   public boolean handleEvent(Event e) {
      if(e.id == Event.WINDOW_DESTROY) dispose();
      return super.handleEvent(e);
   }

   public void paint(Graphics g) {
      g.drawString(msg, 20, 40);
   }

}

////////////////////////////////////////////////////////////////////

class DialogMessage extends Dialog {

   private String msg; // message to be displayed

   public DialogMessage(Frame parent, String s) {
      super(parent, "A Message in a Dialog Box", false);
      msg = s;
   }

   public boolean handleEvent(Event e) {
      if(e.id == Event.WINDOW_DESTROY && e.target == this) dispose();
      return super.handleEvent(e);
   }

   public void paint(Graphics g) {
      g.drawString(msg, 20, 40);
   }

}

////////////////////////////////////////////////////////////////////

class CanvasMessage extends Canvas {

   private String msg; // message to be displayed

   public CanvasMessage(String s) {
      msg = s;
      setBackground(Color.green);
      resize(200, 300);
   }

   public void paint(Graphics g) {
      g.drawString(msg, 20, 40);
   }

   // mouse events are always routed to canvasses!
   public boolean mouseUp(Event e, int x, int y) {
      hide();
      return true;
   }

}

////////////////////////////////////////////////////////////////////

public class Msg extends Frame {

   /* A radio button is a mutually exclusive checkbox; in other words,
      a checkbox which when checked, automatically unchecks other
      checkboxes in its group.
   */
   private Checkbox frameButton, dialogButton, textButton, canvasButton;
   private CheckboxGroup buttons;

   // our message
   private String text = "A squid eating dough";

   // our message boxes
   FrameMessage frameMsg;
   DialogMessage dialogMsg;
   TextArea textMsg;
   CanvasMessage canvasMsg;

   public Msg() {
      setTitle("Message Demos");
      setLayout(new FlowLayout());
      buttons = new CheckboxGroup();
      // radio buttons = grouped checkboxes
      add(frameButton = new Checkbox("Frame", buttons, false));
      add(dialogButton = new Checkbox("Dialog", buttons, false));
      add(textButton = new Checkbox("Text Area", buttons, false));
      add(canvasButton = new Checkbox("Canvas", buttons, false));
   }

   private void destroyAll() {
      if (frameMsg != null) frameMsg.dispose();
      if (dialogMsg != null) dialogMsg.dispose();
      if (textMsg != null) textMsg.hide();
      if (canvasMsg != null) canvasMsg.hide();
   }

   public boolean action(Event e, Object arg) {
      if (e.target instanceof Checkbox)
         if (frameButton.getState()) {
            destroyAll();
            frameMsg = new FrameMessage(text);
            frameMsg.resize(300, 300);
            frameMsg.show();
         }
         else if (dialogButton.getState()) {
            destroyAll();
            dialogMsg = new DialogMessage(this, text);
            dialogMsg.resize(300, 300);
            dialogMsg.show();
         }
         else if (textButton.getState()) {
            destroyAll();
            textMsg = new TextArea(text, 10, 20);
            textMsg.setEditable(false);
            add(textMsg);
            show();
         }
         else if (canvasButton.getState()) {
            destroyAll();
            canvasMsg = new CanvasMessage(text);
            add(canvasMsg);
            canvasMsg.show();
         }
      return true;
   }

   public boolean handleEvent(Event e) {
      if(e.id == Event.WINDOW_DESTROY && e.target == this)
System.exit(0);
      return super.handleEvent(e);
   }

   public static void main() {
      Frame f = new Msg();
      f.resize(600, 300);
      f.show();
   }
}
DevX Pro
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date