AWT Choice and List in Java with Examples

AWT Choice and List in Java with Examples

beginning with programming logo

Table of Contents

a) Introduction to Choice
b) Constructor of Choice
c) Example with source code and output
d) Introduction to List
e) Constructor of List
f) Example with source code and output

***************

Choice:

In AWT, Choice is used to create a drop down list in an application. It means that is used to the select the item from the list of items in the combo box. But only we can select one item at a time in combo box. In SWING we use JComboBox to create the drop down menu. In AWT Choice, scroll bar is also automatically added when there are more than eight (8) items in the list. It generates the ItemEvent every time when an item is selected from the choice.

The constructor of Choice class is Choice(). It has only one constructor.
Methods that is used to create the Choice class are:

add(String) or addItem(String) – both of the methods is used to add the item in the list.
getSelectedIndex() – this method is used to get the selected index of the item. It returns the integer value.
getSelectedItem() – this method is used to get the item selected of the drop down list. It returns the object type.

Let go through the example of Choice with source code and its output:

Source Code:
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package AWTComponents;

/**
 *
 * @author AnkitPC
 */
/*
Create an AWT GUI with a dropdown list and a textfield. When item in the list is selected
,display it in the textfield
*/

import java.awt.*;
import java.awt.event.*;
public class ChoiceDemo extends WindowAdapter implements ItemListener {
    Frame f;
    TextField t;
    Choice c;
// constructor
    ChoiceDemo(){
        f=new Frame("Choice Demo");
        // set size
        f.setSize(400,300);
        // set layout
        f.setLayout(new FlowLayout());
       
        String items[]={"a","b","c","d","e","f","g","h","i"};
       
        t=new TextField(15);
        c=new Choice();
      
        // adding items to the list
        for(int i=0;i<items.length;i++){
             c.addItem(items[i]);
        }
        // registering the item event
        c.addItemListener(this);
        // registering the window event
        f.addWindowListener(this);
         // add components to the frame
        f.add(t);
        f.add(c);
        f.setVisible(true);
    }
    // main method
    public static void main(String[] args) {
        // call constructor
        new ChoiceDemo();
    }
    // overriding the methods of WindowAdapter and ItemListener
     // to close the frame because it is AWT frame
    public void windowClosing(WindowEvent e){
        f.dispose();
    }
    // performing action to display the output to text field.
    public void itemStateChanged(ItemEvent e){
        String s=c.getSelectedItem().toString();
        t.setText(s);
       
    } 
}

Output:
The image below is the initial output of the program.
choice output

choice output


Now you can see I have added 9 items in the list. So scroll bar is appeared to the list.
See the video output also.



List:

List is the class in AWT. List is used to display the multiple items on the display as choice. Unlike choice, we can select more than one item from the list. It means multiple selection of item also can also be done. Unlike Swing JList, we should not add scrollbar manually here. Scroll bar is also automatically added after four (4) items. It also generates the ItemEvent whenever the item is selected from the list.
There are three constructors of the List class, they are;

a) List() – This is the first constructor of the List class. It displays the scroll bar visible after four (4) items in the list.

b) List(int visibleRows) – This is the second constructor of List class. In this constructor we can pass the integer value to set the number of visible rows in the list.

c) List(int visibleRows, boolean isMultipleSelection) – This is the third constructor. As you can see, we should pass two values in this constructor. The first parameter takes the integer value i.e. number of visible rows in the list and the second parameter takes the boolean value i.e. either ‘true’ or ‘false’. True represents multiple selection and False represents single selection.

Let us see the example of List with source code and output;
Source Code:
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package AWTComponents;

/**
 *
 * @author AnkitPC
 */
import java.awt.*;
import java.awt.event.*;
public class ListDemo extends WindowAdapter implements ItemListener {
    Frame f;
    List l;
    TextField t;
    // constructor
    ListDemo(){
        f=new Frame("List Demo");
        // set size of frame
        f.setSize(400,300);
        // set layout
        f.setLayout(new FlowLayout());
        // register window event
        f.addWindowListener(this);
       
        t=new TextField(15);
        // using second constructor
        l=new List(3,true);
        // registering the item event
        l.addItemListener(this);
        String items[]={"a","b","c","d","e","f","g","h","i"};
        // add items to the list
        for(int i=0;i<items.length;i++){
            l.add(items[i]);
        }
       // add components to the frame
        f.add(t);
        f.add(l);
      
        f.setVisible(true);
       
    }
   
    // main method
    public static void main(String[] args) {
        // call constructor
        new ListDemo();  
    }
    // overrind the WindowAdapter class and ItemListener interface
    // closing the window
    public void windowClosing(WindowEvent e){
        f.dispose();
    }
    // performing the action
    public void itemStateChanged(ItemEvent e){
        String s[]=l.getSelectedItems();
        String result="";
        for(String ss:s){
            result+=ss;
        }
        t.setText(result);
    }
}

Output:
The image below is the initial output of the program.
list output

As you can see the multiple selection of the items can be done because I have passed ‘true’ value for isMultipleSlection. You can try giving it value to false in you own.
list output
Also you can watch the video output.


*****************

Post a Comment

0 Comments