Java AWT Change Label Text based on Button Events

Java AWT Change Label Text based on Button Events :

This code is continuation of this Java Frame Change Button Colors Based on Even Odd Logic.

Quite a bit has changed since last version. I have put some codes into functions to make them organize. I will not separate the code into multiple files though to keep it simple. Copy the codes below and save them in a file called

Be aware of  the case of the file name as the class name should match file name. Change in this code from last version is ActionListener interface is implemented to handle button events. It has only one method ActionPerformed which is used to deal different actions. Here i change label based on which button was clicked.

Here I’ve used setActionCommand to identify which key was pressed based on my assigned command.


button events change label 1
button events change label 1
button events change label 2
button events change label 2



import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

 * Created by asif on 8/15/2015.
public class SimpleAwtGui implements ActionListener {
    private static final int button_count = 10;

    // Declare a Frame type variable
    Frame frame;
    MenuBar menuBar;
    Menu fileMenu, optionsMenu, helpMenu;
    Panel panelUpperSection, panelLowerSection;
    Label labelButtonPressHeading, labelButtonPressed;

    // Create an array of Button type Objects
    Button[] button = new Button[button_count];

    public SimpleAwtGui() {
        // Create Frame Object and pass in the Frame Name / title
        frame = new Frame("AWT GUI EXAMPLE");

        // Create MenuBar Object
        menuBar = new MenuBar();


    private void createMenu() {
        // Create Menu objects to add to the MenuBar
        fileMenu = new Menu("File");
        optionsMenu = new Menu("Options");
        helpMenu = new Menu("Help");

    private void createPanels() {
        // Create a panel object for organizing content
        panelUpperSection = new Panel();
        panelLowerSection = new Panel();

    private void createLabels() {
        // create Label
        labelButtonPressHeading = new Label();
        labelButtonPressed = new Label();

    private void createButtons() {
        // Use for loop to instantiate every button object
        for (int i = 0; i < button_count; ++i) {
            button[i] = new Button("" + i);
            button[i].setActionCommand("" + i);

            // if i is odd then change the button color
            if (i % 2 == 1) {

    // Not necessary but good practice all codes inside this can be written inside main
    public void showFrame() {
        // set the size of the window
        frame.setSize(400, 400);

        // set the layout for the window
        frame.setLayout(new GridLayout(2, 1));



        // Add all of the buttons to the layout
        for (int i = 0; i < button_count; ++i) {

        // handles various window events

        // Add Menu to MenuBar

        // Add MenuItem to Menu

        // Add MenuBar to the Frame

        // set the frame visible otherwise nothing will be shown

    private void addPanels() {
        // Add panel to the frame

        // Set Layout for the panels
        panelUpperSection.setLayout(new BorderLayout());
        panelLowerSection.setLayout(new GridLayout());

        // Add Label to Panel
        panelUpperSection.add(labelButtonPressHeading, BorderLayout.NORTH);
        panelUpperSection.add(labelButtonPressed, BorderLayout.CENTER);

    private void addLabels() {
        labelButtonPressHeading.setText("Buttons clicked will be shown Below:");


    private void handleWindowEvents() {
        // Register window listener event to the frame without implementing WindowListener
                new WindowAdapter() {
                    public void windowClosing(WindowEvent e) {

    private void addMenuBarMenus() {
        // Add Menu to the MenuBar

    private void addMenuItems() {
        // Add items to File Menu
        fileMenu.add(new MenuItem("New"));
        fileMenu.add(new MenuItem("Open"));
        fileMenu.add(new MenuItem("Help"));

        // Add items to File Menu
        optionsMenu.add(new MenuItem("Options"));

        // Add items to File Menu
        helpMenu.add(new MenuItem("Help"));
        helpMenu.add(new MenuItem("About"));

    public void actionPerformed(ActionEvent e) {
        int i = Integer.parseInt(e.getActionCommand());

        if (i % 2 == 1) {
            labelButtonPressed.setText("Odd buttons Pressed");
        } else {
            labelButtonPressed.setText("Even buttons Pressed");

    public static void main(String[] args) {

        // Create an instance of SimpleAwtGui
        SimpleAwtGui gapp = new SimpleAwtGui();

        // call the showFrame() function to display the window



One thought on “Java AWT Change Label Text based on Button Events

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s