Choice in Java AWT

Choice class is part of Java Abstract Window Toolkit(AWT). The Choice class presents a pop- up menu for the user, the user may select an item from the popup menu.

This Java AWT program demonstrates the use of the Choice component, which is used to create a drop-down list of items, along with a Button and a Label. The program creates a new Frame named MyApp and sets its size and layout to null (i.e., absolute positioning). It then creates a Choice component named c and adds several programming languages to it.

It also creates a Button named btn with a label "Show Details" and sets its bounds. When the button is clicked, it gets the selected item from the Choice component and sets the Label named lbl with the selected item. The Label initially displays "Empty Label" and is positioned below the Choice and Button.

Finally, the program adds the Choice, Button, and Label to the Frame, and adds a WindowListener to the Frame to exit the program when the window is closed. The main method creates an instance of MyApp to display the GUI.

Source Code

package awtDemo;
import java.awt.*;
import java.awt.event.*;
//Choice in Java AWT
class MyApp extends Frame{
	Choice c;
	Button btn;
	Label lbl;
	public MyApp() {
		super("Tutor Joes");
		setSize(1000, 600);// w,h
		c = new Choice();
		c.setBounds(10, 50, 100, 100);
		btn = new Button("Show Details");
		btn.setBounds(120, 50, 100, 20);
		btn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String data = "Programming language Selected: " + c.getItem(c.getSelectedIndex());
		lbl = new Label("Empty Label");
		lbl.setBounds(10, 70, 300, 30);
		// Close Button Code
		this.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent we) {
public class app {
	public static void main(String[] args) {
		MyApp frm = new MyApp();
To download raw file Click Here


Java AWT

Basic Programs