top of page

Introduction - Java Swing

What is Java Swing?

Java Swing is a lightweight Java graphical user interface (GUI) widget toolkit that includes a rich set of widgets. It is part of the Java Foundation Classes (JFC) and includes several packages for developing rich desktop applications in Java.


What is JFC?


The Java Foundation Classes (JFC) are a set of GUI components which simplify the development of desktop applications.


Java Swing properties:

  • Improved, OS-independent GUI framework

  • Classes in javax.swing and sub-packages

  • Swing implements all components in software

  • Applications look & behave the same on all platforms

  • Applications may not look like native GUI apps (but you can change that by applying a LookAndFeel)

  • Part of the Java Foundation Classes (JFC). JFC also includes a 2D API and drag-and-drop API

Swing graphical user interface:


A graphical interface consists of components, containers, and layouts.

Swing Containers:


A container is a window that can be displayed on screen:

  • JFrame - title bar, menu, and content pane

  • JWindow - no title bar or menu.

  • JDialog - a dialog window, has title bar

  • JApplet - for Applets (run in web browser). Deprecated now.


JFrame is the top level container for most of the applications

  • It has a title bar, menu bar, and content pane

  • JFrame is a heavy weight component


Swing Component:


Swing components are the basic building blocks of an application. We know that Swing is a GUI widget toolkit for Java. Every application has some basic interactive interface for the user. For example, a button, check-box, radio-button, text-field, etc. These together form the components in Swing.They are also known as "widgets".


Some of the commonly used components are as follows:

  • Label

  • Textbox

  • Button

  • Slider

  • Checkbox

  • ComboBox

  • RadioButton

  • ProgressBar


Common properties of components:


These are some of the common properties that almost all the components share
  • setLocation( x, y )

  • setIcon( imageIcon )

  • setBackground( color )

  • setForeground( color )

  • setEnabled( boolean )

  • setVisible( boolean )

  • setToolTipText( string)

Layout in java swing :


Layout refers to the arrangement of components within the container. In another way, it could be said that layout is placing the components at a particular position within the container.

Layout Manager:


The layout manager automatically positions all the components within the container. Even if you do not use the layout manager, the components are still positioned by the default layout manager. It is possible to lay out the controls by hand, however, it becomes very difficult because of the following two reasons.

  • It is very tedious to handle a large number of controls within the container.

  • Usually, the width and height information of a component is not given when we need to arrange them.

Java provides various layout managers to position the controls. Properties like size, shape, and arrangement varies from one layout manager to the other.


Containers and Components:

  • A GUI has many components in containers.

  • Use add to put component in a container.

  • A container is also a component; so a container may contain other containers.


Simple Java Swing Example:


import javax.swing.*;  
public class FirstSwingExample {  
public static void main(String[] args) {  
JFrame f=new JFrame();
JButton b=new JButton("click");
b.setBounds(130,100,100, 40);
f.add(b);
f.setSize(400,500);
f.setLayout(null);
f.setVisible(true);
}  }  

Output:

How does CodersArts help you in Java coding?

CodersArts provide :

  • Java swing assignment Help

  • Help in Java GUI development Projects

  • Mentorship from Experts Live 1:1 session

  • Course and Project completions

  • CourseWork help



Comentarios


bottom of page