Java SWING Tutorial: Container, Components and Event Handling

Overview of Java Swing:

Java Reflection was explained in detail in our previous tutorial. This tutorial will explain about Java Swing.

Swing is mainly used for creating GUI for desktop applications.

In this tutorial, you can explore the basics of swing in the following order and get an idea on how to build a Desktop app.

  • Swing Container
  • Components
  • Event Handling

Read through our Full Training Series on Java for enriching your knowledge on Java concept.

Here is a Video tutorial on Java Swing:

Introduction

When you create an application, initially you should have a base container and you have to add the required components like buttons and text fields in the container.

And when you click or perform any operation on any fields, the event will occur and your code should listen to the events and also handle the event.

Swing Container

A container is a root element for an Application. All the other components are added to that root and it forms a hierarchy.

There are three container classes:

  • JFrame
  • JDialog
  • JApplet

Container Demo using JFrame:

 import java.awt.Color;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class ContainerDemo {

public static void main(String[] args) {

JFrame baseFrame =new JFrame();
baseFrame.setTitle("Base Container");
JPanel contentPane=new JPanel();

contentPane.setBackground(Color.pink);
baseFrame.setSize(400, 400);

baseFrame.add(contentPane);

baseFrame.setDefaultCloseOperation(baseFrame.EXIT_ON_CL
OSE);

baseFrame.setVisible(true);
}

} 

Container Demo using JFrame

When you run the above program, you will get the below output.

base container

Components

JComponent class is a base class for all the components in a swing.

The frequently used components include,

  • JButton
  • JTextField
  • JTextArea
  • JRadioButton
  • JComboBox etc.

All these components should be added to the container if not, it will not appear on the application.

Example:

To create the button instance,

JButton clickButton=new JButton();

To add the button to the container,

myFrame.add();

Event Handling

All the Applications are driven by events like button clicks, mouse clicks, user text input etc. When the event occurs, you have to add a listener and must pass the source event object.

With an inner class, you can handle the event with your logic as shown below.

 public class ContainerDemo {

public void createApp() {
JFrame baseFrame =new JFrame();
JPanel contentPane=new JPanel();
baseFrame.setTitle("Base Container");
baseFrame.setSize(400, 400);
baseFrame.add(contentPane);
JButton demoButton =new JButton("click");
demoButton.setBounds(100,95,95,30);
JTextArea result =new JTextArea();
result.setBounds(130,140,95,30);
contentPane.add(demoButton);
contentPane.add(result);

baseFrame.setDefaultCloseOperation(baseFrame.EXIT_ON_CL
OSE);

baseFrame.setVisible(true);
demoButton.addMouseListener(new
MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
result.setText("button clicked");
}
});
}

public static void main(String[] args) {

ContainerDemo c =new ContainerDemo();
c.createApp();
}
} 

Event Handling

Conclusion

A container is the root element for any app. In the container, you have to add the required components like buttons, text field, radio buttons etc.

All the components should be added to the container using “add(component)method. The events (like button clicks) can be handled by adding the event listeners.

You will learn more about Java Deployment in our upcoming tutorial !!!

PREV Tutorial | NEXT Tutorial