Java Swing How to - Overwrite JOptionPane action event value








Question

We would like to know how to overwrite JOptionPane action event value.

Answer

import java.awt.BorderLayout;
/*  w w w  .jav a  2s.c  om*/
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

public class Main {
  public static void main(String[] args) {
    final JFrame frame = new JFrame();
    JOptionPane pane = new JOptionPane("Some message",
        JOptionPane.QUESTION_MESSAGE, JOptionPane.YES_NO_OPTION) {
      @Override
      public void setValue(Object newValue) {
        super.setValue(newValue);
        JOptionPane.showMessageDialog(frame.getContentPane(), "You have hit "
            + newValue);
      }
    };
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(new BorderLayout());
    frame.add(new JLabel("Some panel in the middle"), BorderLayout.CENTER);
    frame.add(pane, BorderLayout.SOUTH);
    frame.pack();
    frame.setVisible(true);
  }
}