Java AWT | Choice Class

24 Oct, 2019
Choice class is part of Java Abstract Window Toolkit(AWT). The Choice class presents a pop- up menu for the user, the user may select an item from the popup menu. The selected item appears on the top. The Choice class inherits the Component.
Constructor for the Choice class
Choice() : creates an new empty choice menu .
Different Methods for the Choice Class
Below programs illustrate the Choice class in Java AWT:
Program to create a simple choice and add elements to it:// Java Program to create a simple // choice and add elements to it .import java.awt.*;import javax.swing.*;  class choice {      // choice    static Choice c;      // frame    static JFrame f;      // default constructor    choice()    {    }      // Main Method    public static void main(String args[])    {          // create a frame        f = new JFrame("choice");          // create e panel        JPanel p = new JPanel();          // create a choice        c = new Choice();          // add element to the list        c.add("Andrew");        c.add("Arnab");        c.add("Ankit");          // add choice to panel        p.add(c);          // add panel to the frame        f.add(p);          // show the frame        f.show();        f.setSize(300, 300);    }}Output :
// Java Program to create a simple // choice and add elements to it .import java.awt.*;import javax.swing.*;  class choice {      // choice    static Choice c;      // frame    static JFrame f;      // default constructor    choice()    {    }      // Main Method    public static void main(String args[])    {          // create a frame        f = new JFrame("choice");          // create e panel        JPanel p = new JPanel();          // create a choice        c = new Choice();          // add element to the list        c.add("Andrew");        c.add("Arnab");        c.add("Ankit");          // add choice to panel        p.add(c);          // add panel to the frame        f.add(p);          // show the frame        f.show();        f.setSize(300, 300);    }}
Output :
Program to create a simple choice and add ItemListener to it:// Java  Program to create a simple// choice and add ItemListener to itimport java.awt.*;import javax.swing.*;import java.awt.event.*;  class choice implements ItemListener {      // choice    static Choice c;      // frame    static JFrame f;      // label    static Label l;      // default constructor    choice()    {    }      // Main Method    public static void main(String args[])    {          // create a frame        f = new JFrame("choice");          // object        choice ch = new choice();          // create e panel        JPanel p = new JPanel();          // create a choice        c = new Choice();          // add element to the list        c.add("Andrew");        c.add("Arnab");        c.add("Ankit");          // add itemListener to it        c.addItemListener(ch);          // create a label        l = new Label();          // set the label text        l.setText(c.getSelectedItem() + " selected");          // add choice to panel        p.add(c);        p.add(l);          // add panel to the frame        f.add(p);          // show the frame        f.show();        f.setSize(300, 300);    }      // if an item is selected    public void itemStateChanged(ItemEvent e)    {        l.setText(c.getSelectedItem() + " selected");    }}Output :
// Java  Program to create a simple// choice and add ItemListener to itimport java.awt.*;import javax.swing.*;import java.awt.event.*;  class choice implements ItemListener {      // choice    static Choice c;      // frame    static JFrame f;      // label    static Label l;      // default constructor    choice()    {    }      // Main Method    public static void main(String args[])    {          // create a frame        f = new JFrame("choice");          // object        choice ch = new choice();          // create e panel        JPanel p = new JPanel();          // create a choice        c = new Choice();          // add element to the list        c.add("Andrew");        c.add("Arnab");        c.add("Ankit");          // add itemListener to it        c.addItemListener(ch);          // create a label        l = new Label();          // set the label text        l.setText(c.getSelectedItem() + " selected");          // add choice to panel        p.add(c);        p.add(l);          // add panel to the frame        f.add(p);          // show the frame        f.show();        f.setSize(300, 300);    }      // if an item is selected    public void itemStateChanged(ItemEvent e)    {        l.setText(c.getSelectedItem() + " selected");    }}
Output :
Program to create a choice and manually add elements to it (using add(String s) function):// Java Program to create a choice and// manually add elements to it // (using add(String s) function)import java.awt.*;import javax.swing.*;import java.awt.event.*;  class choice implements ItemListener, ActionListener {      // choice    static Choice c;      // frame    static JFrame f;      // label    static Label l;      // textfield    static TextField tf;      // default constructor    choice()    {    }      // Main Method    public static void main(String args[])    {          // create a frame        f = new JFrame("choice");          // object        choice ch = new choice();          // create e panel        JPanel p = new JPanel();          // create a choice        c = new Choice();          // add element to the list        c.add("Andrew");          // create a textfield        tf = new TextField(7);          // create a button        Button b = new Button("ok");          // add actionListener        b.addActionListener(ch);          // add itemListener to it        c.addItemListener(ch);          // create a label        l = new Label();        Label l1 = new Label("add names");          // set the label text        l.setText(c.getSelectedItem() + " selected");          // add choice to panel        p.add(c);        p.add(l);        p.add(l1);        p.add(tf);        p.add(b);          // add panel to the frame        f.add(p);          // show the frame        f.show();        f.setSize(250, 300);    }      // if an item is selected    public void itemStateChanged(ItemEvent e)    {        l.setText(c.getSelectedItem() + " selected");    }      // if button is pressed    public void actionPerformed(ActionEvent e)    {        // add item to the choice        c.add(tf.getText());    }}Output :
Program to create a choice and manually add elements to it (using add(String s) function):
// Java Program to create a choice and// manually add elements to it // (using add(String s) function)import java.awt.*;import javax.swing.*;import java.awt.event.*;  class choice implements ItemListener, ActionListener {      // choice    static Choice c;      // frame    static JFrame f;      // label    static Label l;      // textfield    static TextField tf;      // default constructor    choice()    {    }      // Main Method    public static void main(String args[])    {          // create a frame        f = new JFrame("choice");          // object        choice ch = new choice();          // create e panel        JPanel p = new JPanel();          // create a choice        c = new Choice();          // add element to the list        c.add("Andrew");          // create a textfield        tf = new TextField(7);          // create a button        Button b = new Button("ok");          // add actionListener        b.addActionListener(ch);          // add itemListener to it        c.addItemListener(ch);          // create a label        l = new Label();        Label l1 = new Label("add names");          // set the label text        l.setText(c.getSelectedItem() + " selected");          // add choice to panel        p.add(c);        p.add(l);        p.add(l1);        p.add(tf);        p.add(b);          // add panel to the frame        f.add(p);          // show the frame        f.show();        f.setSize(250, 300);    }      // if an item is selected    public void itemStateChanged(ItemEvent e)    {        l.setText(c.getSelectedItem() + " selected");    }      // if button is pressed    public void actionPerformed(ActionEvent e)    {        // add item to the choice        c.add(tf.getText());    }}
Output :
Note: The programs might not run in an online IDE please use an offline IDE.
Reference : https://docs.oracle.com/javase/7/docs/api/java/awt/Choice.html
ManasChhabra2
Java-AWT
Java
Java Programs
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Stream In Java
Introduction to Java
Constructors in Java
Exceptions in Java
Generics in Java
Java Programming Examples
Convert Double to Integer in Java
Implementing a Linked List in Java using Class
Factory method design pattern in Java
Java Program to Remove Duplicate Elements From the Array