Java Dialog Examples

Simple extended Button

import javax.swing.*;
import java.awt.*;
/**
 * Write a description of class MyButton here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class MyButton extends JButton
{
	public static final int BUTTON_WIDTH  = 120;
	public static final int BUTTON_HEIGHT = 30;

	public MyButton(String s) {
		super(s);
		setBackground(Color.red);
	}
	
	// Sizing the button seems NOT to work?
	public Dimension getPreferredSize() {
		return new Dimension( BUTTON_WIDTH, BUTTON_HEIGHT );
	}

	public Dimension getMinimumSize() {
		return getPreferredSize();
	}
}

Dialog Examples

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

/**
 * Write a description of class SimpleMessage here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class SimpleMessage extends JFrame
{
	public static final int WIDTH         = 200;
	public static final int HEIGHT        = 400;

	Container pane;
	Box box = Box.createVerticalBox();

	MyButton b1 = new MyButton("Simple OK");
	MyButton b2 = new MyButton("Warning Icon");
	MyButton b3 = new MyButton("Fancy Dialog");
	MyButton b4 = new MyButton("Simple Input");

	public SimpleMessage() {
		super("My simple dialog examples");
		addWindowListener( new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});

		pane = getContentPane();

		box.add(b1);
		box.add(Box.createVerticalGlue());
		box.add(b2);
		box.add(Box.createVerticalGlue());
		box.add(b3);
		box.add(Box.createVerticalGlue());
		box.add(b4);
		box.add(Box.createHorizontalStrut(100));

		pane.add(box, BorderLayout.CENTER);

		setSize(WIDTH, HEIGHT);
		show();
	}

	public static void main(String [] args) {
		SimpleMessage app = new SimpleMessage();
		app.setUpListeners(app);
	}

	public void setUpListeners(final JFrame frame) {
		b1.addActionListener( new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JOptionPane.showMessageDialog(frame, 
						"Eggs aren't supposed to be green.");
			}
		});

		b2.addActionListener( new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JOptionPane.showMessageDialog(frame,
    				"Eggs aren't supposed to be green.",
    				"Inane warning",
    				JOptionPane.WARNING_MESSAGE);
			}
		});

		b3.addActionListener( new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Object[] options = {"Yes, please",
                    		"No, thanks",
                    		"No eggs, no ham!"};

				int n = JOptionPane.showOptionDialog(frame,		
    				"Would you like some green eggs to go "
    				+ "with that ham?",
    				"A Silly Question",
    				JOptionPane.YES_NO_CANCEL_OPTION,
    				JOptionPane.QUESTION_MESSAGE,
    				null,
    				options,
    				options[2]);

				JOptionPane.showMessageDialog(frame, 
						"Selection: " + n);

			}
		});

		b4.addActionListener( new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String response;
				response = JOptionPane.showInputDialog(
										"Whazzz up");
				JOptionPane.showMessageDialog(frame, 
						"Response: " + response);

			}
		});

	}
}