In this article Swing in Java we give the information about  java swing is a part of java foundation classes (JFC) which is used to create applications based on Windows.

Swing in java:-

Swing has been created to fulfill the shortcomings of the Abstract Windows toolkit. AWT library provides basic GUI controls. Swing is an advanced version of AWT. There are 2 problems with AWT.

  • AWT programs are very large in size.
  • AWT components remain fixed, you cannot change them much.

Swing removes both these problems. Swing is based on AWT. Therefore swing does not replace AWT.

java swing is a part of java foundation classes (JFC) which is used to create applications based on Windows.

It is built on the AWT (abstract windowing toolkit) API and is written entirely in Java.

Swing API is used by developers to create front end/GUI applications based on Java. And swing API is used as a substitute for AWT API.

In Java, Javax.swing package provides classes for swing API like:- JButton, JComboBox, JList, JTabbedPane, JToolbar, JTextArea, JRadioButton and JPasswordField etc.

Java swing features:-

1:- Platform independent:- The components of swing are platform independent, that is, they run in any operating system.

2:- Light weight:- Since swing components are completely written in Java, hence they are light weight.

3:- controls:- There are many good controls in swing like:- tree, TabbedPane, Slider, ColorPicker and Table controls etc.

4:- customize:- We can easily customize the swing controls.

5:- Look:- The look and feel of applications based on swing can also be changed at runtime.

Methods of java swing components classes:-

method description
public void add(Component c) It is used to add one component to another.
public void setSize(int width,int height) It is used to set the size of the component..
public void setLayout(LayoutManager m) It is used to set the layout manager for the component.
public void setVisible(boolean b) It is used to set the visibility of the component.

Example:- simple program of java swing

import javax.swing.JFrame;
import javax.swing.swingUtilities;

public class App {

public static void main(string[] args) {

swingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = name JFrame(“hello world Swing!”);
frame.setSize(500, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}};

}

}

Swing classes

  • JWindow – JWindow represents the container class hierarchy. JFrame and JDialog classes make this class higher.
  • JFrame – JFrame class is based on AWT’s frame class. This is a container class. This is your base window in which you design your application by adding other components. In this you can add different components like buttons, labels etc. To add components you use add() method.
  • JDialog – By creating an object of this class you can insert Dialog box in your application.
  • JComponent – ​​This class represents all the components in the swing hierarchy. All swing components directly inherit this class.
  • JLabel – By creating an object of this class you can add text labels to your application.
  • JButton – By creating an object of this class you can add a button to your window. While creating its object, you pass a string which is the name of the button.
  • TextField – By creating an object of this class you can add text box in your application.
  • JScrollBar – This class is used to add scroll bar to the application.
  • JMenuBar – Through this class you can add menu bar to your window.
  • JCheckBox – By creating an object of this class you can add check box to your window.
  • JList – This class adds a list to your window.
  • JTextArea – Through this class you can add text area in your application.
  • JRadioButton – This class is used to add radio button to the application.

Example:

public class swingDemo

{

public static void main(String args[])

{

JFrame frame = new JFrame();

JButton button = new JButton(“Click here!”);

button.setBounds(100,100,100,60);

frame.add(button);

frame.setSize(300,300);

f.setVisible(true);

}

}

Swing components and containers:

In Java, swing components are basic user interface objects and are found in all Java applications.

The swing framework has many components that provide great functions and a high level of customization.

To use swing components we need a container. Container provides space to keep components and manage them.

There are two types of containers in it:-

1:- top level containers

2:- light weight containers

1:- Top level container:- It includes components like JFrame, JApplet and JDialog etc. They inherit the components and containers of AWT and these components are very heavyweight.

2:- light weight containers:- Example of light weight container is:- JPanel.

It inherits the JComponent class.

Some important swing components classes:-

Here we will read about some important swing components which are mostly used.

JButton :- JButton class is used to create labeled buttons.

JButton class declaration:- Given below is the declaration of javax.swing.JButton class:-

public class JButton

extends AbstractButton

implements accessible

JButton class constructor:- Following are its main constructors.

  • JButton() – Through this a button is created which has neither text nor icon.
  • JButton(string text)- Through this a button is created which contains text.
  • JButton(Icon icon)- Button is created with icon.
  • JButton(string text, Icon icon)- A button is created with an icon.

JTextField- It is used to edit a single line text.

JTextField class declaration:- Below is the declaration of javax.swing.JTextField class:-

public class JTextField

extends JTextComponent

implements SwingConstants

JTextField class constructor:- Following are the main constructors used in it:-

  • JTextField()- It creates a new textfield.
  • JTextField(string text) – It creates a new TextField which is initialized with a specific text.

JCheckBox:-

It is used to make checkbox.

JCheckBox class declaration:- The declaration of javax.swing.JCheckBox class is given below.

public class JCheckBox

extends JToggleButton

implements accessible

JCheckBox class constructors:-

The constructors mainly used in this are as follows:-

  • JCheckBox():- By this an unselected checkbox is created which has neither text nor icon.
  • JCheckBox(String text):- By this an unselected checkbox is created which contains text.
  • JCheckBox(Icon icon):- By this an unselected checkbox is created which contains an icon.
  • JCheckBox(String text, Icon icon):- By this an unselected checkbox is created which contains both icon and text.

Some More: 

POP- Introduction to Programming Using ‘C’

DS – Data structure Using C

OOP – Object Oriented Programming 

Java Programming

DBMS – Database Management System

RDBMS – Relational Database Management System

Join Now: Data Warehousing and Data Mining 

Leave a Reply

Your email address will not be published. Required fields are marked *