AddingButtonWithActionListener.java Source code

Java tutorial

Introduction

Here is the source code for AddingButtonWithActionListener.java

Source

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class AddingButtonWithActionListener {

    public static void main(String[] a) {
        JFrame frame = new JFrame();
        JOptionPane optionPane = new JOptionPane();
        optionPane.setMessage("I got an icon and a text label");
        optionPane.setMessageType(JOptionPane.INFORMATION_MESSAGE);
        Icon icon = new ImageIcon("yourFile.gif");
        JButton jButton = getButton(optionPane, "OK", icon);
        optionPane.setOptions(new Object[] { jButton });
        JDialog dialog = optionPane.createDialog(frame, "Icon/Text Button");
        dialog.setVisible(true);

    }

    public static JButton getButton(final JOptionPane optionPane, String text, Icon icon) {
        final JButton button = new JButton(text, icon);
        ActionListener actionListener = new ActionListener() {
            public void actionPerformed(ActionEvent actionEvent) {
                // Return current text label, instead of argument to method
                optionPane.setValue(button.getText());
                System.out.println(button.getText());
            }
        };
        button.addActionListener(actionListener);
        return button;
    }

}