2. Swing Containers

2.1. JFrames

The JFrame class provides windows for applets and applications. GUI based applications generally uses at least one frame.

2.1.1. Code Example

public class FrameWindow extends JFrame {
    boolean inAnApplet;
    JTextArea output;

    public FrameWindow() {
        // Calls the Frame constructor and adds components to the window
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                if (inAnApplet)
                    dispose();
                else System.exit(0);
            }
        });
    }

    . . .

    public static void main(String args[]) {
        FrameWindow window = new FrameWindow();
        window.inAnApplet = false;
        window.setTitle("FrameWindow Application");
        window.setSize(450, 200);
        window.setVisible(true);
    }
}

2.1.2. Other methods provided by JFrame

  • String getTitle() and void setTitle(String)

    Returns or sets (respectively) the title of the frame's window.

  • Image getIconImage() and void setIconImage(Image)

    Returns or sets (respectively) the image displayed when the window is iconified.

  • JMenuBar getJMenuBar() and void setJMenuBar(JMenuBar)

    Returns or sets (respectively) the menu bar for this frame.

Audio in Portuguese

2.2. Panels

The JPanel class is a general-purpose Container subclass. You can use it as-is to hold components, or you can define a subclass to perform special functionality, such as event handling for the objects the JPanel contains.

[Note]Note

The JApplet class is a JPanel subclass with special hooks to run in a browser or in other applet viewer.

2.2.1. Code Example

JPanel p = new JPanel();
p.add(new JButton("Button 1"));
p.add(new JButton("Button 2"));
p.add(new JButton("Button 3"));

A JPanel subclass that draws a frame border around its contents.

Figure 10.6. JPanel subclass.

JPanel subclass.

class FramedArea extends JPanel {
    public FramedArea() {
        . . .
        // Set the layout manager.
        // Add any components to this panel.
    }

    // Ensure that no component is placed on top of the frame.
    // The inset values were determined by trial and error.
    public Insets getInsets() {
        return new Insets(4, 4, 5, 5);
    }

    //Draw the frame at this Panel's edges.
    public void paint(Graphics g) {
        Dimension d = getSize();
        Color bg = getBackground();
        g.setColor(bg);
        g.draw3DRect(0, 0, d.width - 1, d.height - 1, true);
        g.draw3DRect(3, 3, d.width - 7, d.height - 7, false);
    }
}

Audio in Portuguese

2.3. Dialogs

Swing provides support for custom dialogs with the JDialog class. It also provides a useful class, JOptionPane, that enables the creation of a variety of standard dialogs through its class methods.

Figure 10.7. Dialog

Dialog

2.3.1. Code Example

class SimpleDialog extends JDialog implements ActionListener {
    JFrame parent;
    JTextField field;
    JButton setButton;

    SimpleDialog(JFrame parent, String title) {
        super(parent, title, false);
        ...
        // Create and add components, such as the set button.   
        // Initialize this dialog to its preferred size.
        pack();
    }

    public void actionPerformed(ActionEvent event) {
        Object source = event.getSource();
        if (source == setButton)
            parent.setTitle(field.getText());
    }
}
     
// Here's the code that brings up the dialog:
SimpleDialog dialog = new SimpleDialog(this, "A Simple Dialog");
dialog.setVisible(true);

2.3.2. JOptionPane

The JOptionPane class displays standard dialogs that prompts the user for input or shows messages. Some of it's main methods are:

Method NameDescription
showConfirmDialogAsks a confirming question, showing the buttons yes,no or cancel.
showInputDialogPrompt the user for some input.
showMessageDialogTells the user about something.
showOptionDialogThe unification of the above three.

The example below cretes a dialog that promps user to choose an option in a list.

Object[] options = { "Option 0", "Option 1", "Option 2", "Option 3" };
JOptionPane.showInputDialog(this, "Message", "Title", JOptionPane.QUESTION_MESSAGE,
                            null, options, options[2]); 

Figure 10.8. A JOptionPane input dialog

A JOptionPane input dialog

Audio in Portuguese