0% found this document useful (0 votes)
13 views

Dynamic_Button_Generator_Instructions

Java GUI study question

Uploaded by

cemmetesaritas
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Dynamic_Button_Generator_Instructions

Java GUI study question

Uploaded by

cemmetesaritas
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

CS 343 Fall 2024 Lab Assignment 3

Dynamic Button Generator

Objective
Create a Java Swing application where user dynamically generate buttons based on a
number entered in a text field. Each button will be assigned a single action listener, and
clicking a button will display its label or index in a message dialog.

Overview
Build a Swing GUI application that allows the user to input a number, generate that number
of buttons dynamically on the window, and handle button clicks with a single action
listener. A sample execution of the program given in Figure 1.

Figure 1. A sample execution of the required program

Instructions
1. Set up the GUI:
- Create a `JFrame` as the main application window.
- Add a `JTextField` for inputting the number of buttons to create.
- Add a `JButton` labeled 'Generate Buttons' for triggering the button generation.
- Add a `JPanel` to dynamically hold the generated buttons.

2. Implement button generation:


- When the 'Generate Buttons' button is clicked, read the number from the `JTextField`.
- Validate the input to ensure it is a positive integer.
- Clear any existing buttons in the panel.
- Dynamically create the specified number of buttons and add them to the panel.
3. Assign a single action listener:
- Attach the same `ActionListener` to all dynamically created buttons (same class but it
might be different instances of that class).
- When a button is clicked, display a `JOptionPane` message dialog showing the button's
label or index.

4. Add error handling:


- Validate that the input is a positive integer.
- If the input is invalid, display an error message using `JOptionPane`.

Requirements
1. Use Swing components like `JLabel`, `JTextField`, `JButton`, and `JOptionPane`.
2. Dynamically generate buttons in response to user input.
3. Use a layout manager like `FlowLayout` or `GridLayout` for arranging the buttons.
4. Handle user input validation and exceptions gracefully.

Deliverables
Submit the Java program (`DynamicButtonGenerator.java`) with the following features:
- Correct implementation of dynamic button creation.
- Proper event handling for button clicks.
- User input validation and error messages for invalid inputs.

You might also like