Bitcoin

Bitcoin
Bitcoin

JDialog

The JDialog control represents a top level window with a border and a title used to take some form of input from the user. It inherits the Dialog class.
Unlike JFrame, it doesn't have maximize and minimize buttons.

JDialog class declaration

Let's see the declaration for javax.swing.JDialog class.
public class JDialog
extends Dialog implements WindowConstants, Accessible, RootPane
A Sample code on JDialog
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class AboutDialog extends JDialog implements ActionListener {
  public AboutDialog(JFrame parent, String title, String message) {
    super(parent, title, true);
    if (parent != null) {
      Dimension parentSize = parent.getSize(); 
      Point p = parent.getLocation(); 
      setLocation(p.x + parentSize.width / 4, p.y + parentSize.height / 4);
    }
    JPanel messagePane = new JPanel();
    messagePane.add(new JLabel(message));
    getContentPane().add(messagePane);
    JPanel buttonPane = new JPanel();
    JButton button = new JButton("OK"); 
    buttonPane.add(button); 
    button.addActionListener(this);
    getContentPane().add(buttonPane, BorderLayout.SOUTH);
    setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    pack(); 
    setVisible(true);
  }
  public void actionPerformed(ActionEvent e) {
    setVisible(false); 
    dispose(); 
  }
  public static void main(String[] a) {
    AboutDialog dlg = new AboutDialog(new JFrame(), "title", "message");
  }
}
THE OUTPUT:
ANOTHER JDIALOG CODE
import java.awt.BorderLayout;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.InputMap;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRootPane;
import javax.swing.KeyStroke;
public class Dialog1 extends JDialog {
    private static final long serialVersionUID = 1L;
    public Dialog1(JFrame parent, String title, String message) {
        super(parent, title);
        System.out.println("creating the window..");
        // set the position of the window
        Point p = new Point(400, 400);
        setLocation(p.x, p.y);
        // Create a message
        JPanel messagePane = new JPanel();
        messagePane.add(new JLabel(message));
        // get content pane, which is usually the
        // Container of all the dialog's components.
        getContentPane().add(messagePane);
        // Create a button
        JPanel buttonPane = new JPanel();
        JButton button = new JButton("Close me");
        buttonPane.add(button);
        // set action listener on the button
        button.addActionListener(new MyActionListener());
        getContentPane().add(buttonPane, BorderLayout.PAGE_END);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        pack();
        setVisible(true);
    }
    // override the createRootPane inherited by the JDialog, to create the rootPane.
    // create functionality to close the window when "Escape" button is pressed
    public JRootPane createRootPane() {
        JRootPane rootPane = new JRootPane();
        KeyStroke stroke = KeyStroke.getKeyStroke("ESCAPE");
        Action action = new AbstractAction() {
            private static final long serialVersionUID = 1L;
            public void actionPerformed(ActionEvent e) {
                System.out.println("escaping..");
                setVisible(false);
                dispose();
            }
        };
        InputMap inputMap = rootPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
        inputMap.put(stroke, "ESCAPE");
        rootPane.getActionMap().put("ESCAPE", action);
        return rootPane;
    }
    // an action listener to be used when an action is performed
    // (e.g. button is pressed)
    class MyActionListener implements ActionListener {
        //close and dispose of the window.
        public void actionPerformed(ActionEvent e) {
            System.out.println("disposing the window..");
            setVisible(false);
            dispose();
        }
    }
    public static void main(String[] a) {
        Dialog1 dialog = new Dialog1(new JFrame(), "hello sjavaspot", "This is a JDialog example");
        // set the size of the window
        dialog.setSize(300, 150);
    }
}
JDIALOG CODE EXAMPLE
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class Dialog2 extends JFrame {
    JDialog d1;

    public Dialog1() {
       createAndShowGUI();
    }
   private void createAndShowGUI()
    {
        setTitle("JDialog Example");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLayout(new FlowLayout());

        // Must be called before creating JDialog for
        // the desired effect
        JDialog.setDefaultLookAndFeelDecorated(true);

        // A perfect constructor, mostly used.
        // A dialog with current frame as parent
        d1=new JDialog(this,"This is title",true);

        // Set size and location
        d1.setSize(400,400);
        d1.setLocation(200, 200);

        // Set some layout
        d1.setLayout(new FlowLayout());
        //Add components in JDialog
        d1.add(new JButton("Button"));
        d1.add(new JLabel("Label"));
        d1.add(new JTextField(20));
        //set JFrame size and visibility
        setSize(1000,700);
        setVisible(true);

        // Like JFrame, JDialog isn't visible, you'll
        // have to make it visible
        // Remember to show JDialog after its parent is
        // shown so that its parent is visible
        d1.setVisible(true);
    }
    public static void main(String[] a) {
        Dialog2 dialog = new Dialog2();
    }
}
THE OUTPUT

This is an example of custom dialog with an expandable details pane. When the Details button is clicked the details pane will visible below the Details button, and the dialog will expand in order to fit the newly visible component.

Similarly, if the details button is clicked while the details pane is visible, the details pane will disappear and the dialog will go back to its previous size.

CustomErrorDialog.java

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.BoxLayout;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.border.EmptyBorder;

public class CustomErrorDialog {

    public static void showDialog(String title, String details) {
        final JTextPane textPane = new JTextPane();
        textPane.setContentType("text/html");

        textPane.setText(details);
        textPane.setEditable(false);


        final JScrollPane scrollPane = new JScrollPane(textPane);
        scrollPane.setAlignmentX(0);

        final JPanel content = new JPanel();
        content.setLayout(new BoxLayout(content, BoxLayout.PAGE_AXIS));

        final JDialog dialog = new JOptionPane(
                content,
                JOptionPane.ERROR_MESSAGE,
                JOptionPane.DEFAULT_OPTION).createDialog(null, "Error!");

        JLabel message = new JLabel(title);
        message.setBorder(new EmptyBorder(10, 10, 10, 10));
        message.setAlignmentX(0);
        Dimension labelSize = message.getPreferredSize();
        labelSize.setSize(300, labelSize.height);
        message.setPreferredSize(labelSize);
        content.add(message);

        JCheckBox cb = new JCheckBox(new AbstractAction() {

            private static final long serialVersionUID = 1L;

            {
                this.putValue(Action.SELECTED_KEY, false);
                this.putValue(Action.NAME, "Show Details");
            }

            @Override
            public void actionPerformed(ActionEvent e) {
                if ((Boolean) this.getValue(Action.SELECTED_KEY)) {
                    content.add(scrollPane);
                } else {
                    content.remove(scrollPane);
                }
                content.invalidate();
                dialog.invalidate();
                dialog.pack();
            }
        });
        content.add(cb);

        dialog.setResizable(false);
        dialog.pack();
        dialog.setLocationRelativeTo(null);
        dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        dialog.setVisible(true);
    }
    public static void main(String[] args){
        CustomErrorDialog error = new CustomErrorDialog();
        CustomErrorDialog.showDialog("<html>Error Message Title</html>",
        "<html>Error Message Description.<br> You can use html here</html>");
    }
}
THE OUTPUT:



No comments:

Post a Comment

Facebook