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: AWT
Expertise: Beginner
Mar 20, 1997

Creating a Dialog Box

Question:
I've been to sites where dialog boxes appear. How can I create one? I also want to ask users for their names, and then to be able to tell how many times they have visited my site.

Answer:
Creating a dialog box in Java is fairly straightforward if you aren't interested in a dialog so much as a monolog.

Let's begin by looking at a schematic of a Frame subclass declaration. An instance of Demo contains a button marked "Info." Clicking it creates and displays an instance of the class InfoDialog. Notice the Demo instance itself is passed to the InfoDialog constructor:

   public class Demo extends Frame {

      public Demo() {
         // ...
         add(new Button("Info"));
         // ...
      }

      public boolean action(Event e, Object arg) {
         // ...
         if (arg.equals("Info")) {
            InfoDialog id = new InfoDialog(this);
            id.show();
         }
         // ...
      }

      // ...
   }

 
To create the InfoDialog class, we extend Java's Dialog class:
 class InfoDialog extends Dialog { ... } 
The constructor for this class expects the parent frame passed as a parameter:
   public InfoDialog(Frame parent) {

      super(parent, "Here's your Info", true);
      add(new label("2 + 2 = 4"));
      add(new Button("Okay"));
      resize(200, 300); // important!

   }
Our constructor first constructs a Dialog base class object using super. The expected parameters are the parent frame, the title of the dialog box, and a boolean. If the boolean parameter is true, then the dialog box will be modal (nothing happens until the user gets rid of the dialog box). If the boolean parameter is false, the dialog box will be modeless (the user can go back to playing with the frame the box came from without closing the box).

When the "OK" button is clicked, all resources allocated to the dialog box are disposed of, and the box disappears.

One last problem. If the user tries to close the dialog box using the close box, this will cause the entire frame to disappear. So InfoDialog must handle this event specifically with:

   public boolean handleEvent(Event e) {
      if (e.id == Event.WINDOW_DESTROY && e.target == this)
         System.exit(0);
      return super.handleEvent(e);
   }
Getting your dialog box to send information typed into it by the user back to its parent frame is a little tricky because this information is disposed of when the dialog box is closed, and because the frame is stuck while the dialog box is open (if it's modal). One solution is to add a "Send" button to your dialog box. After the user types some information into a text field, he/she clicks the "Send" button, and the dialog box's action handler explicitly calls a function in the parent frame, passing it the content of the text field:
   public boole action(Event e, Object arg) {
      // ...
      if (arg.equals("Send"))
         ((Demo)getParent()).infoHandler(nameField.getText());
      // ...
   }
This method is a little lame since the dialog box must know that its parent is a Demo frame. Consult a good Java book for other techniques.

Now for your last question. If getting a dialog box to send information back to its parent frame is tricky, getting it to send information back to the Web server is downright heroic.

Suppose you want to keep track of the names of all visitors to your Web page. When someone views the page, a Java program is downloaded to that person's computer, which pops up a dialog box asking for his/her name. After the name is entered, the Java program must send the name back to the Web server. The Web server must then call another program written by you to process the name.

Fortunately, there is a protocol for doing this sort of thing. The program on the server side that processes the name (e.g. enters it into a database) is called a CGI script. Conceivably, CGI scripts can be Java applications, but more conventionally they are written in Perl. Of course you need to get your system administrator to agree to let you keep CGI scripts on the server.

Most sites have simple CGI scripts you can invoke from your HTML file that will insert into the downloaded Web page the number of times it has been hit, when it was last updated, etc. Ask your system admin for details, and consult a good book on CGI for all the gnarly details about writing CGI scripts.

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