top

Search

Java Tutorial

.

UpGrad

Java Tutorial

Java Swing

Java is a prominent general-purpose programming language that is used in developing mobile and desktop applications, web applications, big data processing, server-side technologies, cloud computing, etc. This advanced language is fast, secure, and reliable so it is widely used, and popular among developers. Java provides a large set of libraries that are used to create graphical user interfaces (GUI) in a platform-independent way. Java Swing is a lightweight and platform-independent class that belongs to the Java foundation class. It is used in developing the graphical user interface for Java class. It includes some improved features to enhance the look and functionality of GUI. Swing supports model/view design architecture. It offers powerful components like tables, lists, scroll panes, color chooser, tabbed-pane, etc.

Overview

Initially, a brief idea about Java and Swing is presented. After that complete information about swing is explained which covers various topics including AWT, swing, Java foundation class, the hierarchy of Java Swing Classes, commonly used methods of component, features of swing class, swing classes hierarchy, the MVC connection, etc. Different types of examples are explained to give a clear idea about Java swing, swing by association inside the constructor, swing by inheritance, etc.

Difference Between AWT and Swing 

1. The Abstract Window Toolkit (AWT) is an application programming interface that supports a graphical user interface (GUI) or window-based applications in Java. Swing belongs to the Java foundation class library and it is also an extension of AWT. Swing is used to develop Java-based front-end GUI applications.

2. Swing supports much more improved functionality than AWT. It supports new components, expanded component features, and exceptional event-handling features which include drag-and-drop support.

3. Components of AWT are heavily weighted whereas components of AWT are light-weighted.

4. AWT is platform-dependent and swing is platform-independent.

5. Swing has much more advanced components whereas the components of  AWT are not that much advanced.

6. Execution speed of the swing is faster and the execution speed of the AWT is not much faster.

7. Swing supports a pluggable look and feel whereas AWT does not support it.

8. Swing supports MVC patterns.

9. Java Swing Components require more memory space whereas components of AWT don't require much memory space..

What Is JFC?

JFC stands for Java Foundation Classes. JFC supports a wide range of GUI components and services. This makes the development and deployment of desktop and internet applications easier. JFC supports swing, cut and paste, accessibility features, desktop color features, JAVA- 2D, etc. Java swing tutorial is a segment of JFC that is used in developing window-based applications.

Hierarchy Of Java Swing Classes 

The hierarchy of the Java swing w3schools begins with the component class. Different types of user interface elements such as buttons, levels, and text fields are derived from the Java swing class.

Commonly Used Methods Of Component 

Some methods of a component class are listed below.

1. public void add(Component c): This method is used to add two components.

2. public void setSize(int width,int height): This method is used to assign the size of a component. This method sets the width and height of the component.

3. public void setLayout(LayoutManager m): This method is used to assign the layout manager.

4. public void setVisible(boolean b): This method sets the visibility of the component.

Java Swing Examples 

Example: A simple Java swing program

import javax.swing.*;  
public class SwingExample 
{  
public static void main(String[] args) 
{  
JFrame f=new JFrame();          
JButton b=new JButton("click");
b.setBounds(120,50,50, 40);          
f.add(b);          
f.setSize(200,300);
f.setLayout(null);
f.setVisible(true);
}  
}  

Explanation:

1. JFrame and JButton are created.

2. Values of all the bounds such as x-axis, y-axis, width, and height are set.

3. JButton is added in JFrame.

4. The class JButton creates a labeled button with platform-independent implementation. It is used for the implementation of a push button.

JFrame provides a frame in the specific  graphics configuration of the screen

Simple Java Swing Example

Example: A program to create three buttons with captions PAST, PRESENT, and FUTURE.

import java.awt.*;
class button
 {
            button()
 {
                        Frame f = new Frame();
                        Button b1 = new Button("PAST");
                        b1.setBounds(110, 75, 75, 60);
                        f.add(b1);
                        Button b2 = new Button("PRESENT");
                        b2.setBounds(110, 120, 40, 80);
                        f.add(b2);
                        Button b3 = new Button("FUTURE");
                        b3.setBounds(110, 120, 60, 40);
                        f.add(b3);
                        f.setSize(250, 250);
                        f.setLayout(null);
                        f.setVisible(true);
            }


            public static void main(String a[]) 
{
                        new button();
            }
}

Example : A program to create three buttons with captions ODISHA, CHHATTISGARH, BIHAR

import java.awt.*;
class Lan {
            Lan() {
                        Frame f = new Frame();


                        Label l1 = new Label("Select your state");


                        l1.setBounds(110, 60, 140, 100);
                        f.add(l1);


                        Checkbox c2 = new Checkbox("ODISHA");
                        c2.setBounds(110, 100, 80, 80);
                        f.add(c2);
                        Checkbox c3 = new Checkbox("CHHATTISGARH");
                        c3.setBounds(100, 120, 70, 50);
                        f.add(c3);
                        Checkbox c4 = new Checkbox("BIHAR");
                        c4.setBounds(120, 150, 60, 80);
                        f.add(c4);


                        f.setSize(250, 250);
                        f.setLayout(null);
                        f.setVisible(true);
            }


            public static void main(String ar[]) {
                        new Lan();
            }
}

Example Of Swing By Association Inside Constructor 

Example: Create a program using Java constructor

import javax.swing.*;  
public class Simple 
{  
JFrame f;  
Simple()
{  
f=new JFrame();          
JButton b=new JButton("click");
b.setBounds(120,110,100, 50);         
f.add(b);          
f.setSize(300,500);//300 width and 500 height  
f.setLayout(null);//using no layout managers  
f.setVisible(true);//making the frame visible  
}    
public static void main(String[] args) {  
new Simple();  
}  
}  

Simple Example Of Swing By Inheritance 

Example : Create a program using inheritance

import javax.swing.*;  
public class Simple2 extends JFrame
{
JFrame g;  
Simple2()
{  
JButton c=new JButton("click"); 
c.setBounds(120,100,110, 50);  
add(c);  
setSize(200,300);  
setLayout(null);  
setVisible(true);  
}  
public static void main(String[] args)
 {  
new Simple2();  
}
}  

Features Of Swing Class 

1. Lightweight components: A swing component is known as a lightweight component as it is completely written in Java. It can do the high-level display work itself. It does not rely on any non java system classes or code provided by the operating system. The components have their specific view supported by Java’s look and feel classes.

2. Pluggable look and feel: Here Look means the appearance of GUI widgets and feel denotes the way the widgets perform. The pluggable look and feel feature is a mechanism that permits the change of the look and feel of the GUI at runtime. Wherever the program runs this feature allows the components’ look and feel to remain the same. This offers flexibility in choosing the look and feel of the GUI of an application.

3. Highly customizable: The visual appearance does not depend upon the internal presentation. So, the swing controls can be easily customized.

4. Rich controls: Developed controls like Tree tabbedpane, slider, colorpicker, and table controls are provided.

Swing Classes Hierarchy 

The MVC Connection

The model view controller (MVC) is a prominent design pattern that is used in the web development world. This method is used to organize the code. Three logical components of MVC are the model, the view, and the controller. Every component handles certain development aspects of a robust structure for web applications. MVC states that a program or application must consist of three features.  The 'model' term signifies state information linked to the component including all the data and related logic, the terminology 'view' indicates the look of the component on the screen and it also handles user interaction, and the 'controller' term shows, how the component reacts to the user or, this is an interface between model and view components. 

Conclusion

Initially, a brief description is given related to Java and Swing. Then a clear idea about the difference between AWT and swing is presented. Next Java Foundation Class is defined. After that, a figure is presented which describes the hierarchy of Java swing documentation. Some commonly used methods of components are explained which include public void add(Component c), public void setSize(int width,int height), public void setLayout(LayoutManager m), and public void setVisible(boolean b). Some basic programs related to Java Swing intellij are provided to give a clear idea about Java Swing. 

FAQS

1. What is Java?

Java is a prominent general-purpose programming language that is used in developing mobile and desktop applications, web applications, big data processing, server-side technologies, cloud computing, etc.

2. What is Java Swing?

Java Swing is a lightweight and platform-independent class that belongs to the Java foundation class. It is used in developing the graphical user interface for Java class. It includes some improved features to enhance the look and functionality of GUI.

3. What is AWT?

The Abstract Window Toolkit (AWT) is an application programming interface that supports a graphical user interface (GUI) or window-based applications in Java.

4. What is the difference between Java and Swing?

Swing belongs to the Java foundation class library and it is also an extension of AWT. Swing is used to develop Java-based front-end GUI applications. Swing supports much more improved functionality than AWT. It supports new components, expanded component features, and exceptional event-handling features.

5. What Is JFC?

JFC stands for Java Foundation Classes. JFC supports a wide range of GUI components and services. This makes the development and deployment of desktop and internet applications easier.

6. What are the features of the swing class?

The features of the swing class include lightweight components, a pluggable look and feel, highly customizable, rich control.

7. What is MVC?

The model view controller (MVC) is a prominent design pattern that is used in the web development world. This method is used to organize the code.

8. What is javax.swing?

javax.swing contains a set of lightweight components such as JButtons, JtextField etc.

9. What is JButton?

The class JButton creates a labeled button with platform-independent implementation. It is used for implementation of push buttons.

10. What is JFrame?

It provides a frame in the specific  graphics configuration of the screen.

Leave a Reply

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