Choice defines only the default constructor, which creates an empty list....................
Choice controls:
The Choice class is used to create a pop-up list of items from which the user may choose. Thus, a Choice control is a form of menu. When inactive, a Choice component takes up only enough space to show the currently selected item. When the user clicks on it, the whole list of choices pops up, and a new selection can be made. Each item in the list is a string that appears as a left-justified label in the order it is added to the Choice object. Choice defines only the default constructor, which creates an empty list.
choice control: constructors
- Choice(): Creates a new choice menu.
choice control:methods
- add( ):To add a selection to the list, call add( ).
syntax:
void add(String name)
- getSelectedIndex():Returns the index of the currently selected item.
syntax:
int getSelectedIndex()
- getSelectedItem():Gets a representation of the current choice as a string.
syntax:
string getSelectedItem()
- getItemCount() : Returns the number of items in this Choice menu.
syntax:
int getItemCount()
- getItem(int index): Gets the string at the specified index in this Choice menu.
syntax:
String getItem(int index)
sample program for choice controls
import java.awt.*;
import java.awt.event.*;
public class AwtControlDemo
{
private Frame mainFrame;
private Label headerLabel;
private Label statusLabel;
private Panel controlPanel;
public AwtControlDemo()
{
prepareGUI();
}
public static void main(String[] args)
{
AwtControlDemo awtControlDemo = new AwtControlDemo();
awtControlDemo.showChoiceDemo();
}
private void prepareGUI()
{
mainFrame = new Frame("Java AWT Examples");
mainFrame.setSize(400,400);
mainFrame.setLayout(new GridLayout(3, 1));
mainFrame.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent windowEvent)
{
System.exit(0);
}
});
headerLabel = new Label();
headerLabel.setAlignment(Label.CENTER);
statusLabel = new Label();
statusLabel.setAlignment(Label.CENTER);
statusLabel.setSize(350,100);
controlPanel = new Panel();
controlPanel.setLayout(new FlowLayout());
mainFrame.add(headerLabel);
mainFrame.add(controlPanel);
mainFrame.add(statusLabel);
mainFrame.setVisible(true);
}
private void showChoiceDemo()
{
headerLabel.setText("Control in action: Choice");
final Choice fruitChoice = new Choice();
fruitChoice.add("Apple");
fruitChoice.add("Grapes");
fruitChoice.add("Mango");
fruitChoice.add("Peer");
Button showButton = new Button("Show");
showButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String data = "Fruit Selected: " + fruitChoice.getItem(fruitChoice.getSelectedIndex());
statusLabel.setText(data);
}
});
controlPanel.add(fruitChoice);
controlPanel.add(showButton);
mainFrame.setVisible(true);
}
}
output of this program:
No comments :
Post a Comment