Creating a Dialog Box

Creating a Dialog Box

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.

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);  ;         }         // …      }      // …   } 
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 ( == Event.WINDOW_DESTROY && == 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.

Share the Post:
data observability

Data Observability Explained

Data is the lifeblood of any successful business, as it is the driving force behind critical decision-making, insight generation, and strategic development. However, due to its intricate nature, ensuring the

Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved