Table of Contents

GUI Absolute Layout

JOptionPane Methods

In SE1010 you were exposed to a few class methods from the JOptionPane class. A class method is a method that is declared as static and is called with the class name before the dot (instead of an object name).

Here are some examples:

JOptionPane.showMessageDialog(null, "Hello.");

Produces this dialog box:

There are two parameters passed to the method.

JOptionPane.showInputDialog(null, "Enter a goofy number", 
				"I am a title, see me roar", JOptionPane.ERROR_MESSAGE);

Produces this dialog box:

Here we have four parameters. The first two are the same as in the showMessageDialog example.

showInputDialog returns a String, but we are ignoring that value since we don't assign the returned value to a String variable.

if(JOptionPane.NO_OPTION == JOptionPane.showConfirmDialog(null, "Play another game?", "Confirmation",
		JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE))
{
	System.out.println("Done");
} else {
	System.out.println("Continuing");
}

Produces this dialog box:

If the user selects the “No” option, then “Done” will be displayed to the console. Otherwise, “Continuing” is displayed to the console.

We could list out what all of the parameters represent, but it would be better for you to get used to reading the Java documentation provided by Sun. There is detailed documentation for the JOptionPane class with a specific entry for the five parameter showConfirmDialog method used above.

I encourage you to spend some time getting familiar with how to read Sun's documentation pages. Once you get comfortable with how to interpret these pages, you'll find them very handy.

Object[] options = { "Bye", "Second", "Eric" };
JOptionPane.showOptionDialog(null, "Game over; you're broke.", "Title",
		JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE,
		null, options, options[1]);

Produces this dialog box:

This method allows you to present the user with an array of options and specify the default option.

JFrame

The JFrame class provides us with a content pane on which we can place a number of GUI components. The content pane is a Container object. A Container is a generic AWT object that allows multiple AWT components to be added to it. In order to create a JFrame object and access its content pane, we need to do something like this:

JFrame frame = new JFrame("Title bar text");
Container pane = frame.getContentPane();

Once we have a content pane, we can start adding GUI components to it. Some useful GUI components include: JButton, JLabel, and JTextField (take a look at the “Direct Known Subclasses” of JComponent for others).

In addition to adding components, the content pane has a number attributes that can be set. These attributes are set using setter methods like setSize(), setLocation(), setBackground(), setDefaultCloseOperation(), etc…

Once all of the components have been added and the attributes have been set as desired, we must tell the JFrame object to be visible (by default, the object is not visible). This is done via a call to setVisible().

Absolute Positioning

The GUI components that are added the JFrame object's content pane must find a physical location where they will be displayed. The content pane's layout manager is responsible of determining where the components are drawn. If we want complete control over the positioning of components on the content pane, we can tell it to not use a layout manager. Here's how:

pane.setLayout(null);

We can specify the bounds of the components that we wish to add to the content pane. This is done by way of the setBounds() method. For example,

JFrame frame = new JFrame("Absolute Position Layout");
frame.setSize(250,100);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container pane = frame.getContentPane();
pane.setLayout(null);
 
JButton button = new JButton("ClickMe");
button.setBounds(10, 30, 80, 30);
pane.add(button);
 
frame.setVisible(true);

produces: