Swing Focus Adapter

Swing Focus Adapter

  1. FocusAdapter class is an abstract adapter class in Java Swing.
  2. FocusAdapter class is for receiving keyboard focus events.
  3. Methods specified in FocusAdapter class are empty.
  4. FocusAdapter class exists as convenience for creating listener objects

Class declaration

Below is the declaration for java.awt.event.FocusAdapter class –

public abstract class FocusAdapter
   extends Object
      implements FocusListener

Class constructors

FocusAdapter()

Class methods

No.MethodReturn TypeParameterInvoked when
1focusGained()voidFocusEventcomponent gains the keyboard focus
2focusLost()voidFocusEventcomponent loses the keyboard focus

Methods inherited

This class inherits methods from the following classes:

java.lang.Object

Swing Focus Adapter Example

AdapterExample.java

package com.c4learn;

import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class AdapterExample {
   private JFrame baseFrame;
   private JLabel headLabel;
   private JLabel msgLabel;
   private JPanel controlPanel;

   public AdapterExample() {
      initializeUI();
   }

   public static void main(String[] args) {
      AdapterExample adapterDemo = new AdapterExample();
      adapterDemo.showFocusApapter();
   }

   private void initializeUI() {
      baseFrame = new JFrame("Java Swing Examples");
      baseFrame.setSize(500, 500);
      baseFrame.setLayout(new GridLayout(3, 1));

      headLabel = new JLabel("", JLabel.CENTER);
      msgLabel = new JLabel("", JLabel.CENTER);

      msgLabel.setSize(300, 100);

      baseFrame.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent) {
            System.exit(0);
         }
      });

      controlPanel = new JPanel();
      controlPanel.setLayout(new FlowLayout());

      baseFrame.add(headLabel);
      baseFrame.add(controlPanel);
      baseFrame.add(msgLabel);
      baseFrame.setVisible(true);
   }

   private void showFocusApapter() {
      headLabel.setText("FocusAdapter Example");

      JButton btn1 = new JButton("Button 1");
      JButton btn2 = new JButton("Button 2");
      
      btn1.addFocusListener(new FocusAdapter() {
         public void focusGained(FocusEvent e) {
            msgLabel.setText("Button 1 - Got focus.");
         }
      });

      btn1.addFocusListener(new FocusAdapter() {
         public void focusLost(FocusEvent e) {
            msgLabel.setText("Button 1 - Lost focus.");
         }
      });

      controlPanel.add(btn1);
      controlPanel.add(btn2);
      baseFrame.setVisible(true);
   }
}

After running program in the eclipse you will see below output

focus adapter

Explanation

In this example consider below two buttons –

JButton btn1 = new JButton("Button 1");
JButton btn2 = new JButton("Button 2");

Now we have added focus listener for the button 1 –

btn1.addFocusListener(new FocusAdapter() {

   public void focusGained(FocusEvent e) {
	msgLabel.setText("Button 1 - Got focus.");
   }

   public void focusLost(FocusEvent e) {
	msgLabel.setText("Button 1 - Lost focus.");
   }

});

Now focusLost() method will provide the code to be executed when button1 will lost its focus. Similarly focusGained() method would be executed when button 1 gains focus.

Swing Key Adapter

Swing Key Adapter

  1. KeyAdapter class is an abstract adapter class in Java Swing.
  2. KeyAdapter class is for receiving keyboard events.
  3. Methods specified in KeyAdapter class are empty.
  4. KeyAdapter class exists as convenience for creating listener objects

Class declaration

Below is the declaration for java.awt.event.KeyAdapter class –

public abstract class KeyAdapter
   extends Object
      implements KeyListener

Class constructors

KeyAdapter()

Class methods

No.MethodReturn TypeParameterInvoked when
1keyPressed()voidKeyEventkey has been pressed
2keyReleased()voidKeyEventkey has been released
3keyTyped()voidKeyEventkey has been typed

Methods inherited

This class inherits methods from the following classes:

java.lang.Object

Key Adapter Example

AdapterExample.java

package com.c4learn;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class AdapterExample {
   private JFrame baseFrame;
   private JLabel headLabel;
   private JLabel msgLabel;
   private JPanel controlPanel;

   public AdapterExample() {
      initializeUI();
   }

   public static void main(String[] args) {
      AdapterExample adapterDemo = new AdapterExample();
      adapterDemo.showMouseApapter();
   }

   private void initializeUI() {
      baseFrame = new JFrame("Java Swing Examples");
      baseFrame.setSize(500, 500);
      baseFrame.setLayout(new GridLayout(3, 1));

      headLabel = new JLabel("", JLabel.CENTER);
      msgLabel = new JLabel("", JLabel.CENTER);

      msgLabel.setSize(300, 100);

      baseFrame.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent) {
            System.exit(0);
         }
      });

      controlPanel = new JPanel();
      controlPanel.setLayout(new FlowLayout());

      baseFrame.add(headLabel);
      baseFrame.add(controlPanel);
      baseFrame.add(msgLabel);
      baseFrame.setVisible(true);
   }

   private void showMouseApapter() {
      headLabel.setText("KeyAdapter Example");

      final JTextField textField = new JTextField(10);

      JButton displayButton = new JButton("Display");
      
      displayButton.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
            msgLabel.setText("You entered : " + textField.getText());
         }
      });
      
      textField.addKeyListener(new KeyAdapter() {
         public void keyPressed(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_ENTER) {
               msgLabel.setText("You entered : " + textField.getText());
            }
         }
      });
      
      controlPanel.add(textField);
      controlPanel.add(displayButton);
      baseFrame.setVisible(true);
   }
}

After running program in the eclipse you will see below output

key_adapter

Swing Mouse Adapter

Swing MouseAdapter

  1. MouseAdapter class is an abstract adapter class in Java Swing.
  2. MouseAdapter class is for receiving mouse events.
  3. Methods specified in MouseAdapter class are empty.
  4. MouseAdapter class exists as convenience for creating listener objects

Class declaration

Below is the declaration for java.awt.event.MouseAdapter class –

public abstract class MouseAdapter
   extends Object
      implements MouseListener, 
                 MouseWheelListener, 
                 MouseMotionListener

Class constructors

MouseAdapter()

Class methods

No.MethodReturn TypeParameterInvoked when
1mouseClicked()voidMouseEventmouse button has been clicked
2mouseDragged()voidMouseEventmouse button is pressed on a component and then dragged
3mouseEntered()voidMouseEventmouse enters a component.
4mouseExited()voidMouseEventmouse exits a component.
5mouseMoved()voidMouseEventmouse cursor has been moved onto a component but no buttons have been pushed
6mousePressed()voidMouseEventmouse button has been pressed on a component
7mouseReleased()voidMouseEventmouse button has been released on a component
8mouseWheelMoved()voidMouseEventmouse wheel is rotated.

Methods inherited

This class inherits methods from the following classes:

java.lang.Object

Mouse Adapter Example

AdapterExample.java

package com.c4learn;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class AdapterExample {
   private JFrame baseFrame;
   private JLabel headLabel;
   private JLabel msgLabel;
   private JPanel controlPanel;

   public AdapterExample() {
      initializeUI();
   }

   public static void main(String[] args) {
      AdapterExample adapterDemo = new AdapterExample();
      adapterDemo.showMouseApapter();
   }

   private void initializeUI() {
      baseFrame = new JFrame("Java Swing Examples");
      baseFrame.setSize(500, 500);
      baseFrame.setLayout(new GridLayout(3, 1));

      headLabel = new JLabel("", JLabel.CENTER);
      msgLabel = new JLabel("", JLabel.CENTER);

      msgLabel.setSize(300, 100);

      baseFrame.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent) {
            System.exit(0);
         }
      });

      controlPanel = new JPanel();
      controlPanel.setLayout(new FlowLayout());

      baseFrame.add(headLabel);
      baseFrame.add(controlPanel);
      baseFrame.add(msgLabel);
      baseFrame.setVisible(true);
   }

   private void showMouseApapter() {
      headLabel.setText("Listener in action : MouseAdapter");

      JPanel panel = new JPanel();
      panel.setBackground(Color.gray);
      panel.setLayout(new FlowLayout());
      
      JLabel msglabel = new JLabel("www.c4learn.com", JLabel.CENTER);

      msglabel.addMouseListener(new MouseAdapter() {
         public void mouseClicked(MouseEvent e) {
            msgLabel.setText("Mouse Clicked: (" + e.getX() + ", "
                  + e.getY() + ")");
         }
      });
      
      panel.add(msglabel);

      controlPanel.add(panel);
      baseFrame.setVisible(true);
   }
}

After running program in the eclipse you will see below output

mouse_adapter

Swing MouseMotion Adapter

Swing Mouse Motion Adapter

  1. MouseMotionAdapter class is an abstract adapter class in Java Swing.
  2. MouseMotionAdapter class is for receiving mouse motion events.
  3. Methods specified in MouseMotionAdapter class are empty.
  4. MouseMotionAdapter class exists as convenience for creating listener objects

Class declaration

Below is the declaration for java.awt.event.MouseMotionAdapter class –

public abstract class MouseMotionAdapter
   extends Object
      implements MouseMotionListener

Class constructors

MouseMotionAdapter()

Class methods

No.MethodReturn TypeParameterInvoked when
1mouseDragged()voidMouseEventMouse button is pressed on a component and then dragged.
2mouseMoved()voidMouseEventMouse cursor has been moved onto a component but no buttons is clicked

Methods inherited

This class inherits methods from the following classes:

java.lang.Object

WindowAdapter Example

AdapterExample.java

package com.c4learn;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class AdapterExample {
   private JFrame baseFrame;
   private JLabel headLabel;
   private JLabel msgLabel;
   private JPanel controlPanel;

   public AdapterExample() {
      initializeUI();
   }

   public static void main(String[] args) {
      AdapterExample adapterDemo = new AdapterExample();
      adapterDemo.showMouseApapter();
   }

   private void initializeUI() {
      baseFrame = new JFrame("Java Swing Examples");
      baseFrame.setSize(500, 500);
      baseFrame.setLayout(new GridLayout(3, 1));

      headLabel = new JLabel("", JLabel.CENTER);
      msgLabel = new JLabel("", JLabel.CENTER);

      msgLabel.setSize(300, 100);

      baseFrame.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent) {
            System.exit(0);
         }
      });

      controlPanel = new JPanel();
      controlPanel.setLayout(new FlowLayout());

      baseFrame.add(headLabel);
      baseFrame.add(controlPanel);
      baseFrame.add(msgLabel);
      baseFrame.setVisible(true);
   }

   private void showMouseApapter() {
      headLabel.setText("MouseMotionAdapter Example");

      JPanel panel = new JPanel();
      panel.setBackground(Color.gray);
      panel.setLayout(new FlowLayout());
      
      panel.addMouseMotionListener(new MouseMotionAdapter() {
         public void mouseMoved(MouseEvent e) {
            msgLabel.setText("Mouse Moved: (" + e.getX() + ", "
                  + e.getY() + ")");
         }
      });

      JLabel msglabel = new JLabel("www.c4learn.com");
      panel.add(msglabel);

      controlPanel.add(panel);
      baseFrame.setVisible(true);
   }
}

After running program in the eclipse you will see below output

mouse_motion_adapter

Swing Window Adapter

Swing Window Adapter

  1. WindowAdapter class is an abstract adapter class in Java Swing.
  2. WindowAdapter class is for receiving window events.
  3. Methods specified in WindowAdapter class are empty.
  4. WindowAdapter class exists as convenience for creating listener objects

Class declaration

Below is the declaration for java.awt.event.WindowAdapter class –

public abstract class WindowAdapter
   extends Object
      implements WindowListener, 
                 WindowStateListener, 
                 WindowFocusListener

Class constructors

WindowAdapter()

Class methods

No.MethodReturn TypeParameterInvoked when
1windowActivated()voidWindowEventwindow is activated.
2windowClosed()voidWindowEventwindow has been closed.
3windowClosing()voidWindowEventwindow is in the process of being closed.
4windowDeactivated()voidWindowEventwindow is de-activated.
5windowDeiconified()voidWindowEventwindow is de-iconified.
6windowGainedFocus()voidWindowEventWindow is set to be the focused Window
7windowIconified()voidWindowEventwindow is iconified.
8windowLostFocus()voidWindowEventWindow is no longer the focused Window
9windowOpened()voidWindowEventwindow has been opened.
10windowStateChanged()voidWindowEventwindow state is changed.

Methods inherited

This class inherits methods from the following classes:

java.lang.Object

WindowAdapter Example

AdapterExample.java

package com.c4learn;

import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class AdapterExample {
   private JFrame baseFrame;
   private JLabel headLabel;
   private JLabel msgLabel;
   private JPanel controlPanel;

   public AdapterExample() {
      initializeUI();
   }

   public static void main(String[] args) {
      AdapterExample adapterDemo = new AdapterExample();
      adapterDemo.showWindowApapter();
   }

   private void initializeUI() {
      baseFrame = new JFrame("Java Swing Examples");
      baseFrame.setSize(500, 500);
      baseFrame.setLayout(new GridLayout(3, 1));

      headLabel = new JLabel("", JLabel.CENTER);
      msgLabel = new JLabel("", JLabel.CENTER);

      msgLabel.setSize(300, 100);
      
      baseFrame.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent) {
            System.exit(0);
         }
      });
      
      controlPanel = new JPanel();
      controlPanel.setLayout(new FlowLayout());

      baseFrame.add(headLabel);
      baseFrame.add(controlPanel);
      baseFrame.add(msgLabel);
      baseFrame.setVisible(true);
   }

   private void showWindowApapter() {
      headLabel.setText("WindowAdapter Example");

      final JFrame innerFrame = new JFrame();
      innerFrame.setSize(300, 200);
      innerFrame.setTitle("WindowAdapter Demo");
      
      innerFrame.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent) {
            innerFrame.dispose();
         }
      });
      
      JLabel msglabel = new JLabel("Welcome to Swing", JLabel.CENTER);
      innerFrame.add(msglabel);
      innerFrame.setVisible(true);
   }
}

After ruuning program in the eclipse you will see below output

window_adapter

Swing JSpinner Class

Swing JSpinner Class

package com.c4learn.swing;

import java.awt.FlowLayout;
import java.awt.GridLayout;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerModel;
import javax.swing.SpinnerNumberModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class JSpinnerDemo {

  private JFrame mainFrame;
  private JLabel headLabel;
  private JLabel msgLabel;
  private JPanel mainPanel;

  public JSpinnerDemo() {
    mainFrame = new JFrame("Java Swing Examples");
    mainFrame.setSize(400, 400);
    mainFrame.setLayout(new GridLayout(3, 1));

    headLabel = new JLabel("Spinner Demo", JLabel.CENTER);
    msgLabel = new JLabel("", JLabel.CENTER);
    mainPanel = new JPanel();
    mainPanel.setLayout(new FlowLayout());

    mainFrame.add(headLabel);
    mainFrame.add(mainPanel);
    mainFrame.add(msgLabel);
    mainFrame.setVisible(true);
  }

  public static void main(String[] args) {
    JSpinnerDemo swingControlDemo = new JSpinnerDemo();
    swingControlDemo.showSpinnerDemo();
  }

  private void showSpinnerDemo() {

    SpinnerModel spinnerModel = new 
                 SpinnerNumberModel(5, 0, 100, 1);
    msgLabel.setText("Spinner Value : 5");
    JSpinner spinner = new JSpinner(spinnerModel);

    spinner.addChangeListener(new ChangeListener() {
      public void stateChanged(ChangeEvent e) {
        msgLabel.setText("Spinner Value : "
            + ((JSpinner) e.getSource()).getValue());
      }
    });

    mainPanel.add(spinner);
    mainFrame.setVisible(true);
  }
}

Output :

Swing Spinner

Swing JSlider Class

Swing JSlider Class

package com.c4learn.swing;

import java.awt.FlowLayout;
import java.awt.GridLayout;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class JSliderDemo {

  private JFrame mainFrame;
  private JLabel headLabel;
  private JLabel msgLabel;
  private JPanel mainPanel;
  
  public JSliderDemo() {
    mainFrame = new JFrame("Java Swing Examples");
    mainFrame.setSize(400, 400);
    mainFrame.setLayout(new GridLayout(3, 1));

    headLabel = new JLabel("Slider Demo", JLabel.CENTER);
    msgLabel = new JLabel("", JLabel.CENTER);
    mainPanel = new JPanel();
    mainPanel.setLayout(new FlowLayout());

    mainFrame.add(headLabel);
    mainFrame.add(mainPanel);
    mainFrame.add(msgLabel);
    mainFrame.setVisible(true);
  }

  public static void main(String[] args) {
    JSliderDemo swingControlDemo = new JSliderDemo();
    swingControlDemo.showSliderDemo();
  }

  private void showSliderDemo() {

    JSlider slider= new 
        JSlider(JSlider.HORIZONTAL,0,100,10);
    msgLabel.setText("Value : 10");
    
    slider.addChangeListener(new ChangeListener() {
           public void stateChanged(ChangeEvent e) {
             msgLabel.setText("Value : " 
              + ((JSlider) e.getSource()).getValue());
           }
        });
    
    mainPanel.add(slider);
    mainFrame.setVisible(true);
  }
}

Output :

Swing JSlider Class

Swing JProgressBar Class

Swing JProgressBar Class

package com.c4learn.swing;

import java.awt.FlowLayout;
import java.awt.GridLayout;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;

public class JProgressDemo {

  private JFrame mainFrame;
  private JLabel headLabel;
  private JLabel msgLabel;
  private JPanel mainPanel;
  private JProgressBar jProgress;

  public JProgressDemo() {
    mainFrame = new JFrame("Java Swing Examples");
    mainFrame.setSize(400, 400);
    mainFrame.setLayout(new GridLayout(3, 1));

    headLabel = new JLabel("Progress Bar", JLabel.CENTER);
    msgLabel = new JLabel("No Progress", JLabel.CENTER);
    mainPanel = new JPanel();
    mainPanel.setLayout(new FlowLayout());

    mainFrame.add(headLabel);
    mainFrame.add(mainPanel);
    mainFrame.add(msgLabel);
    mainFrame.setVisible(true);
  }

  public static void main(String[] args) {
    JProgressDemo demo = new JProgressDemo();
    demo.showButtonDemo();
    demo.iterate();
  }

  private void showButtonDemo() {

    jProgress = new JProgressBar(0, 100);
    jProgress.setBounds(10, 10, 300, 15);
    jProgress.setValue(0);
    jProgress.setStringPainted(true);

    mainPanel.add(jProgress);
    mainFrame.setVisible(true);
  }

  public void iterate() {
    int i = 0;
    while (i <= 100) {
      jProgress.setValue(i);
      String valText = 
          Integer.toString(jProgress.getValue());
      i = i + 1;
      try {
        msgLabel.setText("Progress : " + valText);
        Thread.sleep(150);
      } catch (Exception e) {
      }
    }
  }
}

Output :

Swing JProgressBar Class

Swing JFileChooser Class

Swing JFileChooser Class

package com.c4learn.swing;

import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class JFileChooserDemo {

  private JFrame mainFrame;
  private JLabel headLabel;
  private JLabel msgLabel;
  private JPanel mainPanel;

  public JFileChooserDemo() {
    mainFrame = new JFrame("Java Swing Examples");
    mainFrame.setSize(400, 400);
    mainFrame.setLayout(new GridLayout(3, 1));

    headLabel = new JLabel("File Chooser", JLabel.CENTER);
    msgLabel = new JLabel("File Name :", JLabel.CENTER);
    mainPanel = new JPanel();
    mainPanel.setLayout(new FlowLayout());

    mainFrame.add(headLabel);
    mainFrame.add(mainPanel);
    mainFrame.add(msgLabel);
    mainFrame.setVisible(true);
  }

  public static void main(String[] args) {
    JFileChooserDemo demo = new JFileChooserDemo();
    demo.selectFile();
  }

  private void selectFile() {

    JButton btnFileOpen = new JButton("Select File");
    final JFileChooser fileDialog = new JFileChooser();

    btnFileOpen.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {

       int ret = fileDialog.showOpenDialog(mainFrame);

       if (ret == JFileChooser.APPROVE_OPTION) {
            File file = fileDialog.getSelectedFile();
            msgLabel.setText(file.getName());
        } else {
            msgLabel.setText("No file Selection.");
        }
      }
    });

    mainPanel.add(btnFileOpen);
    mainFrame.setVisible(true);
  }
}

Output :

Swing JFileChooser Class

Swing JOptionPane : Class

Swing JOptionPane Class :

package com.c4learn.swing;

import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class JOptionPaneDemo {

  private JFrame mainFrame;
  private JLabel headLabel;
  private JLabel msgLabel;
  private JPanel mainPanel;

  public JOptionPaneDemo() {
    mainFrame = new JFrame("Java Swing Examples");
    mainFrame.setSize(400, 400);
    mainFrame.setLayout(new GridLayout(3, 1));

    headLabel = new JLabel("Optionpane Demo", JLabel.CENTER);
    msgLabel = new JLabel("No Action", JLabel.CENTER);
    mainPanel = new JPanel();
    mainPanel.setLayout(new FlowLayout());

    mainFrame.add(headLabel);
    mainFrame.add(mainPanel);
    mainFrame.add(msgLabel);
    mainFrame.setVisible(true);
  }

  public static void main(String[] args) {
    JOptionPaneDemo demo = new JOptionPaneDemo();
    demo.optionPaneDemo();
  }

  private void optionPaneDemo() {

    JButton okButton = new JButton("Message Box");

    okButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {

        int optn = JOptionPane.showConfirmDialog(mainFrame,
            "Click any button", "Option Pane",
            JOptionPane.YES_NO_CANCEL_OPTION,
            JOptionPane.INFORMATION_MESSAGE);

        if (optn == JOptionPane.YES_OPTION) {
          msgLabel.setText("Yes Selected.");
        } else if (optn == JOptionPane.NO_OPTION) {
          msgLabel.setText("No Selected.");
        } else if (optn == JOptionPane.CANCEL_OPTION) {
          msgLabel.setText("Cancel Selected.");
        }
      }
    });

    mainPanel.add(okButton);
    mainFrame.setVisible(true);
  }
}

Output :

Swing JOptionPane 1
Swing JOptionPane 2