Pages

Create combo box using JComboBox

Source code below will show you, how to create a combo box in java using JComboBox class.

**************************************************************************
COMPLETE SOURCE CODE FOR : CreateComboBoxUsingJComboBox.java
**************************************************************************


import javax.swing.JComboBox;
import javax.swing.JFrame;

import java.awt.FlowLayout;

public class CreateComboBoxUsingJComboBox
{
public static void main(String[]args)
{
//Create contents for JComboBox using array
//Using String as array type
String[]comboBoxContents={"HI","HA","HU"};

//Create a combo box using JComboBox
JComboBox comboBox=new JComboBox(comboBoxContents);

//Create a JFrame that will be use to put JComboBox into it
JFrame frame=new JFrame("Create combo box using JComboBox");

//set JFrame layout to FlowLayout
frame.setLayout(new FlowLayout());

//add created JComboBox into JFrame
frame.add(comboBox);

//set default close operation for JFrame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//set JFrame size
frame.setSize(400,400);

//make JFrame visible. So we can see it
frame.setVisible(true);
}
}


**************************************************************************
JUST COMPILE AND EXECUTE IT
**************************************************************************
Related Posts Plugin for WordPress, Blogger...