(back to the documentation page)
To display a Swing component, you must:
Construct and initialize the component.
Example: button = new JButton ("ButtonLabel");
Add it to the content pane of the window or to a JPanel that is added to the display, and validate the content pane after adding all components. For example:
Container contentPane = getContentPane();
contentPane.add (button);
...
contentPane.validate();Import javax.swing.* and often java.awt.* in the class creating the components.
To handle events from a GUI component, you must do the following:
Declare that the class handling the event implements the appropriate listener interface.
Example: implements ActionListener
Define the method that the listener interface requires.
Example: public void actionPerformed(ActionEvent event)
Add a listener appropriate for the component to the component.
Example: button.addActionListener(this);
Import java.awt.event.* (and occasionally javax.swing.event.*) at the beginning of the class that is the listener.
When the listener method is called, you can find out which component sent the event by calling getSource() on the event:
public void actionPerformed (ActionEvent event) {
    Object theButton = event.getSource();
    if (theButton == framedCircleButton) {
        // Create a framed circle
    }
}If a method returns a String or Object, remember to compare the result using the .equals() method, not ==. For example:
aMenu.getSelectedItem().equals("A value")
The following methods can be applied to any Component:
void setFont(Font f)
void setForeground(Color c)
void setBackground(Color c)To construct a font use:
new Font(String name, int style, int size)Find out the font names on the computer in Eclipse as follows:
Font.BOLD, Font.ITALIC, Font.PLAIN, or `Font.BOLDThe specific components we have considered:
JButton
| Constructor | new JButton (String s) | 
| General methods | String getText()void setText(String s) | 
| Listener interface | ActionListener | 
| Adding the listener | void addActionListener(ActionListener al) | 
| Listening method | void actionPerformed(ActionEvent e) | 
JComboBox
| Constructor and initialization | new JComboBox()void addItem(Object item) | 
| To find out which item was selected | String getSelectedItem() | 
| To find out the index of the item that was selected | int getSelectedIndex() | 
| Listener interface | ActionListener | 
| Adding the listener | void addActionListener(ItemListener il) | 
| Listening method | void actionPerformed(ActionEvent e) | 
JLabel
| Constructors | new JLabel(String s) new JLabel(String s, int align)   (align is one of JLabel.RIGHT, JLabel.LEFT, or JLabel.CENTER) | 
| General methods | void setText(String s) String getText() | 
| Listener interface | no listeners | 
JSlider
| Constructor | new JSlider (int orientation, int minimum, int maximum, int initialValue)( orientation is either JSlider.HORIZONTAL or JSlider.VERTICAL) | 
| To set the value | void setValue(int newVal) | 
| To find out the current value | int getValue() | 
| Listener interface | ChangeListener | 
| Adding the listener | addChangeListener(ChangeListener al) | 
| Listening method | void stateChanged(ChangeEvent e) | 
JTextField
| Constructor | new JTextField(String s) | 
| To set the value | void setText(String s) | 
| To find out the value typed | String getText() | 
| Listener interface | ActionListener | 
| Adding the listener | addActionListener(ActionListener al) | 
| Listening method | void actionPerformed(ActionEvent e) | 
Both JPanel and the object obtained by sending getContentPane() to a WindowController object are containers (and have type Container). The following methods are available for all containers. To define the type of layout, use:
void setLayout(LayoutManager lm)LayoutManager may be any of the layout managers listed below. To add something to a container:
void add (Component c)Component may be any Component (such as JButton, JTextField, JSlider, …) or Container (such as JPanel). Use the method above if the container has a FlowLayout or GridLayout. Use the one below if it has a BorderLayout.
void add (Component c, int position)The position may be any of BorderLayout.NORTH, BorderLayout.SOUTH, BorderLayout.EAST, BorderLayout.WEST, or BorderLayout.CENTER.
Construct a JPanel with new JPanel()
BorderLayout (Default for WindowController)
Constructor:
new BorderLayout()
FlowLayout (Default for JPanel)
Constructor:
new FlowLayout()
GridLayout
Constructors:
new GridLayout(int rows, int cols)
new GridLayout(int rows, int cols, int colSpacing, int rowSpacing)