Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML
 
 
Search All Tutorials
  

 
Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML
 
Java
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

Create a JComboBox Component in Java

                         

In this section, you will learn about the JComboBox Component of swing in java. The JComboBox is used to display drop-down list. It provides you options to select an item from the item list. You can never select more than one items from a combo box. Combo Box can be whether editable or only non-editable means only readable.

This program displays a simple combo box on the frame which contains multiple items like: BCA, MCA, PPC etc. The background color of this combo box is gray and foreground color is red. Here, the background color of the combo box and the foreground color is set using the setBackground(Color) and setForeground(Color) method of the JComboBox class. When you select the item from the combo box then the selected item is displayed in the text box. Image of the result for the given program is as follows in the pictorial form:

Swing Combo Box Component

And
Swing Drop-Down List

APIs used in the program:

JComboBox:
This is the class which is used to create a combo box in swing using it's constructor.

itemStateChanged():
This is the method which receives the ItemEvent generated by the addItemListener() method of the JComboBox class. The event is generated when you select item from the combo box.

Here is the code of program:

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

public class ComboBox{
  JComboBox combo;
  JTextField txt;
  public static void main(String[] args) {
    ComboBox b = new ComboBox();
  }

  public ComboBox(){
    String course[] {"BCA","MCA","PPC","CIC"};
    JFrame frame = new JFrame("Creating a JComboBox Component");
    JPanel panel = new JPanel();
    combo = new JComboBox(course);
    combo.setBackground(Color.gray);
    combo.setForeground(Color.red);
    txt = new JTextField(10);
    panel.add(combo);
    panel.add(txt);
    frame.add(panel);
    combo.addItemListener(new ItemListener(){
      public void itemStateChanged(ItemEvent ie){
        String str = (String)combo.getSelectedItem();
        txt.setText(str);
      }
    });
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(400,400);
    frame.setVisible(true);
  }
}

Download this example.

                         

Facing Programming Problem?
Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

4 comments so far (post your own) View All Comments Latest 10 Comments:

this is good site for learning java

Posted by venu on Saturday, 07.19.08 @ 03:13am | #67992

my question :- if multiple combobox how we check validation on apply button

Posted by chandan jha on Thursday, 05.29.08 @ 13:29pm | #61338

code fro to select an image from combobox & drag& drop on frame in java

Posted by sayali on Wednesday, 04.18.07 @ 09:42am | #14476

i have some items in the textarea(multiline).if a user selects an item ,that is to be displayed in the textbox we provide.i need jsp code for this.plz help me in this regard.

Posted by anshu on Wednesday, 12.13.06 @ 13:10pm | #942

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.