Add a button to the given container, including an action listener and a text to be shown on the button - Java Swing

Java examples for Swing:JButton

Description

Add a button to the given container, including an action listener and a text to be shown on the button

Demo Code


//package com.java2s;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;

public class Main {
    /**/*from w w  w  . ja  v  a 2  s  . c  om*/
     * Add a button to the given container, including an action listener and a text to be shown
     * on the button
     * @param Container c is the container to add the button to
     * @param String title is the title to show in the button
     * @param ActionListener listener is the event handler for the button
     */

    public static void addButton(Container c, String title,
            ActionListener listener) {
        JButton button = new JButton(title);
        c.add(button);
        button.addActionListener(listener);

    }
}

Related Tutorials