0% found this document useful (0 votes)
12 views5 pages

2024 09 25 AWT Choice

The Choice class is used to create a popup menu for user selections, inheriting from the Component class. It includes a constructor and various methods for adding items, handling events, and managing the selection state. Example programs demonstrate how to implement the Choice class in a graphical user interface using Java AWT.

Uploaded by

dk9693126
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views5 pages

2024 09 25 AWT Choice

The Choice class is used to create a popup menu for user selections, inheriting from the Component class. It includes a constructor and various methods for adding items, handling events, and managing the selection state. Example programs demonstrate how to implement the Choice class in a graphical user interface using Java AWT.

Uploaded by

dk9693126
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

25/09/2024

Choice Class
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.

Constructor of Choice Class


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

Choice Class Methods

Sr. Method name Description


no.
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 It gets the accessbile context related to the choice.
getAccessibleContext()
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 It processes the event on the choice.
processEvent(AWTEvent e)
15. protected void processItemEvent Processes item events occurring on this Choice
(ItemEvent e) 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 It removes the mentioned item listener. Thus is
(ItemListener l) 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.

Choice Examples

Program 1
1 // Choice Menu Example 1
2
3 import java.awt.*;
4
5 public class ChoiceExapmle1 {
6
7 ChoiceExapmle1()
8 {
9 Frame f = new Frame ("Choice Menu Example 1 ");
10
11 Choice c= new Choice();
12 c.setBounds(100,100,200,50);
13 f.add(c);
14
15
16 c.add("Advanced Java");
17 c.add("Python");
18 c.add("HTML");
19 c.add("CSS");
20
21
22
23 f.setSize(500,500);
24 f.setLayout(null);
25 f.setVisible(true);
26 }
27 public static void main(String args[]) {
28 ChoiceExapmle1 obj=new ChoiceExapmle1();
29 }
30 }
31

Output :
Program2
1 // Choice Menu Example 1
2
3 import java.awt.*;
4 import java.awt.event.*;
5
6 public class ChoicewithActionListener {
7 Label l;
8 Button b;
9 ChoicewithActionListener()
10 {
11 Frame f = new Frame ("Choice Menu Example 1 ");
12 l= new Label("Hello");
13 l.setBounds(100,250,200,50);
14 f.add(l);
15
16 b=new Button("Submit");
17 b.setBounds(100,200,100,50);
18 f.add(b);
19
20 Choice c= new Choice();
21 c.setBounds(100,100,200,50);
22 f.add(c);
23
24
25 c.add("Advanced Java");
26 c.add("Python");
27 c.add("HTML");
28 c.add("CSS");
29
30
31
32 f.setSize(500,500);
33 f.setLayout(null);
34 f.setVisible(true);
35
36
37
38 b.addActionListener(new ActionListener(){
39 public void actionPerformed(ActionEvent e){
40 String s=c.getItem(c.getSelectedIndex());
41 // String s=" "+c.getSelectedIndex();
42 // System.out.println(s);
43 l.setText(s);
44 }
45 });
46
47
48 }
49 public static void main(String args[]) {
50 ChoicewithActionListener obj=new
ChoicewithActionListener();
51 }
52 }
53

Output :

You might also like