Tuesday 28 March 2017

Dialog Boxes & FileDialog - Java Tutorials

Dialog Boxes

Often, you will want to use a dialog box to hold a set of related controls. Dialog boxes are primarily used to obtain user input. They are similar to frame windows, except that dialog boxes are always child windows of a top-level window. Also, dialog boxes don’t have menu bars. In other respects, dialog boxes function like frame windows. (You can add controls to them, for example, in the same way that you add controls to a frame window.) Dialog boxes may be modal or modeless. When a modal dialog box is active, all input is directed to it until it is closed. This means that you cannot access other parts of your program until you have closed the dialog box. When a modeless dialog box is active, input focus can be directed to another window in your program. Thus, other parts of your program remain active and accessible. Dialog boxes are of type Dialog. Two commonly used constructors are shown here:

      Dialog(Frame parentWindow, boolean mode)
      Dialog(Frame parentWindow, String title, boolean mode)

Here, parentWindow is the owner of the dialog box. If mode is true, the dialog box is modal. Otherwise, it is modeless. The title of the dialog box can be passed in title. Generally, you will subclass Dialog, adding the functionality required by your application. Following is a modified version of the preceding menu program that displays a modeless dialog box when the New option is chosen. Notice that when the dialog box is closed, dispose( ) is called. This method is defined by Window, and it frees all system resources associated with the dialog box window.

  // Demonstrate Dialog box.
  import java.awt.*;
  import java.awt.event.*;
  import java.applet.*;
  /*
    <applet code="DialogDemo" width=250 height=250>
    </applet>
  */

  // Create a subclass of Dialog.
  class SampleDialog extends Dialog implements ActionListener {
    SampleDialog(Frame parent, String title) {
      super(parent, title, false);
      setLayout(new FlowLayout());
      setSize(300, 200);

      add(new Label("Press this button:"));
      Button b;
      add(b = new Button("Cancel"));
      b.addActionListener(this);
    }

    public void actionPerformed(ActionEvent ae) {
      dispose();
    }

    public void paint(Graphics g) {
      g.drawString("This is in the dialog box", 10, 70);
    }
  }

  // Create a subclass of Frame.
  class MenuFrame extends Frame {
    String msg = "";
    CheckboxMenuItem debug, test;

    MenuFrame(String title) {
      super(title);

      // create menu bar and add it to frame
      MenuBar mbar = new MenuBar();
      setMenuBar(mbar);

      // create the menu items
      Menu file = new Menu("File");
      MenuItem item1, item2, item3, item4;
      file.add(item1 = new MenuItem("New..."));
      file.add(item2 = new MenuItem("Open..."));
      file.add(item3 = new MenuItem("Close"));
      file.add(new MenuItem("-"));
      file.add(item4 = new MenuItem("Quit..."));
      mbar.add(file);

      Menu edit = new Menu("Edit");
      MenuItem item5, item6, item7;
      edit.add(item5 = new MenuItem("Cut"));
      edit.add(item6 = new MenuItem("Copy"));
      edit.add(item7 = new MenuItem("Paste"));
      edit.add(new MenuItem("-"));

      Menu sub = new Menu("Special", true);
      MenuItem item8, item9, item10;
      sub.add(item8 = new MenuItem("First"));
      sub.add(item9 = new MenuItem("Second"));
      sub.add(item10 = new MenuItem("Third"));
      edit.add(sub);

      // these are checkable menu items
      debug = new CheckboxMenuItem("Debug");
      edit.add(debug);
      test = new CheckboxMenuItem("Testing");
      edit.add(test);

      mbar.add(edit);

      // create an object to handle action and item events
      MyMenuHandler handler = new MyMenuHandler(this);
      // register it to receive those events
      item1.addActionListener(handler);
      item2.addActionListener(handler);
      item3.addActionListener(handler);
      item4.addActionListener(handler);
      item5.addActionListener(handler);
      item6.addActionListener(handler);
      item7.addActionListener(handler);
      item8.addActionListener(handler);
      item9.addActionListener(handler);
      item10.addActionListener(handler);
      debug.addItemListener(handler);
      test.addItemListener(handler);

      // create an object to handle window events
      MyWindowAdapter adapter = new MyWindowAdapter(this);
      // register it to receive those events
      addWindowListener(adapter);
    }
    public void paint(Graphics g) {
      g.drawString(msg, 10, 200);

      if(debug.getState())
        g.drawString("Debug is on.", 10, 220);
      else
        g.drawString("Debug is off.", 10, 220);

      if(test.getState())
        g.drawString("Testing is on.", 10, 240);
      else
        g.drawString("Testing is off.", 10, 240);
    }
  }

  class MyWindowAdapter extends WindowAdapter {
    MenuFrame menuFrame;
    public MyWindowAdapter(MenuFrame menuFrame) {
      this.menuFrame = menuFrame;
    }

    public void windowClosing(WindowEvent we) {
      menuFrame.dispose();
    }
  }

  class MyMenuHandler implements ActionListener, ItemListener {
    MenuFrame menuFrame;
    public MyMenuHandler(MenuFrame menuFrame) {
      this.menuFrame = menuFrame;
    }  
    // Handle action events
    public void actionPerformed(ActionEvent ae) {
      String msg = "You selected ";
      String arg = (String)ae.getActionCommand();
      // Activate a dialog box when New is selected.
      if(arg.equals("New...")) {
        msg += "New.";
        SampleDialog d = new
          SampleDialog(menuFrame, "New Dialog Box");
        d.setVisible(true);
      }
      // Try defining other dialog boxes for these options.
      else if(arg.equals("Open..."))
        msg += "Open.";
      else if(arg.equals("Close"))
        msg += "Close.";
      else if(arg.equals("Quit..."))
        msg += "Quit.";
      else if(arg.equals("Edit"))
        msg += "Edit.";
      else if(arg.equals("Cut"))
        msg += "Cut.";
      else if(arg.equals("Copy"))
        msg += "Copy.";
      else if(arg.equals("Paste"))
        msg += "Paste.";
      else if(arg.equals("First"))
        msg += "First.";
      else if(arg.equals("Second"))
        msg += "Second.";
      else if(arg.equals("Third"))
        msg += "Third.";
      else if(arg.equals("Debug"))
        msg += "Debug.";
      else if(arg.equals("Testing"))
        msg += "Testing.";
      menuFrame.msg = msg;
      menuFrame.repaint();
    }
    public void itemStateChanged(ItemEvent ie) {
      menuFrame.repaint();
    }
  }

  // Create frame window.
  public class DialogDemo extends Applet {
    Frame f;
    
    public void init() {
      f = new MenuFrame("Menu Demo");
      int width = Integer.parseInt(getParameter("width"));
      int height = Integer.parseInt(getParameter("height"));

      setSize(width, height);

      f.setSize(width, height);
      f.setVisible(true);
    }

    public void start() {
      f.setVisible(true);
    }

    public void stop() {
      f.setVisible(false);
    }
  }




FileDialog

Java provides a built-in dialog box that lets the user specify a file. To create a file dialog box, instantiate an object of type FileDialog. This causes a file dialog box to be displayed. Usually, this is the standard file dialog box provided by the operating system. FileDialog provides these constructors:

      FileDialog(Frame parent, String boxName)
      FileDialog(Frame parent, String boxName, int how)
      FileDialog(Frame parent)

Here, parent is the owner of the dialog box, and boxName is the name displayed in the box’s title bar. If boxName is omitted, the title of the dialog box is empty. If how is FileDialog.LOAD, then the box is selecting a file for reading. If how is FileDialog.SAVE, the box is selecting a file for writing. The third constructor creates a dialog box for selecting a file for reading. FileDialog( ) provides methods that allow you to determine the name of the file and its path as selected by the user. Here are two examples:

      String getDirectory( )
      String getFile( )

These methods return the directory and the filename, respectively. The following program activates the standard file dialog box:

  /* Demonstrate File Dialog box.

     This is an application, not an applet.
  */
  import java.awt.*;
  import java.awt.event.*;

  // Create a subclass of Frame
  class SampleFrame extends Frame {
    SampleFrame(String title) {
      super(title);
      // create an object to handle window events
      MyWindowAdapter adapter = new MyWindowAdapter(this);
      // register it to receive those events
      addWindowListener(adapter);
    }
  }

  class MyWindowAdapter extends WindowAdapter {
    SampleFrame sampleFrame;
    public MyWindowAdapter(SampleFrame sampleFrame) {
      this.sampleFrame = sampleFrame;
    }
    public void windowClosing(WindowEvent we) {
      sampleFrame.setVisible(false);
    }
  }

  // Create frame window.
  class FileDialogDemo {
    public static void main(String args[]) {
      Frame f = new SampleFrame("File Dialog Demo");
      f.setVisible(true);
      f.setSize(100, 100);
      FileDialog fd = new FileDialog(f, "File Dialog");
      fd.setVisible(true);
    }
  }

No comments:

Post a Comment