Java Swing How to - Extend JDialog to create confirmation dialog








Question

We would like to know how to extend JDialog to create confirmation dialog.

Answer

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Frame;
/* w ww .  ja  va2  s  .  c  o  m*/
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.border.EmptyBorder;

public class Main {
  public static void main(String[] args) {
    JFrame f = new JFrame();
    final ConfirmDialog dialog = new ConfirmDialog(f);
    final JTree tree = new JTree();
    tree.setVisibleRowCount(5);
    final JScrollPane treeScroll = new JScrollPane(tree);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JButton b = new JButton("Choose Tree Item");
    b.addActionListener(e -> {
      int result = dialog.showConfirmDialog(treeScroll, "Choose an item");
      if (result == ConfirmDialog.OK_OPTION) {
        System.out.println(tree.getSelectionPath());
      } else {
        System.out.println("User cancelled");
      }
    });
    JPanel p = new JPanel(new BorderLayout());
    p.add(b);
    p.setBorder(new EmptyBorder(50, 50, 50, 50));
    f.setContentPane(p);
    f.pack();
    f.setLocationByPlatform(true);
    f.setVisible(true);

  }
}

class ConfirmDialog extends JDialog {

  public static final int OK_OPTION = 0;
  public static final int CANCEL_OPTION = 1;

  private int result = -1;

  JPanel content;

  public ConfirmDialog(Frame parent) {
    super(parent, true);

    JPanel gui = new JPanel(new BorderLayout(3, 3));
    gui.setBorder(new EmptyBorder(5, 5, 5, 5));
    content = new JPanel(new BorderLayout());
    gui.add(content, BorderLayout.CENTER);
    JPanel buttons = new JPanel(new FlowLayout(4));
    gui.add(buttons, BorderLayout.SOUTH);

    JButton ok = new JButton("OK");
    buttons.add(ok);
    ok.addActionListener(e->{
        result = OK_OPTION;
        setVisible(false);
    });

    JButton cancel = new JButton("Cancel");
    buttons.add(cancel);
    cancel.addActionListener(e->{
        result = CANCEL_OPTION;
        setVisible(false);
    });
    setContentPane(gui);
  }
  public int showConfirmDialog(JComponent child, String title) {
    setTitle(title);
    content.removeAll();
    content.add(child, BorderLayout.CENTER);
    pack();
    setLocationRelativeTo(getParent());
    setVisible(true);
    return result;
  }
}