Introduction to Java Swing

Java Swing

Introduction

It is a part of Java Foundation Classes (JFC) that is used to create window-based applications. Java Swing is platform-independent and can be used on any operating system that supports Java. It provides a set of lightweight components that are not only easy to use but also customizable. Some of the commonly used components in Swing are buttons, text fields, labels, menus, and many more.

What is JFC?

The Java Foundation Classes (JFC) is a graphical framework for building portable Java-based graphical user interfaces (GUIs).

Controls

Every user interface considers the following three main aspects −

UI Elements − these are the core visual elements the user eventually sees and interacts with. GWT provides a huge list of widely used and common elements varying from basic to complex, which we will cover in this tutorial.

Layouts − they define how UI elements should be organized on the screen and provide a final look and feel to the GUI (Graphical User Interface). This part will be covered in the Layout chapter.

Behavior − these are the events which occur when the user interacts with UI elements.

Every SWING controls inherit properties from the following Component.

Class and Descriptions

JLabel

A JLabel object is a component for placing text in a container.

JButton

This class creates a labeled button.

JColorChooser

A JColorChooser provides a pane of controls designed to allow a user to manipulate and select a color.

JCheck Box

A JCheckBox is a graphical component that can be in either an on (true) or off (false) state.

JRadioButton

The JRadioButton class is a graphical component that can be in either an on (true) or off (false) state. in a group.

JList

A JList component presents the user with a scrolling list of text items.

JComboBox

A JComboBox component presents the user with to show up menu of choices.

JTextField

A JTextField object is a text component that allows for the editing of a single line of text.

JPasswordField

A JPasswordField object is a text component specialized for password entry.

JTextArea

A JTextArea object is a text component that allows editing of a multiple lines of text.

ImageIcon

A ImageIcon control is an implementation of the Icon interface that paints Icons from Images

JScrollbar

A Scrollbar control represents a scroll bar component in order to enable the user to select from range of values.

JOptionPane

JOptionPane provides set of standard dialog boxes that prompt users for a value or informs them of something.

JFileChooser

A JFileChooser control represents a dialog window from which the user can select a file.

JProgressBar

As the task progresses towards completion, the progress bar displays the task’s percentage of completion.

JSlider

A JSlider lets the user graphically select a value by sliding a knob within a bounded interval.

JSpinner

A JSpinner is a single line input field that lets the user select a number or an object value from an ordered sequence.

The methods of Component class are widely used in java swing that are given below.

MethodDescription
public void add(Component c)add a component on another component.
public void setSize(int width,int height)sets size of the component.
public void setLayout(LayoutManager m)sets the layout manager for the component.
public void setVisible(boolean b)sets the visibility of the component. It is by default false.

About Javax

Java is the core programming language, while Javax is a prefix used for a set of standard extension packages. Java provides the foundation for developing applications, and Javax adds additional functionality and application programming interfaces (APIs) for specific purposes.

An Example

import javax.swing.*; 

public class myown

public static void main(String[] args)

JFrame x=new JFrame(); 

JButton y=new JButton(“Please Press This Button”); 

y.setBounds(130,100,300, 40);    

x.add(y);

x.setSize(400,500);                                                      width and height 

x.setLayout(null); 

x.setVisible(true);

Setting Actions Simple

import javax.swing.*;

import java.awt.event.*;

import java.awt.*;

public class myown { 

public static void main(String[] args)

JFrame x=new JFrame(); 

JButton y=new JButton(“Please Press This Button”); 

y.setBounds(130,100,400, 100);  

x.add(y);

x.setSize(800,500); 

x.setLayout(null); 

x.setVisible(true); 

y.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent kk) {

        JOptionPane.showMessageDialog(null, “Hello World”);

           }

           });

Question – Put two buttons in a frame and show any message if a button is clicked

Leave a Comment

Scroll to Top