import javax.swing.*;
import javax.swing.event.*;
import javax.swing.SwingUtilities;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.net.URL;

public class ButtonDemo extends JApplet
                        implements ActionListener {

    protected JButton b1, b2, b3;

    URL codeBase;         // used for browser version only....
    URL leftButtonURL;    // URL for left button image ...
    URL middleButtonURL;  // URL for left button image ...
    URL rightButtonURL;   // URL for right button image ...

    public void init() {

        // 1. Get a reference to content pane of the applet...

        Container contentPane = this.getContentPane();

        // 2. Construct URL for image....

        codeBase = getCodeBase();
        try {
           leftButtonURL = new URL( codeBase, "images/left.gif" );
        } catch ( java.net.MalformedURLException e ) {
           System.out.println("Badly specified URL!!");
        }

        try {
           middleButtonURL = new URL( codeBase, "images/middle.gif" );
        } catch ( java.net.MalformedURLException e ) {
           System.out.println("Badly specified URL!!");
        }

        try {
           rightButtonURL = new URL( codeBase, "images/right.gif" );
        } catch ( java.net.MalformedURLException e ) {
           System.out.println("Badly specified URL!!");
        }

        // 3. Download and save image icon ....

        ImageIcon leftButtonIcon   = new ImageIcon( leftButtonURL );
        ImageIcon middleButtonIcon = new ImageIcon( middleButtonURL );
        ImageIcon rightButtonIcon  = new ImageIcon( rightButtonURL );

        // 5. Create buttons ....

        b1 = new JButton("Disable middle button", leftButtonIcon);
        b1.setVerticalTextPosition(AbstractButton.CENTER);
        b1.setHorizontalTextPosition(AbstractButton.LEFT);
        b1.setMnemonic(KeyEvent.VK_D);
        b1.setActionCommand("disable");

        b2 = new JButton("Middle button", middleButtonIcon);
        b2.setVerticalTextPosition(AbstractButton.BOTTOM);
        b2.setHorizontalTextPosition(AbstractButton.CENTER);
        b2.setMnemonic(KeyEvent.VK_M);

        b3 = new JButton("Enable middle button", rightButtonIcon);

        // Use the default text position of CENTER, RIGHT.

        b3.setMnemonic(KeyEvent.VK_E);
        b3.setActionCommand("enable");
        b3.setEnabled(false);

        // 6. Listen for actions on buttons 1 and 3.

        b1.addActionListener(this);
        b3.addActionListener(this);

        b1.setToolTipText("Click this button to disable the middle button.");
        b2.setToolTipText("This middle button does nothing when you click it.");
        b3.setToolTipText("Click this button to enable the middle button.");

        // 7. Create 1-by-3 grid layout for buttons
        //    Horizontal gap = 5, vertical gap 5.

        GridLayout grid = new GridLayout (1, 3, 5, 5 );
        contentPane.setLayout( grid );

        // 8. Add buttons to container ....

        contentPane.add(b1);
        contentPane.add(b2);
        contentPane.add(b3);
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals("disable")) {
            b2.setEnabled(false);
            b1.setEnabled(false);
            b3.setEnabled(true);
        } else { 
            b2.setEnabled(true);
            b1.setEnabled(true);
            b3.setEnabled(false);
        }
    }
}
