devxlogo

Creating a Dialog Box

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 timesthey have visited my site.

Answer:
Creating a dialog box in Java is fairly straightforward if youaren’t interested in a dialog so much as a monolog.

Let’s begin by looking at a schematic of a Frame subclassdeclaration. An instance of Demo contains a button marked”Info.” Clicking it creates and displays aninstance of the class InfoDialog. Notice the Demo instanceitself 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 passedas 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 classobject using super. The expected parameters are theparent frame, the title of the dialog box, and a boolean.If the boolean parameter is true, then the dialog box willbe modal (nothing happens until the user gets rid of thedialog box). If the boolean parameter is false, thedialog box will be modeless (the user can go back toplaying with the frame the box came from without closingthe box).

When the “OK” button is clicked, allresources allocated to the dialog box are disposed of, andthe box disappears.

One last problem. If the user tries to close the dialogbox using the close box, this will cause the entire frameto disappear. So InfoDialog must handle this event specificallywith:

   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 intoit by the user back to its parent frame is a little trickybecause this information is disposed of when the dialog boxis closed, and because the frame is stuck while thedialog box is open (if it’s modal). One solution is toadd a “Send” button to your dialog box. After the usertypes some information into a text field, he/she clicksthe “Send” button, and the dialog box’s action handlerexplicitly calls a function in the parent frame, passing itthe 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 knowthat its parent is a Demo frame. Consult a good Java bookfor other techniques.

Now for your last question. If getting a dialog box to sendinformation back to its parent frame is tricky, getting it tosend 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, whichpops up a dialog box asking for his/her name. After the name is entered,the Java program must send the name back tothe Web server. The Web server must then call anotherprogram 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 conventionallythey are written in Perl. Of course you need to get your systemadministrator to agree to let you keep CGI scripts on the server.

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

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist