Adv Java - [Swing: JDialog Class]

♠ Posted by Unknown in at 00:22

JDialog Class


A dialog box is a window that pops up out of another window. Dialog boxes are heavily used in windowed programming environments, but less frequently used in applets.




To create a dialog box, you inherit from JDialog, which is just another kind of Window, like a JFrame. A JDialog has a layout manager (which defaults to BorderLayout) and you add event listeners to deal with events. One significant difference when windowClosing() is called is that you don’t want to shut down the application. Instead, you release the resources used by the dialog’s window by calling dispose().

Example:

/* <APPELT CODE = "JDialogExample.CLASS" WIDTH = 500 HEIGHT = 500>,/APPLET>
 */
 import javax.swing.*;
 import java.awt.*;
 import java.awt.event.*;
 class MyDialog extends JDialog implements ActionListener
 {
          public MyDialog(JFrame parent)
          {
                   super(parent, "My Dialog...", true);
                   Container contentPane = getContentPane();
                   contentPane.setLayout(new FlowLayout());
                   contentPane.add(new JLabel("Here is my Dialog Box."));                  
                   JButton ok = new JButton("Ok");
                   ok.addActionListener(this);
                   contentPane.add(ok);
                   setSize(150,125);
          }
          public void actionPerformed(ActionEvent ae)
          {
                   dispose();
          }
 }

public class JDialogExample extends JApplet implements ActionListener
{
Image: Swing JDialog Example          JButton jb = new JButton("Dialog Box");
          MyDialog dlg = new MyDialog(null);
         
          public void init()
          {
                   jb.addActionListener(this);
                   getContentPane().add(jb);
          }
         
          public void actionPerformed(ActionEvent ae)
          {
                   dlg.show();  
          }
}

0 comments:

Post a Comment