Swing Components

The Component class is the abstract superclass of many of the interactive visual objects that are available in Swing. A component is something that has a position, a size, can be painted on the screen, and can receive mouse and keyboard inputs.

Container

A container is a component that holds other components.

A Panel is a container that all applets extend. Therefore all applets inherit a default Panel. Various other components (including other Panels) can he placed into a Panel.

When creating complex component layouts we can place several other Panels into the default Panel.

For example, a Panel might contain three Buttons (for example this.home, this.next and this.prev). By positioning the Panel at the bottom of the screen we automatically position the three Buttons together at the bottom of the screen.

Container example: (Run Applet)

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

public class ContainerDemo extends JApplet
{
    @Override
    public void init()
    {
        this.setContentPane(new View());
    }

    public class View extends JPanel
    {
        public View()
        {
            super();

            final JPanel navigation = new JPanel();
            final JButton home = new JButton("HOME");
            final JButton previous = new JButton("PREVIOUS");
            final JButton next = new JButton("NEXT");

            // navigation panel
            navigation.setLayout(new GridLayout(1, 3));
            navigation.add(home);
            navigation.add(previous);
            navigation.add(next);

            // default panel for applet
            this.setLayout(new BorderLayout());
            this.add("South", navigation);
        }
    }
}

Label

An instance of the Label class can be used to hold non-editable text. To construct a label use:

Label()
Label(String s)
Label(String s, int alignment) // where alignment is Label.LEFT, Label.RIGHT or Label.CENT 

To write/overwrite the contents of a label use:

setText(String s)  

To read the contents of a label use:

String getText();  

To change the alignment of a label use:

setAlignment(int alignment) // alignment is Label.LEFT, Label.RIGHT or Label.CENTER 

Label Fxample: (Run Applet)

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

public class LabelDemo extends JApplet
{
    @Override
    public void init()
    {
        this.setContentPane(new View());
    }

    public class View extends JPanel
    {
        public View()
        {
            super();

            JLabel labelNorth = new JLabel("North");
            JLabel labelSouth = new JLabel();

            setLayout(new BorderLayout());
            labelSouth.setText("South");

            add("North", labelNorth);
            add("South", labelSouth);
        }
    }
}

Button

Use a Button to trigger an event.

To create a button use:

Button (String name) 

Button Example: (Run Applet)

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

public class ButtonDemo extends JApplet
{
    @Override
    public void init()
    {
        this.setContentPane(new View());
    }

    public class View extends JPanel implements ActionListener
    {
        private JButton button1 = new JButton("Button 1");
        private JButton button2 = new JButton("Button 2");

        public View()
        {
            super();
            this.button1.setBackground(Color.red);
            this.button2.setBackground(Color.yellow);

            this.add(this.button1);
            this.add(this.button2);

            this.button1.addActionListener(this);
            this.button2.addActionListener(this);
        }

        @Override
        public void actionPerformed(ActionEvent e)
        {
            final Object source = e.getSource();
            if (source == this.button1)
            {
                this.button1.setVisible(false);
                this.button2.setVisible(true);
            }
            else if (source == this.button2)
            {
                this.button2.setVisible(false);
                this.button1.setVisible(true);
            }
        }
    }
}
 
<div align="center"><a href="../versionC/index.html" title="DKIT Lecture notes homepage for Derek O&#39; Reilly, Dundalk Institute of Technology (DKIT), Dundalk, County Louth, Ireland. Copyright Derek O&#39; Reilly, DKIT." target="_parent" style='font-size:0;color:white;background-color:white'>&nbsp;</a></div>