How to dropdown in AWT program in java

import java.awt.*;

public class ChoiceExample
{
    ChoiceExample()
    {
        Frame f = new Frame();
        Choice c = new Choice();
        c.setBounds(100, 100, 75, 75);
        
        c.add("Item 1 by Rajendra");
        c.add("Item 2 by Sarvam");
        c.add("Item 3 by TechSarvam");
        c.add("Item 4");
        c.add("Item 5");
        
        f.add(c);
        f.setSize(400, 400);
        f.setLayout(null);
        f.setVisible(true);
    }
    
    public static void main(String args[])
    {
        new ChoiceExample();
    }
}

This program discusses how to employ the Choice class with Java’s Abstract Window Toolkit. It creates an application with an easy GUI whose dropdown menu features several items in it.

Step-by-Step Explanation

1. Import Statement:

import java.awt.*;

At the beginning, the program needs to import java.awt.*. This package is required here because we intend to use components of AWT, such as Frame and Choice.

2. Class Declaration:

public class ChoiceExample

The class ChoiceExample is declared. This is the class that contains all the code for the GUI application.

3. Constructor (ChoiceExample():

ChoiceExample() {

Frame f = new Frame(); // Create a new Frame (window)

Choice c = new Choice(); // Create a new Choice component (dropdown menu)

  • The constructor ChoiceExample() is called when an instance of the class is created (i.e., in the main() method).
  • A Frame object f is created, which represents the main window of the application.
  • A Choice object c is declared. This is a dropdown menu for the user to choose from.

4. Setting Bounds of the Choice:

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

  • The c.setBounds(x, y, width, height) specifies the position and size of the Choice component in the window.
  • Here, the drop-down list will be visible at the point (100, 100) with a length of 75 and a height of 75.

5. Adding Items to the Choice:

c.add(“Item 1 by Rajendra”);

c.add(“Item 2 by sarvam”);

c.add(“Item 3 by TechSarvam”);

c.add(“Item 4”);

c.add(“Item 5”);

  • The add() method is used to add several items to the dropdown menu. These items will be shown in the list for the user to choose from.
  • Five items are added in total, each corresponding to a string.

6. Adding the Choice to the Frame:

f.add(c); // Add the Choice component to the Frame

Now: Add the component c, which is actually a Choice component into the frame f via the add() method: This will reveal the dropdown inside the frame, so the items are visible by default.

7. Configuration of Frame’s Properties:

f.setSize(400, 400);  // Sets up the window of size 400×400 in pixels

f.setLayout(null);  // Change the layout from default to make absolute positioning easier

f.setVisible(true);   // Make it visible

  • f.setSize(400, 400) sets the size of the window to 400×400 pixels.
  • f.setLayout(null) disables the default layout manager, allowing for absolute positioning of the components (in this case, the Choice menu).
  • f.setVisible(true) makes the window visible on the screen.

8. Main Method (main():

public static void main(String args[]) {

new ChoiceExample(); // Create an instance of ChoiceExample (invokes the constructor)

 }

  • The main() method is the entry point of the Java application.
  • It creates a new instance of ChoiceExample, which triggers the constructor to run and display the GUI.

Output of the Program:

When the program is run, the following occurs:

  • There will be a window (frame) of 400×400 pixels.
  • Within the frame, there will be a drop-down menu that is a Choice component located at the coordinates (100, 100) with dimensions 75×75 pixels.

The menu items will have five items, as follows

  1. Item 1 by Rajendra

  2. Item 2 by sarvam

  3. Item 3 by TechSarvam

  4. Item 4

  5. Item 5

  • The user can select any item from the list, but no specific action is defined in the program for selection, so it will just show the list of items in the dropdown.
  • The window will remain visible until it is closed by the user.

Diagram:

———————————————————

|                Frame 400×400 pixels                |

|——————————————————-|

|   | Item 1 by Rajendra  |                               |

|   | Item 2 by sarvam    |                               |

|   | Item 3 by TechSarvam |                            |

|   | Item 4              |                               |

|   | Item 5              |                               |

|   —————————————————————————|

|       Choice Component at (100,100) with size 75×75   |

———————————————————

Key Points:

  • The Choice component is a dropdown list for selecting an item.
  • The setBounds() method controls the position and size of the Choice component.
  • The window size is set to 400×400 pixels with absolute layout.
  • No event handling or interaction (like detecting the selection) is implemented in this code.

If you wish to capture the selection of an item from the dropdown, you would need to add an event listener like this:

nc.addItemListener(new ItemListener() {

    public void itemStateChanged(ItemEvent e) {

        System.out.println(“Selected item: ” + c.getSelectedItem());

    }

});

This would print the selected item whenever the user selects an option from the dropdown.

Leave a Comment

Your email address will not be published. Required fields are marked *