Header Ads Widget

Responsive Advertisement

Ticker

6/recent/ticker-posts

Add JComboBox into JFrame (Java)

Source code below will show you how create contents for JComboBox. After that we will add JComboBox into a JFrame.

***********************************************************************
COMPLETE SOURCE CODE FOR : AddJComboBoxIntoJFrame.java
***********************************************************************
import javax.swing.JComboBox;
import javax.swing.JFrame;

import java.awt.FlowLayout;

public class AddJComboBoxIntoJFrame
{
public static void main(String[]args)
{
 //Create combo box contents from type String
 String[]comboBoxContents={"Chicken","Duck","Cow","Sheep"};

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

 //Create a JFrame with title ( Add JComboBox into JFrame )
 JFrame frame=new JFrame("Add JComboBox into JFrame");

 //Set layout for JFrame
 frame.setLayout(new FlowLayout());

 //Add JComboBox into JFrame
 frame.add(myComboBox);

 //Set 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
***********************************************************************