Showing posts with label Choice control in Java AWT. Show all posts
Showing posts with label Choice control in Java AWT. Show all posts

Monday, 18 February 2019

Choice control in Java AWT | Java AWT Choice Control - Online Help

Choice control in Java AWT:

The choice control in java awt is a class which can be used to drop-down down a list of the items to a container. A choice menu is created as an object of the chosen class.

The constructor in java awt choice control:

The choice class has only one constructor.

choice()
it is used to create an empty choice instance.

Methods available in Java AWT Choice Control:

The most commonly used choice() methods are as follows:

void addItemListener(ItemListener Listener)
This method is used to configure an event handler for the choice box.

void insert(String str, int index)
The insert() method is used to insert the string into the choice at the specified position.

void remove(int index)
The remove() is used to remove the item at the Index position.




int getSelectedIndex()
The getSelectedIndex() method is used to return the index of the selected item.

void removeAll()
The removeAll() method is used to remove all items from the choice box.

void setFont(Font strFont)
The setFont() method is used to set the font for the choice box.

void setBackground(Color bgcolor)
The setBackground() method is used to set the background colour of the choice box

void setForeground(Color strcolor)
The setForeground() method is used to set the color of the string for the choice box.

Sample Program:

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
/* <applet code="choicedemo" width=500 height=500>
</applet> */
public class choicedemo extends Applet implements ItemListener
{
Choice c1;
Label l1;
public void init()
{
c1=new Choice();
c1.addItem("manager");
c1.addItem("supervisor");
c1.addItem("staff");
c1.addItemListener(this);
add(c1);
l1=new Label(" ");
add(l1);
}
public void itemStateChanged(ItemEvent ie)
{
Choice c=(Choice)ie.getItemSelectable();
l1.setText(c.getSelectedItem());
}
}