0% found this document useful (0 votes)
6 views39 pages

Javaswings Part 1

Java Swing, introduced in 1997, is part of the Java Foundation Classes (JFC) for creating window-based applications with a platform-independent GUI. It offers features like pluggable look and feel, lightweight components, and follows the MVC architecture, allowing for rich customization and advanced controls. Swing components, such as JFrame and JPanel, provide a flexible framework for building user interfaces in Java applications.

Uploaded by

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

Javaswings Part 1

Java Swing, introduced in 1997, is part of the Java Foundation Classes (JFC) for creating window-based applications with a platform-independent GUI. It offers features like pluggable look and feel, lightweight components, and follows the MVC architecture, allowing for rich customization and advanced controls. Swing components, such as JFrame and JPanel, provide a flexible framework for building user interfaces in Java applications.

Uploaded by

shreyassupe346
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 39

SWINGS

• Java Swings was introduced in 1997 and is a part of


Java Foundation Classes (JFC) that is used to create
window-based applications.

• The Java Foundation Classes (JFC) are a set of GUI


components which simplify the development of
desktop applications.

• It is built on the top of AWT (Abstract Windowing


Toolkit) API and entirely written in java.
• Swing is a set of classes that provides more flexible
GUI Components

• AWT provided limited graphical interface

• Limitation of AWT is it translates its visual


components into platform specific equivalents i.e a
component might look ,or act differently on different
platforms.
• Java Swing provides platform-independent and
lightweight components.
Features of SWINGS
• Pluggable look and feel - This feature enables the user to
switch the look and feel of Swing components without
restarting an application.

• The Swing library supports components’ look and feels that


remain the same across all platforms wherever the program
runs.

• Uses MVC architecture.

• Lightweight Components - For a component to qualify as


lightweight, it must not depend on any non-Java [O/s based)
system classes
• Platform Independent - Java is a platform-independent
language and runs on any client machine, the GUI look and
feel, owned and delivered by a platform-specific O/S,
simply does not affect an application’s GUI constructed
using Swing components.
• Advanced features such as JTable, JTabbedPane,
JScollPane, etc.

• Highly customizable – Swing controls can be customized


in a very easy way as visual appearance is independent of
internal representation.

• Rich controls– Swing provides a rich set of advanced


controls like Tree TabbedPane, slider, colorpicker, and
table controls.
MVC Architecture
• Model View Controller or MVC as it is popularly
called, is a software design pattern for developing
web applications. A Model View Controller
pattern is made up of the following three parts:

• Model
• View
• Controller
MVC Architecture
The Model
The model is responsible for managing the data of the
application.
It responds to the request from the view and it also
responds to instructions from the controller to update
itself.

The View
A presentation of data in a particular format, triggered
by a controller's decision to present the data.
They are script based templating systems like JSP, ASP,
PHP.
The Controller

The controller is responsible for responding to user


input and perform interactions on the data model
objects.

The controller receives the input, it validates the


input and then performs the business operation
that modifies the state of the data model.

Software Code that controls the interactions


between the Model and View.
HIERARCHY OF JAVA SWING CLASSES
Components And containers
• A component is an independent visual control and Java
Swing Framework contains a large set of these
components which provide rich functionalities and
allow high level of customization.

• They all are derived from JComponent class.

• A container holds a group of components. It provides a


space where a component can be managed and
displayed.
Containers are of two types:

Top level Containers :


Example: JFrame, JDialog, JApplet

Lightweight Containers :
Example: JPanel
Top Level Containers: JFrame
• A Frame, in general, is a container that can contain other
components such as buttons, labels, text fields, etc.
• A Frame window can contain a title, a border, and also menus,
text fields, buttons, and other components.
• The Frame in Java Swing is defined in class javax.swing.Jframe.
• Usually used as a program’s main window
We can create a JFrame window object using 2 approaches:

1) By Extending The JFrame Class


import javax.swing.*;
class FrameInherited extends JFrame{ //inherit from JFrame class
FrameInherited(){
JButton b=new JButton("JFrame_Button");//create button object
b.setBounds(100,50,150, 40);

add(b);//add button on frame


setSize(300,200);
setLayout(null);
setVisible(true);
}
}
public class Main {
public static void main(String[] args) {
new FrameInherited(); //create an object of FrameInherited class
}
}
2) By Instantiating The JFrame Class
import javax.swing.*;
public class Main {

public static void main(String[] args) {


JFrame f=new JFrame("JFrameInstanceExample");//create a JFrame object

JButton b=new JButton("JFrameButton");//create instance of JButton


b.setBounds(100,50,150, 40);//dimensions of JButton object

f.add(b);//add button in JFrame

f.setSize(300,200);//set frame width = 300 and height = 200


f.setLayout(null);//no layout manager specified
f.setVisible(true);//make the frame visible
}
}
Top Level Containers: JDialog

javax.swing.JDialog:
• More simple and limited than frames

• Typically used for showing a short message on the screen

• Also has a border and a title bar

• Unlike JFrame, it doesn't have maximize and minimize


buttons.

17
• Use the static method of JoptionPane to show
standard dialog boxes:

JOptionPane.showMessageDialog(null, "4+2=6");
Lightweight Containers : JPanel

• JPanel, a part of the Java Swing package, is a


container that can store a group of components.

• The main task of JPanel is to organize


components, various layouts can be set in JPanel
which provide better organization of components,
however, it does not have a title bar.
Commonly used Methods
of Container class
Method Description

add a component on another


public void add(Component c)
component.

public void setSize(int width,int


sets size of the component.
height)

public void sets the layout manager for the


setLayout(LayoutManager m) component.

sets the visibility of the component.


public void setVisible(boolean b)
It is by default false.
General purpose containers
BASIC CONTROLS
JComponents
Java JLabel
• The object of JLabel class is a component for placing text in a
container.

• . It is used to display a single line of read only text.

• The text can be changed by an application but a user cannot


edit it directly.

• It inherits JComponent class.


Commonly used Constructors:
Constructor Description

JLabel() Creates a JLabel instance with no image


and with an empty string for the title.

JLabel(String s) Creates a JLabel instance with the


specified text.
JLabel(Icon i) Creates a JLabel instance with the
specified image.
JLabel(String s, Icon i, Creates a JLabel instance with the
int horizontalAlignment) specified text, image, and horizontal
alignment.
Commonly used Methods

Methods Description

String getText() It returns the text string that a label displays.

void setText(String text) It defines the single line of text this component will
display.
void It sets the alignment of the label's contents along the X
setHorizontalAlignment(int axis.
alignment)
Icon getIcon() It returns the graphic image that the label displays.

int It returns the alignment of the label's contents along the


getHorizontalAlignment() X axis.
Java JButton
JButton class in Java is used to create push buttons that
can be used to perform any ActionEvent whenever it is
clicked.
Commonly Used Constructors
Commonly used Methods of AbstractButton class

Methods Description

void setText(String s) It is used to set specified text on button

String getText() It is used to return the text of the button.

void setEnabled(boolean b) It is used to enable or disable the button.

void setIcon(Icon b) It is used to set the specified Icon on the


button.

Icon getIcon() It is used to get the Icon of the button.

void setMnemonic(int a) It is used to set the mnemonic on the button.

void It is used to add the action listener to this


addActionListener(ActionListener a) object.
//Java program to demonstrate JLabel and JButton using Action Event
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Action extends JFrame implements ActionListener
{
JButton b1=new JButton("OK");
JButton b2=new JButton("CANCEL");
JLabel l1=new JLabel();
Action()
{
setLayout(new FlowLayout());
add(l1);add(b1);add(b2);
b1.addActionListener(this);
b2.addActionListener(this);
setTitle("Action Event Demonstration");
setSize(500,600);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
String s=e.getActionCommand();
if(s.equals("OK"))
l1.setText("OK BUTTON CLICKED");
else
l1.setText("CANCEL BUTTON CLICKED");
}
public static void main(String[] args)
{
new Action();
}
}
Java JTextField

• The object of a JTextField class is a text component


that allows the editing of a single line text.
• It inherits JTextComponent class.
Commonly used Constructors:

Constructor Description

JTextField() Creates a new TextField

JTextField(String text) Creates a new TextField initialized with the


specified text.

JTextField(String text, Creates a new TextField initialized with the


int columns) specified text and columns.

JTextField(int Creates a new empty TextField with the


columns) specified number of columns.
Commonly used Methods

Methods Description

void It is used to add the specified action


addActionListener(ActionList listener to receive action events from
ener l) this textfield.
Action getAction() It returns the currently set Action for
this ActionEvent source, or null if no
Action is set.
void setFont(Font f) It is used to set the current font.
void It is used to remove the specified
removeActionListener(Action action listener so that it no longer
Listener l) receives action events from this
textfield.
//Java program to demonstrate JTextField

import javax.swing.*;

class TextFieldExample {

public static void main(String args[])

JFrame f= new JFrame("TextField Example");

JTextField t1,t2;

t1=new JTextField("Welcome to KLEBCA Hubli");

t1.setBounds(50,100, 200,30);

t2=new JTextField(“Subject");

t2.setBounds(50,150, 200,30);

f.add(t1); f.add(t2);

f.setSize(400,400);

f.setLayout(null);

f.setVisible(true);

}
Java JCheckBox
• The JCheckBox class is used to create a checkbox.

• It is used to turn an option on (true) or off (false).

• Clicking on a CheckBox changes its state from "on" to "off" or from


"off" to "on ".
• It inherits JToggleButton class.

• JCheckBox class declaration


Declaration for javax.swing.JCheckBox class.
public class JCheckBox extends JToggleButton implements
Accessible
Commonly used Constructors:

• JCheckBox(String str)

• JCheckBox(Icon i, boolean state)

• JCheckBox(Icon i)

• JCheckBox(String str,Icon i)

• JCheckBox(String str,Icon i,boolean state)


Methods

Methods Description

AccessibleContext It is used to get the AccessibleContext


getAccessibleContext associated with this JCheckBox.
()

protected String It returns a string


paramString() representation of this JCheckBox.
import javax.swing.*;
import java.awt.event.*;
public class jcheckbox
{
jcheckbox()
{
JFrame f= new JFrame("CheckBox Example");
final JLabel label = new JLabel();
label.setHorizontalAlignment(JLabel.CENTER);
label.setSize(400,100);

JCheckBox checkbox1 = new JCheckBox("Java");


checkbox1.setBounds(150,100, 50,50);

JCheckBox checkbox2 = new JCheckBox("Advance Java");


checkbox2.setBounds(150,150, 50,50);
f.add(checkbox1); f.add(checkbox2); f.add(label);
checkbox1.addItemListener(new ItemListener()
{
public void itemStateChanged(ItemEvent e)
{
label.setText("Java Checkbox: " +
e.getStateChange()==1?"checked":"unchecked"));
}
});
checkbox2.addItemListener(new ItemListener()
{
public void itemStateChanged(ItemEvent e)
{
label.setText("Advance Java Checkbox: " +
(e.getStateChange()==1?"checked":"unchecked"));
}
});

f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);

}
//add main method

You might also like