Adv Java - [Swing: JComboBox Class]

♠ Posted by Unknown in at 20:49

JComboBox Class


Swing provides a combo box (a combination of a text field and a drop-down list) through the JComboBox class, which extends JComponent. A combo box normally displays one entry. However, it can also display a drop-down list that allows a user to select a different entry. You can also type your selection into the text field.
Two of JComboBox’s constructors are show here:

     JComboBox()
     JComboBox(Vector v)

Items are added to the list of choices via the addItem() method, whose signature is show here:
    

     void addItem(Object obj)

Example:

/ * <APPLET CODE = "JComboboxExample.CLASS" WIDTH = 500 HEIGHT = 500></APPLET>
 */
 import java.awt.*;
Image: Swing JComboBox Example import java.awt.event.*;
 import javax.swing.*;

public class JComboboxExample extends JApplet implements ItemListener
{
          JLabel jl;

          public void init()
          {
                   Container contentPane = getContentPane();
                   contentPane.setLayout(new FlowLayout());
                  
                   JComboBox jc = new JComboBox();
                   jc.addItem("Java Logo");
                   jc.addItem("Winxp Logo");
                   jc.addItem("Green Logo");
                   jc.addItem("Red Logo");
                   jc.addItemListener(this);
                   contentPane.add(jc);
                  
                   jl = new JLabel(new ImageIcon("c:\\PracticeJava\\javalogo.gif"));
                   contentPane.add(jl);
          }
         
          public void itemStateChanged(ItemEvent ie)
          {
                   String s = (String)ie.getItem();
                   if(s == "Java Logo")
                   {
                             jl.setIcon(new ImageIcon("C:\\PracticeJava\\javalogo.gif"));
                   }
                   else if(s == "Windxp Logo")
                   {
                             jl.setIcon(new ImageIcon("C:\\PracticeJava\\winxp.gif"));
                   }
                   else if(s == "Green Logo")
                   {
                             jl.setIcon(new ImageIcon("C:\\PracticeJava\\greenshd.gif"));
                   }
                   else
                   {
                             jl.setIcon(new ImageIcon("C:\\PracticeJava\\redshd.gif"));
                   }
          }

}


0 comments:

Post a Comment