Double Bonanza Offer - Upto 30% Off + 1 Self Paced Course Free | OFFER ENDING IN:0 D 0 H 0 M 0 S

Log In to start Learning

Login via

Post By AdminLast Updated At 2021-04-14
Explain about Java Swing Components and Container class

Swing in Java is a GUI toolkit and a part of JFC (Java Foundation Class) useful in developing window-based apps. Java swing is lightweight and platform-independent that includes various components and container class. Moreover, the Java swing library is built on the top of AWT or Abstract Window Toolkit which is an API and is completely written in Java. In every application, a user can find an interactive and user-friendly interface that gives him the freedom to use the app.

In Java Swing, there are a number of components like a scroll bar, button, text field, text area, checkbox, radio button, etc. All these components together, form a GUI that offers a rich set of functionalities and also allows high-level customization. 

Furthermore, Java swing components are the interactive elements of the Java application. 

Hence, This is in brief about the Java Swing and its component types. In this blog, we are going to explore things in detail about the process of developing applications using Java Swing components.

What is Java swing?

Java swing is nothing but a GUI toolkit with lightweight that provides various components to build optimized windows based apps. This involves JFC or Java Foundation Classes and built on the top of AWT API with platform-independent features. JFC is an Application Programming Interface that provides GUI to Java programs. Also, this is useful in developing apps easily through GUI components like radio-button, text-field, etc. and it doesn’t require starting from scratch. 

A Graphical User Interface is a type of visual experience builder for many Java apps that is easy to use. This includes various components like buttons, text, labels, etc.

Java Swing components

Moreover, all the components within Java Swing are the JComponent that is added to the container classes in Java. Java AWT or Abstract Window Toolkit is an API useful to build Graphical User Interface apps within Java. The AWT components in Java are displayed based on the Operating System’s view and the Java AWT is a heavyweight API.

Get more details on Java applications and their real-time use through expert guidance by opting for Core Java Online Course

Overall, Java AWT and Swing are two different areas and they possess some key differences.

Difference between AWT and Java swing 

The following are the key difference between Java AWT and Java Swing.

Components of Java AWT and Swing are platform-independent.

The components of Java AWT are of the heavyweight structure whereas Swing components are lightweight.

The AWT components do not follow MVC but Swing components follow.

Moreover, AWT components are lesser powerful whereas Swing has more powerful components.

The support for pluggable look and feel is absent in Java AWT components but Swing components support them. 

Java Swing Container class structure

The Java swing class structure includes different types of components such as radio-button, JList, JBox, JLabel are inherited from the JComponent class. These are added later to the container class. Further, the container classes are the windows similar to the frames and dialog boxes. The basic swing components are the base for developing GUI apps. Components such as JFrame and JDialog box can add a component to themselves only. 

There are three different types of Java Swing Containers such as -

Panel: The panel is a pure container but doesn’t a window. The main purpose of this container is to arrange the components onto a window.

Dialog: The container dialog is similar to a pop-up window that pops out whenever a message is displayed. But this container is not a fully functioning window similar to the Frame container.

Frame: It is a fully functioning window with its title and icons.

||{"title":"Master in Core Java", "subTitle":"Core Java Certification Training by ITGURU's", "btnTitle":"View Details","url":"https://onlineitguru.com/core-java-online-training-placement.html","boxType":"demo","videoId":"Vi7pdpk5Vq0"}||

Top-level Containers

It takeovers the Component and Container of the AWT Abstract Window Toolkit.

This cannot be included within other containers.

Heavyweight Containers

The Example includes JFrame, JDialog, and  JApplet containers.

Lightweight Containers

These types of containers takeovers the JComponent class.

This is a general-purpose container system in Java Swing containers.

Also, this is useful to arrange related components together in one place.

The Example is: Java JPanel

Let us understand the different types of components of the Java Swing class in detail in the following content.

Java swing components

The following are the important components of the Java Swing class.

JButton Class

The JButton Class within the Java swing components is the component useful to build a labeled button. Moreover, this button has platform-independent execution. Further, when the button is pushed it makes some action as a result and it also inherits the AbstractButton class. There are some commonly used constructors of the JButton class such as JButton(), JButton (String S), and JButton (Icon i). 

Java JButton Example

The following code explains the JButton class in Java Swing.

import javax.swing.*;    

public class JButton Example {  

public static void main(String args[]) {  

    JFrame x = new JFrame ("Example JButton");  

    JButton y = new JButton ("Click this");  

    y.setBounds(30,90,85,20);  

    x.add(b);  

    x.setSize(200,200);  

    x.setLayout(null);  

    x.setVisible(true);   

}  

}  

The output will come here as - “Click This”

Imagelcon

This Java swing component helps to build an icon-sized image from the image that occupies the source URL.

The code snippet for this component is as follows -

ImageIcon homepageIcon = latest ImageIcon(“source/images/home.jpg”);

JLabel

The JLabel class object within the swing component is useful for placing text in a container. Also, this class is useful to present a single line of text in read-only. There is an option to change the text later by an application but a user has no authority to edit directly. Also, the JLabel component inherits JComponent class. The constructors that JLabel uses are the following types. Such as JLabel(), JLabel (String S), (Icon i), and JLabel(String s, Icon I, int horizontal alignment).

The following is the example code for the JLabel component.

import javax.swing.*;

class Example{

public static void main(String args[])

{

JFrame x = new JFrame("example of Label");

JLabel y1;

y1 = new JLabel("Label 2");

y1.setBounds(60,60,95,30);

x.add(y1);

x.setSize(500,500);

x.setLayout(null);

x.setVisible(true);

}

}
Java JTextField

The JTextField class object inherits the JTextComponent class and it allows the modification of a single line of text.

The below is the example code for the Java JTextField component.


import javax.swing.*;

public class exampleTextField{

public static void main(String args[]) {

Text Field x, y;

JFrame x = new JFrame("example Text Field");

JTextField y = new JTextField("XYZ");

y.setBounds(40,100,200,20);

x.add(y);

x.setSize(350,350);

x.setLayout(null);

x.setVisible(true);

}

}
Java JTextArea

The JTextArea class object includes multiple lines region that presents the text. Also, this component takeovers the JTextComponent class and allows the modification of the multiple-line text. Here, the constructors of this component include the following.-

JTextArea(), JTextArea(int row, int column), etc.

The example code for the JTextArea component is as follows.-

import javax.swing.*;  

public class example 

{  

     TextArea Example (){  

        JFrame x = new JFrame();  

        JTextArea area = new JTextArea("Open the window");  

        area.setBounds(20,40,100,300);  

        y.add(area);  

        y.setSize(250,250);  

        y.setLayout(null);  

        y.setVisible(true);  

     }  

public static void main(String args[])  

    {  

   new TAExample();  

    }

}  
JList Class

The JComponent Class takeovers the JList Class where the JList Class represents the object of the list of text items. Hence, the constructors that JList class includes the following as JList (), JList(ary[] list data), etc.

The following code snippet presents the JList Class.

import javax .swing.*;

public class JListExample

{

Example(){

JFrame x  = new JFrame("JList example");

DefaultListModel l1 = new DefaultListModel< >();

l1.addElement("01 item");

l1.addElement("02 item");

JList y = new JListmodel < >(l);

y.setBounds(100,100,65,65);

x.add(y);

x.setSize(300,300);

x.setVisible(true);

x.setLayout(null);

}

public static void main (String args[])

{

new JList Example();

}

}
JComboBox Class

The choice class is useful to present the popup of the menu of choices and it also takeovers the JComponent class. Whatever choice the user selects it shows on the top of the menu. 

The constructors of the JComboBox Class are as follows-

JComboBox(), JComboBox(Object[] items), etc.

The below syntax is the example code for JComboBox class which is-

import javax.swing.*;

public class JComboBoxExample {

JFrame x ;

JcomboBox Example (){

x = new JFrame("JCombobox Example");

string class[] = { "class 1","class 2", "class 3"};

JComboBox y  = new JComboBox (classes);

y.setBounds(30,40,80,20);

x.add(y);

x.setSize(250,250);

x.setLayout(null);

x.setVisible(true);

}

public static void main(String args [])

{

New JCombo Example();

}

}  
JTable

The Java Swing component class object of JTable is useful to present the data in a tabular format. This table composes various rows and columns. The JTable constructors include JTable(), JTable (Object[][] rows, Object[][] columns), etc. 

The below for the JTable code is as follows.:-

import javax.swing.*;    

public class ExampleJTable {    

    JFrame x;    

    JTableExample (){    

    X = new JFrame ();    

    String data [][] = { {"001","Ameet","650000"},    

                          {"002","Jagan","580000"},    

                          {"003","Sagar","680000"}};    

    String column [] = {"EmpID","EmpNAME","EmpSALARY"};         

    JTable jt = latest Table (data,column,);    

    jt.setBounds(20,30,100,400);          

    JScrollPane ep = new JScrollPane (jt);    

    x.add(ep);          

    x.setSize(200,300);    

    x.setVisible(true);    

}     

public static void main (String args[]) {    

    new ExampleJTable ();    

}    

}  


Java JScroll bar

This JScrollBar is useful to add a horizontal and vertical scrollbar and is an implementation of the scroll bar. Also, the JScrollbar inherits the JComponent Class. 

||{"title":"Master in Core Java", "subTitle":"Core Java Certification Training by ITGURU's", "btnTitle":"View Details","url":"https://onlineitguru.com/core-java-online-training-placement.html","boxType":"reg"}||

Java JTree

The Java Swing component JTree class is useful to present the tree-structured data and also inherits Jcomponent. This is a complex component and includes the root node that is the parent node for all other nodes of the tree. The constructors of the JTree component class include-

JTree(), JTree(Object value[]), and the JTree(Tree Node root).

JCheckBox

This Java swing component is useful to build the checkbox. Also, it is useful to turn an option on or off that represents true or false. The JCheckbox inherits the JToggleButton class and by clicking on the check box it modifies the state of on/off options. This check box commonly uses the constructors such as JJCheckBox(), JCheckBox(Action X), JCheckBox (String s), etc.

Java Layout Manager

The Java Layout manager is useful to arrange the GUI components within the container. There are different types of layout managers but some of the regularly used are-

  • Java Border Layout
  • GridBag Layout
  • Flow Layout

Java Border layout- This is a default layout manager for JFrame and also places five different types of component places. They are right, left, top, bottom, and center.

Flow layout - It simply lays out the components within a row one by one and is a default layout manager for JPanel.

Java GridBagLayout - This layout places the components within a grid that allows them to extend to more than one cell. It helps them to span more.

Final thoughts

Hence, the above details specify the Java Swing components and the container class structure. Java swing class components are platform-independent and lightweight structures. They form a Graphical user interface toolkit that allows rich set functions along with customization. These components are very much useful in developing Java apps in different ways. To learn these components and other Java aspects through expert guidance, go through the Core Java Online Training with the ITGuru platform. This learning can help you to explore the latest skills in Java and its various components.