Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials:
 

Software Solutions and Services
 

 
  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.

 

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.

                         

» View all related tutorials
Related Tags: c memory image class images interface io colors sed color get implementation display icon height value source width int id

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.

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

Current Comments

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

Hi ya

I got a assignment in java. Do u knw y this bluj terminal window popping up with "null" all the time

Posted by bob on Friday, 12.5.08 @ 08:24am | #82447

Thanks budy for this example.
keep this futher.

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

Training Courses
Tell A Friend
Your Friend Name
Website Designing Services
 
Web Designing Packages From $150!
 
Website Designing Company Web Hosting
 
Website Designing Quotation
 
Search Tutorials:

 

 
 

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 | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

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

Copyright © 2008. All rights reserved.