Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
AWT Choice
2.1.
Syntax
3.
Choice Constructors
4.
Choice Methods
5.
Example
5.1.
Code 1
5.2.
Code 2
6.
Frequently Asked Questions
6.1.
What is the use of a choice class in Java?
6.2.
What are AWT classes in Java?
6.3.
How to show the popup menu of choices in Java AWT?
7.
Conclusion
Last Updated: Mar 27, 2024
Easy

AWT Choice

Crack Google SDE interview : Essential projects
Speaker
Saurav Prateek
SDE-2 @
20 Jun, 2024 @ 01:30 PM

Introduction

The choice becomes one of the essential aspects of programming when choosing one of them based on the user's choice. The choice control displays a popup menu of options. The top of the menu shows the selected option. In this article, we shall be learning about AWT Choice in Java.

Read More About, Basics of Java

AWT Choice

The Java Abstract Window Toolkit includes the Choice class (AWT). The Choice class displays a popup menu to the user, from which the user can pick an item. The selected item is shown at the top of the list. The Choice class inherits the Component class.

Syntax

public class Choice extends Component implements ItemSelectable, Accessible
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Choice Constructors

Choice Methods

You can also read about the Multiple Inheritance in Java.

Example

This section will look for a few codes related to AWT Choice.

Code 1

import java.awt.*;
import javax.swing.*;
public class AWTChoiceExample {


static Choice c;


    static JFrame f;
  
    public static void main(String args[])
    {


        f = new JFrame("choice");


        JPanel p = new JPanel();
  
        c = new Choice();


        c.add("Mutiur");
        c.add("Yawar");
        c.add("Saalim");
  
        p.add(c);
  
        f.add(p);


        f.show();

        f.setSize(300, 300);

    }

}

Output

We have created a choice list in this example and added a few names.

Code 2

import java.awt.*;    
import java.awt.event.*;
public class AWTChoiceDemo {
  
AWTChoiceDemo() {    
  
    Frame f = new Frame();    
 
    final Label label = new Label();   
           
    label.setAlignment(Label.CENTER);    
    label.setSize(400, 100);   
   
    Button b = new Button("Show");   
 
    b.setBounds(200, 100, 50, 20);    
 
    final Choice c = new Choice();    


    c.setBounds(100, 100, 75, 75);   


    c.add("C");    
    c.add("C++");    
    c.add("Java");    
    c.add("PHP");    
    c.add("Android");   
   
    f.add(c);  
    f.add(label);  
    f.add(b);    
  
    f.setSize(400, 400);    
    f.setLayout(null);    
    f.setVisible(true);    
  
    b.addActionListener(new ActionListener() {    
        public void actionPerformed(ActionEvent e) {         
     String data = "Programming language Selected: "+ c.getItem(c.getSelectedIndex());    
     label.setText(data);    
    }    
    });             
    }    
 
public static void main(String args[])    
{    
new AWTChoiceDemo();    
}    
}    

Output

In this article, we have created a choice list, and the user selects one of them, which is shown by the side of the list.

Try it by yourself on java online compiler.

Frequently Asked Questions

What is the use of a choice class in Java?

The Java Abstract Window Toolkit includes the Choice class (AWT). The Choice class displays a popup menu to the user, from which the user can pick an item. The selected item is shown at the top of the list. The Choice class inherits the Component class. 

What are AWT classes in Java?

TextField, Label, TextArea, RadioButton, CheckBox, Choice, List, and other AWT API classes are available in java. awt package.

How to show the popup menu of choices in Java AWT?

The Choice class object is used to display a popup menu of options. A user-selected option appears at the top of a menu.

Conclusion

In this article, we have extensively discussed the AWT Choice. We have briefly introduced the topic along with its methods and constructors. We also have discussed a few codes with their output and solution, which explains the purpose of this blog.

If you want to explore more about AWT, visit here.

You can improve your skills in  Data Structures and AlgorithmsCompetitive ProgrammingJavaScriptSystem Design, and more with our Coding Ninjas Studio  Guided Path. If you want to sharpen your coding skills to the test, check out the mock test series and enter the contests on Coding Ninjas Studio! If you're just getting started to know what questions big giants like Amazon, Microsoft, and Uber ask, check the problemsinterview experiences, and interview bundle for placement preparations.

We hope that this blog has helped you in enhancing your knowledge. 

"Happy Coding!".

Previous article
AWT CheckBox
Next article
AWT List
Live masterclass