[go: up one dir, main page]

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

Mod 555

Uploaded by

vabb22ise
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)
23 views39 pages

Mod 555

Uploaded by

vabb22ise
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

MODULE-3

SWING
Swing in Java

Java Swing is used to develop Graphical User Applications (GUI),


desktop-based applications. Swing is built in Java and built on top of the
AWT (abstract windowing toolkit)(AWT was also used to develop GUI).
Swing has benefits over AWT, including that:
•Its components are lightweight and more powerful (tables, lists, color
choosers, etc.).
•It's platform-independent vs. AWT's dependence.
•It supports a better look and feel that can plug into anything.
Components and Containers

A Swing GUI consists of two key items: components and containers.


However, this distinction is mostly conceptual because all containers
are also components. The difference between the two is found in their
intended purpose: As the term is commonly used, a component is an
independent visual control, such as a push button or slider.
A container holds a group of components. Thus, a container is a special
type of component that is designed to hold other components. Furthermore,
in order for a component to be displayed, it must be held within a container.
Thus, all Swing GUIs will have at least one container. Because containers
are components, a container can also hold other containers. This enables
Swing to define what is called a containment hierarchy, at the top of which
must be a top-level container.
What is a Container Class?
• Container classes are classes that can have other components
on it. So for creating a Java Swing GUI, we need at least one
container object. There are 3 types of Java Swing containers.
1. Panel: It is a pure container and is not a window in itself. The
sole purpose of a Panel is to organize the components on to a
window.
2. Frame: It is a fully functioning window with its title and icons.
3. Dialog: It can be thought of like a pop-up window that pops out
when a message has to be displayed. It is not a fully functioning
window like the Frame.
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:
It inherits Component and Container of AWT.
It cannot be contained within other containers.
Heavyweight.
Example: JFrame, JDialog, Japplet

Lightweight Containers:
It inherits JComponent class.
It is a general purpose container.
It can be used to organize related components together.
Example: JPanel
Components

In general, Swing components are derived from the JComponent class.


(The only exceptions to this are the four top-level containers,
described in the next section.) JComponent provides the functionality
that is common to all components. For example, JComponent supports
the pluggable look and feel. JComponent inherits the AWT classes
Container and Component.

Thus, a Swing component is built on and compatible with an AWT


component.

All of Swing’s components are represented by classes defined within the


package javax.swing.
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.
import javax.swing.*;
class LabelExample
{
public static void main(String args[])
{
JFrame f= new JFrame("Label Example");
JLabel l1,l2;
l1=new JLabel("First Label.");
l1.setBounds(50,50, 100,30);
l2=new JLabel("Second Label.");
l2.setBounds(50,100, 100,30);
f.add(l1); f.add(l2);
f.setSize(300,300);
f.setLayout(null);
f.setVisible(true);
}
}
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.

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 Javatpoint.");
t1.setBounds(50,100, 200,30);
t2=new JTextField("AWT Tutorial");
t2.setBounds(50,150, 200,30);
f.add(t1); f.add(t2);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
}
Java JButton
• The JButton class is used to create a labeled button that has platform independent implementation.

import javax.swing.*;
public class ButtonExample {
public static void main(String[] args) {
JFrame f=new JFrame("Button Example");
JButton b=new JButton("Click Here");
b.setBounds(50,100,95,30);
f.add(b);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
}
Java JToggleButton
• JToggleButton is used to create toggle button, it is two-states button to switch on or
off.

import java.awt.FlowLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JFrame;
import javax.swing.JToggleButton;

public class JToggleButtonExample extends JFrame implements ItemListener {


public static void main(String[] args) {
new JToggleButtonExample();
}
private JToggleButton button;
JToggleButtonExample() {
setTitle("JToggleButton with ItemListener Example");
setLayout(new FlowLayout());
setJToggleButton();
setAction();
setSize(200, 200);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void setJToggleButton() {
button = new JToggleButton("ON");
add(button);
}
private void setAction() {
button.addItemListener(this);
}
public void itemStateChanged(ItemEvent eve) {
if (button.isSelected())
button.setText("OFF");
else
button.setText("ON");
}
}
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.
import javax.swing.*;
public class CheckBoxExample
{
CheckBoxExample(){
JFrame f= new JFrame("CheckBox Example");
JCheckBox checkBox1 = new JCheckBox("C++");
checkBox1.setBounds(100,100, 50,50);
JCheckBox checkBox2 = new JCheckBox("Java", true);
checkBox2.setBounds(100,150, 50,50);
f.add(checkBox1);
f.add(checkBox2);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String args[])
{
new CheckBoxExample();
}}
Java JRadioButton
The JRadioButton class is used to create a radio button. It is used to
choose one option from multiple options. It is widely used in exam
systems or quiz.
It should be added in ButtonGroup to select one radio button only.
import javax.swing.*;
public class RadioButtonExample {
JFrame f;
RadioButtonExample(){
f=new JFrame();
JRadioButton r1=new JRadioButton("A) Male");
JRadioButton r2=new JRadioButton("B) Female");
r1.setBounds(75,50,100,30);
r2.setBounds(75,100,100,30);
ButtonGroup bg=new ButtonGroup();
bg.add(r1);bg.add(r2);
f.add(r1);f.add(r2);
f.setSize(300,300);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String[] args) {
new RadioButtonExample();
}
}

You might also like