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

Combo Box operation in Java Swing

                         

In this section, you can learn how to operate the Combo Box component, you will learn how to add items to the combo box, remove items from the combo box.

This program shows a text field, a combo box and two command buttons, first is for the adding items to the combo box and another is for the removing items from the combo. When you click on the add command button then the text of the text box is added to the combo box if the text box is not blank otherwise a message box will display with the message "Please enter text in Text Box". But when you click on the remove button the item at the 0th (zero) position of the combo box will be remove from the combo box if the combo box has one item at least otherwise a message box will display with the message "Item not available.". Following is the image for the result of the given program:

Add Remove Items from Combo Box in Swing

This program has used various java APIs for doing required are explained as follows:

JComboBox combo = new JComboBox(items);
The above code has been used to create a combo box in this program. The JComboBox instance combo is created  using the constructor of the JComboBox class of the javax.swing.*; package. This constructor holds the string array in which items for the combo box are kept.

getItemCount():
This is the method of the JComboBox class which return the number of the items present is the combo box.

getItemAt(index):
this is the method of the JComboBox class which returns the name of the item of the combo box at the specified position. This specification of position the item in the combo box is held by the getItemAt() method as a parameter.

showMessageDialog():
This the method of the JOptionPane class of javax.swing.*; package. This method displays some messages in the special dialog box. This method holds two argument in this program in which first is the parent object name and another is the message text which has to be displayed. 

addItem(String):
This is the method of the JComboBox class which adds items to the combo box. This method takes a string argument which is to be used to add to the combo box.

removeItemAt(index):
This is the method of the JComboBox class which remove the item at the specified position of the combo box. This method holds the integer value for the position number of he of the item in combo box to remove it.

Here is the code of the program:

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

public class AddRemoveItemFromCombo{
  JComboBox combo;
  JTextField txtBox;
  public static void main(String[] args){
    AddRemoveItemFromCombo ar = new AddRemoveItemFromCombo();
  }

  public AddRemoveItemFromCombo(){
    JFrame frame = new JFrame("Add-Remove Item of a Combo Box");
    String items[] {"Java""JSP""PHP""C""C++"};
    combo = new JComboBox(items);
    JButton button1 = new JButton("Add");
    txtBox = new JTextField(20);
    button1.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent e){
        if (!txtBox.getText().equals("")){
          int a = 0;
          for(int i = 0; i < combo.getItemCount(); i++){
            if(combo.getItemAt(i).equals(txtBox.getText())){
              a = 1;
              break;
            }
          }
          if (a == 1)
            JOptionPane.showMessageDialog(null,"Combo has already this item.");
          else
            combo.addItem(txtBox.getText());
        }
        else{
          JOptionPane.showMessageDialog(null,"Please enter text in Text Box");
        }
      }
    });
    JButton button2 = new JButton("Remove");
    button2.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent e){
        if (combo.getItemCount() 0)
          combo.removeItemAt(0);
        else
          JOptionPane.showMessageDialog(null,"Item not available");
      }
    });
    JPanel panel = new JPanel();
    JPanel panel1 = new JPanel();
    panel.add(txtBox);
    panel.add(combo);
    panel.add(button1);
    panel.add(button2);
    frame.add(panel);
//    frame.add(panel1);
    frame.setSize(400400);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  }
}

Download this example.

                         

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

Current Comments

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

Thanks budy for this example.
keep this futher.

Posted by Gayan on Saturday, 12.8.07 @ 11:37am | #41581

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

 

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.

Hot Web Programming Job

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

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

Copyright © 2007. All rights reserved.