Share on Google+Share on Google+

Java ComboBox in JOptionPane

In this section, you will learn how to create combobox in JOptionPane dialog box.

Java ComboBox in JOptionPane

In this section, you will learn how to create combo box in JOptionPane dialog box. We have created a button to perform an action.On clicking the button, you will get the dialog box with combo box in it. 

Here is the code:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ComboBoxInJOptionPane extends JFrame{
  static final String Select = "Sports";
   String input = "";
  public ComboBoxInJOptionPane() {
    JButton button=new JButton("Add");
  ActionListener lst = new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    String[] sport = new String[] {"Cricket""FootBall""Tennis""Hockey" };
     input = (StringJOptionPane.showInputDialog(ComboBoxInJOptionPane.this,
"Please select your favorite sport"
null, sport,
     JOptionPane.showMessageDialog(null,"You have selected: "+input);

  public static void main(String argv[]) {
    new ComboBoxInJOptionPane();


Posted on: October 23, 2009 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.