Creating the Computer Information Dialog Box


The CompInfoDialog.java file creates a dialog box that displays the information of the network computer.

Listing 7-4 shows the contents of the CompInfoDialog.java file:

Listing 7-4: The CompInfoDialog.java File
start example
 /* Import java packages */ import javax.swing.*; import java.awt.*; import java.awt.event.*; /* Class CompInfoDialog - This class creates a computer information dialog box. Fields:    cont - Creates the container.    key_lbl - Creates the label for "Echo Time" or "Date/Time".    value_lbl - Creates the label to display the result. Methods:    actionPerformed() - This method is invoked when end user clicks the OK button. */ public class CompInfoDialog extends JDialog implements ActionListener {    /* Declares the object of the Container class. */    Container cont = null;    /* Declares the object of the JLabel class. */    JLabel key_lbl;    JLabel value_lbl;    /* Declares the object of the JButton class. */    JButton ok_btn;    /* Defines the default constructor.   */    public CompInfoDialog(NetCompFrame parent, String title, boolean modal, int port, String value)    {       super(parent, title, modal);       /* Set the size of the Computer Information dialog box. */       setSize(200, 140);       /* Creates the object of the Point class to get the parent frame location. */       Point p = parent.getLocation();       /* Set the location of the dialog box. */       setLocation((int)p.getX()+10, (int)p.getY()+10);       /*       addWindowListener - It contains the windowClosing() method.       windowClosing: It is called when the user clicks the cancel button of the Window.        It closes the main window.       Parameter: we- Object of WindowEvent class.       Return Value: NA       */       addWindowListener(new WindowAdapter()       {          public void windowClosing(WindowEvent we)          {             dispose();          }       });       cont = this.getContentPane();       /* Sets the layout of the application to null. */       cont.setLayout(null);       /* This section is executed, if port number equals to 13. */       if (port == 13)       {          /* Initialize the label. */          key_lbl = new JLabel("Date and Time", SwingConstants.CENTER);       }       /* This section is executed, if port number equals to 7. */          else       {          /* Initialize the label. */          key_lbl = new JLabel("Echo Time", SwingConstants.CENTER);       }       /* Initializes the object of the JLabel class */       value_lbl = new JLabel(value, SwingConstants.CENTER);       /* Initializes the object of the JButton class */       ok_btn = new JButton("Ok");       /* Adds the actionListener event to the ok button. */       ok_btn.addActionListener(this);       /* Sets the position of the swing components on the container. */          key_lbl.setBounds(0, 10, 200, 20);       value_lbl.setBounds(0, 40, 200, 20);       ok_btn.setBounds(70, 70, 60, 20);       /* Adds the components to the container. */       cont.add(key_lbl);       cont.add(value_lbl);       cont.add(ok_btn);       show();    }    /*    actionPerformed() - This method is called when the user selects any menu item from the menu bar.    Parameters:   ae - an ActionEvent object containing details of the event.    Return Value: NA    */    public void actionPerformed(ActionEvent ae)    {       if (ae.getSource() == ok_btn)       {          dispose();       }    } } 
end example
 

Download this Listing .

In the above code, the constructor of the CompInfoDialog class reads the object of the NetCompFrame class, title, model, port number, and value. Here, title represents a string that sets the title, model represents the Boolean value, and value represents the string that contains the network information.

When an end user clicks the OK button in the Computer Information dialog box, the actionPerformed() method is invoked. This method calls the dispose() method to close the dialog box.




Java InstantCode. Developing Applications Using Java NIO
Java InstantCode. Developing Applications Using Java NIO
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 55

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net