Search This Blog

ADVANCED JAVA

C-PROGRAMMING

Wednesday, December 28, 2022

Java AWT Choice

 

Java AWT Choice

The object of Choice class is used to show popup menu of choices. Choice selected by user is shown on the top of a menu. It inherits Component class.

AWT Choice Class Declaration

  1. public class Choice extends Component implements ItemSelectable, Accessible  

Choice Class constructor

Sr. no.ConstructorDescription
1.Choice()It constructs a new choice menu.

Methods inherited by class

The methods of Choice class are inherited by following classes:

  • java.awt.Component
  • java.lang.Object

Choice Class Methods

Sr. no.Method nameDescription
1.void add(String item)It adds an item to the choice menu.
2.void addItemListener(ItemListener l)It adds the item listener that receives item events from the choice menu.
3.void addNotify()It creates the peer of choice.
4.AccessibleContext getAccessibleContext()It gets the accessbile context related to the choice.
5.String getItem(int index)It gets the item (string) at the given index position in the choice menu.
6.int getItemCount()It returns the number of items of the choice menu.
7.ItemListener[] getItemListeners()It returns an array of all item listeners registered on choice.
8.T[] getListeners(Class listenerType)Returns an array of all the objects currently registered as FooListeners upon this Choice.
9.int getSelectedIndex()Returns the index of the currently selected item.
10.String getSelectedItem()Gets a representation of the current choice as a string.
11.Object[] getSelectedObjects()Returns an array (length 1) containing the currently selected item.
12.void insert(String item, int index)Inserts the item into this choice at the specified position.
13.protected String paramString()Returns a string representing the state of this Choice menu.
14.protected void processEvent(AWTEvent e)It processes the event on the choice.
15.protected void processItemEvent (ItemEvent e)Processes item events occurring on this Choice menu by dispatching them to any registered ItemListener objects.
16.void remove(int position)It removes an item from the choice menu at the given index position.
17.void remove(String item)It removes the first occurrence of the item from choice menu.
18.void removeAll()It removes all the items from the choice menu.
19.void removeItemListener (ItemListener l)It removes the mentioned item listener. Thus is doesn't receive item events from the choice menu anymore.
20.void select(int pos)It changes / sets the selected item in the choice menu to the item at given index position.
21.void select(String str)It changes / sets the selected item in the choice menu to the item whose string value is equal to string specified in the argument.

Java AWT Choice Example

In the following example, we are creating a choice menu using Choice() constructor. Then we add 5 items to the menu using add() method and Then add the choice menu into the Frame.

ChoiceExample1.java

  1. // importing awt class  
  2. import java.awt.*;   
  3. public class ChoiceExample1 {    
  4.   
  5.          // class constructor  
  6.         ChoiceExample1() {    
  7.   
  8.         // creating a frame  
  9.         Frame f = new Frame();    
  10.   
  11.         // creating a choice component  
  12.         Choice c = new Choice();   
  13.   
  14.         // setting the bounds of choice menu   
  15.         c.setBounds(1001007575);    
  16.   
  17.         // adding items to the choice menu  
  18.         c.add("Item 1");    
  19.         c.add("Item 2");    
  20.         c.add("Item 3");    
  21.         c.add("Item 4");    
  22.         c.add("Item 5");    
  23.   
  24.         // adding choice menu to frame  
  25.         f.add(c);    
  26.   
  27.         // setting size, layout and visibility of frame  
  28.         f.setSize(400400);    
  29.         f.setLayout(null);    
  30.         f.setVisible(true);    
  31.      }    
  32.   
  33. // main method  
  34. public static void main(String args[])    
  35. {    
  36.    new ChoiceExample1();    
  37. }    
  38. }     

Output:

java awt choice example 1

Java AWT Choice Example with ActionListener

In the following example, we are creating a choice menu with 5 items. Along with that we are creating a button and a label. Here, we are adding an event to the button component using addActionListener(ActionListener a) method i.e. the selected item from the choice menu is displayed on the label when the button is clicked.

ChoiceExample2.java

  1. // importing necessary packages  
  2. import java.awt.*;    
  3. import java.awt.event.*;   
  4.   
  5. public class ChoiceExample2 {  
  6.   
  7.       // class constructor    
  8.         ChoiceExample2() {    
  9.   
  10.         // creating a frame  
  11.         Frame f = new Frame();    
  12.   
  13.         // creating a final object of Label class  
  14.         final Label label = new Label();   
  15.   
  16.         // setting alignment and size of label component           
  17.         label.setAlignment(Label.CENTER);    
  18.         label.setSize(400100);   
  19.   
  20.         // creating a button   
  21.         Button b = new Button("Show");   
  22.   
  23.         // setting the bounds of button   
  24.         b.setBounds(2001005020);    
  25.   
  26.         // creating final object of Choice class  
  27.         final Choice c = new Choice();    
  28.   
  29.         // setting bounds of choice menu  
  30.         c.setBounds(1001007575);   
  31.   
  32.         // adding 5 items to choice menu   
  33.         c.add("C");    
  34.         c.add("C++");    
  35.         c.add("Java");    
  36.         c.add("PHP");    
  37.         c.add("Android");   
  38.   
  39.         // adding above components into the frame   
  40.         f.add(c);  
  41.         f.add(label);  
  42.         f.add(b);    
  43.   
  44.         // setting size, layout and visibility of frame  
  45.         f.setSize(400400);    
  46.         f.setLayout(null);    
  47.         f.setVisible(true);    
  48.   
  49.         // adding event to the button  
  50.         // which displays the selected item from the list when button is clicked  
  51.         b.addActionListener(new ActionListener() {    
  52.             public void actionPerformed(ActionEvent e) {         
  53.          String data = "Programming language Selected: "+ c.getItem(c.getSelectedIndex());    
  54.          label.setText(data);    
  55.         }    
  56.         });             
  57.         }    
  58.   
  59. // main method  
  60. public static void main(String args[])    
  61. {    
  62.    new ChoiceExample2();    
  63. }    
  64. }    

Output:

java awt choice example 2
Next Topic

0 comments:

Post a Comment

OS

Top