[Go to site: main page, start]

0% found this document useful (0 votes)
252 views5 pages

Java Swing Tutorial Overview

Java Swing is a GUI widget toolkit for Java that is built on top of AWT. It provides platform-independent and lightweight components like buttons, text fields, menus and more. Swing components follow the MVC pattern and support pluggable look and feel. Common Swing containers include JFrame for windows, JPanel for components grouping, and JDialog for modal windows.

Uploaded by

gjkumar_mca
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
252 views5 pages

Java Swing Tutorial Overview

Java Swing is a GUI widget toolkit for Java that is built on top of AWT. It provides platform-independent and lightweight components like buttons, text fields, menus and more. Swing components follow the MVC pattern and support pluggable look and feel. Common Swing containers include JFrame for windows, JPanel for components grouping, and JDialog for modal windows.

Uploaded by

gjkumar_mca
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
  • Java Swing Tutorial
  • Commonly used Methods of Component Class
  • Containers
  • Dialog Boxes and Panels
  • Panel Methods

Java Swing Tutorial

Java is a part of Java Foundation Classes (JFC) that is used to create window-based
applications. It is built on the top of AWT (Abstract Windowing Toolkit) API and entirely
written in java.
Unlike AWT, Java Swing provides platform-independent and lightweight components.
The [Link] package provides classes for java swing API such as JButton, JTextField,
JTextArea, JRadioButton, JCheckbox, JMenu, JColorChooser etc.
Difference between AWT and Swing
There are many differences between java awt and swing that are given below.
No.

Java AWT

Java Swing

1)

AWT
components
dependent.

are platform-

2)

AWT components are heavyweight.

Swing components arelightweight.

3)

AWT doesn't support pluggable look


and feel.

Swing supports pluggable look and


feel.

4)

AWT
provides less
Swing.

Swing
provides more
powerful
components such as tables, lists,
scrollpanes, colorchooser, tabbedpane
etc.

5)

AWT doesn't follows MVC(Model View


Controller) where model represents data,
view
represents
presentation
and
controller acts as an interface between
model and view.

components than

Java swing components areplatformindependent.

Swing follows MVC.

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

Hierarchy of Java Swing classes


The hierarchy of java swing API is given below.

Commonly used Methods of Component class


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

Description

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
component.

public void setVisible(boolean b)

sets the visibility of the component. It is by


default false.

manager

for

the

Java Swing Examples


There are two ways to create a frame:
By creating the object of Frame class (association)
By extending Frame class (inheritance)
We can write the code of swing inside the main(), constructor or any other method.

Simple Java Swing Example


Let's see a simple swing example where we are creating one button and adding it on the
JFrame object inside the main() method.
File: [Link]
import [Link].*;
public class FirstSwingExample {
public static void main(String[] args) {
JFrame f=new JFrame();//creating instance of JFrame
JButton b=new JButton("click");//creating instance of JButton
[Link](130,100,100, 40);//x axis, y axis, width, height
[Link](b);//adding button in JFrame

[Link](400,500);//400 width and 500 height


[Link](null);//using no layout managers
[Link](true);//making the frame visible
}
}

Containers
Containers are integral part of SWING GUI components. A container provides a space
where a component can be located. A Container in AWT is a component itself and it
adds the capability to add component to itself. Following are noticable points to be
considered.
Sub classes of Container are called as Container. For example JPanel, JFrame and
JWindow.
Container can add only Component to itself.
A default layout is present in each container which can be overridden using
setLayout method.
SWING Containers:
Following is the list of commonly used containers while designed GUI using SWING.
Sr.
No.

Container & Description

Panel
JPanel is the simplest container. It provides space in which any
other component can be placed, including other panels.

Frame
A JFrame is a top-level window with a title and a border

Window
A JWindow object is a top-level window with no borders and no
menubar.

Top-level Containers are: JFRame, JWindow and JDialog


Light Weight Container: JPanel
JDialog
A Dialog window is an independent subwindow meant to carry temporary notice apart
from the main Swing Application Window. Most Dialogs present an error message or

warning to a user, but Dialogs can present images, directory trees, or just about
anything compatible with the main Swing Application that manages them.
For convenience, several Swing component classes can directly instantiate and
display dialogs. To create simple, standard dialogs, you use the JOptionPane class.
The ProgressMonitor class can put up a dialog that shows the progress of an operation.
Two other classes, JColorChooser and JFileChooser, also supply standard dialogs. To
bring up a print dialog, you can use the Printing API. To create a custom dialog, use
the JDialog class directly.
The code for simple dialogs can be minimal. For example, here is an informational
dialog:

Here is the code that creates and shows it:


[Link](frame,
green.");

"Eggs

are

not

supposed

to

be

JPanel:
The class JPanel is a generic lightweight container.
Class constructors
S.N.

Constructor & Description

JPanel()
Creates a new JPanel with a double buffer and a flow layout.

JPanel(boolean isDoubleBuffered)
Creates a new JPanel with FlowLayout and the specified buffering strategy.

JPanel(LayoutManager layout)
Create a new buffered JPanel with the specified layout manager.

JPanel(LayoutManager layout, boolean isDoubleBuffered)


Creates a new JPanel with the specified layout manager and buffering strategy.

Class methods
S.N.

Method & Description

AccessibleContext getAccessibleContext()
Gets the AccessibleContext associated with this JPanel.

PanelUI getUI()
Returns the look and feel (L&F) object that renders this component.

String getUIClassID()
Returns a string that specifies the name of the L&F class that renders this component.

protected String paramString()


Returns a string representation of this JPanel.

void setUI(PanelUI ui)


Sets the look and feel (L&F) object that renders this component.

void updateUI()
Resets the UI property with a value from the current look and feel.

Common questions

Powered by AI

Containers in Java Swing serve as the base for holding and organizing components within a GUI. Top-level containers like JFrame, JWindow, and JDialog are used to provide the main window features like borders and windows management, whereas lightweight containers like JPanel are used to group components within top-level containers . Each container type comes with its default layout, which can be customized using the setLayout method to arrange components as desired .

Layout managers in Java Swing control the placement and size of components within a container, influencing the spatial arrangement and adaptability to different window sizes and resolutions. By using various layout managers such as FlowLayout, BorderLayout, or GridLayout, developers can customize how the components appear and rearrange themselves dynamically when the container is resized . This leads to flexible GUIs that can accommodate different screen sizes without manual repositioning of elements .

Lightweight components in Java Swing are significant because they offer better flexibility and consistency in appearance across different platforms. They are designed to be independent of the native system GUI components, allowing them to have a uniform look and feel . In comparison, heavyweight components such as those in AWT are dependent on the native system. Lightweight components generally have better performance as they render faster due to lower overhead from not using native resources, and they are easier to integrate into multi-platform applications .

The importance of pluggable look and feel in Java Swing lies in its ability to provide a consistent user experience across different platforms and to allow customization of the appearance of applications. This is achieved by separating the look (appearance) and feel (behavior) of components from their core functionality, enabling developers to change the GUI's skin without altering the application's code . This feature enhances user interface design by allowing applications to adapt to various operating system aesthetics and user preferences seamlessly .

JFrame and JWindow are both top-level containers in Java Swing used for creating windows in applications. A JFrame is a standard window with a title bar, borders, and buttons for minimizing, maximizing, and closing. It is typically used as the main window of an application . On the other hand, JWindow is a simplified version of JFrame without a title bar or standard window decorations, making it suitable for pop-up alerts or auxiliary windows without the full window management features . Both support adding components, but JFrame is more suited for complex applications while JWindow is for basic, less intrusive windows .

Java Swing achieves platform-independence by providing lightweight components that do not rely on the underlying operating system's graphical user interface components. This is unlike AWT, which uses the native system's GUI elements, making Swing more versatile across different operating systems . Key advantages of Swing over AWT include a pluggable look and feel, a more extensive set of components, and adherence to the Model-View-Controller (MVC) architecture, which allows for a more organized and manageable GUI development .

Dialog boxes in Java Swing are used as independent subwindows to display information or capture user input temporarily. These are generally created using the JDialog class. However, for simplicity and standard dialogs, the JOptionPane class can be used. For example, an informational dialog can be displayed using JOptionPane.showMessageDialog method: 'JOptionPane.showMessageDialog(frame, "Eggs are not supposed to be green.");' This line brings up a simple dialog box with a message .

A JPanel in Java Swing is preferred in scenarios where you need to organize and group components within a top-level container such as JFrame. Its defining characteristics include being a lightweight, flexible container that can hold other components like buttons, labels, or even other panels, allowing for an organized layout of the UI elements . Panels are also beneficial when the application requires customizing layouts using different layout managers or needs logical grouping of components for better manageability and visual clarity .

The implementation of the Model-View-Controller (MVC) pattern in Java Swing contributes to an application's design by clearly separating concerns, allowing developers to manage the data flow and GUI elements independently. The model manages data and business logic, the view displays the data, and the controller acts as an interface between model and view, handling user inputs and updating the model accordingly . This separation increases modularity and scalability, making applications easier to maintain and extend .

The Component class in Java Swing acts as a base class for all Swing GUI components, facilitating manipulation and management of UI elements within an application. The methods such as add(Component c), setSize(int width, int height), setLayout(LayoutManager m), and setVisible(boolean b) enhance component management by allowing developers to configure the appearance, layout, and behavior of components dynamically . This flexibility enables complex UI designs with customized look-and-feel and responsiveness tailored to specific application needs .

Java Swing Tutorial
Java  is a part of Java Foundation Classes (JFC) that is used to create window-based
applications. It is
Commonly used Methods of Component class
The methods of Component class are widely used in java swing that are given below.
M
f.setSize(400,500);//400 width and 500 height  
f.setLayout(null);//using no layout managers  
f.setVisible(true);
warning  to  a  user, but  Dialogs  can  present  images,  directory  trees,  or  just  about
anything compatible with the ma
2
PanelUI getUI()
Returns the look and feel (L&F) object that renders this component.
3
String getUIClassID()
Returns a strin

You might also like