Unit 5 Swings
Unit 5 Swings
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.
In general, Swing components are derived from the JComponent class. 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.
Containers:
Glass pane: This is the first pane and is very close to the monitor’s screen. Any
components to be displayed in the foreground are attached to this glass pane. To
reach this glass pane we use getGlassPane() method of JFrame class, which return
Component class object.
Root Pane: This pane is below the glass pane. Any components to be displayed in
the background are displayed in this frame. To go to the root pane, we can use
getRootPane() method of JFrame class, which returns JRootPane object.
Layered pane: This pane is below the root pane. When we want to take several
components as a group, we attach them in the layered pane. We can reach this
pane by calling getLayeredPane() method of JFrame class which returns
JLayeredPane class object.
Conent pane: This is bottom most of all, Individual components are attached to
this pane. To reach this pane, we can call getContentPane() method of JFrame
class which returns Container class object.
We know frame represents a window with a title bar and borders. Frame becomes
the basis for creating the GUIs for an application because all the components go into
the frame. To create a fram, we have to create an object to JFrame class in swing as
setDefaultCloseOperation(constant)
import javax.swing.*;
class FrameDemo
jf.setSize(200,200);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE );
}
JApplet:
A Java applet is a special kind of Java program that a browser enabled with
Java technology can download from the internet and run. An applet is
typically embedded inside a web page and runs in the context of a
browser. An applet must be a subclass of the java.applet.Applet class. The
Applet class provides the standard interface between the applet and the
browser environment.
The methods of Component class are widely used in java swing that are given below.
Method Description
Constructor Description
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 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.
JLabel class declaration
public class JLabel extends JComponent implements SwingConstants, Accessible
Commonly used Constructors:
Constructor Description
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.
JTextField class declaration
Constructor Description
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 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
public class JCheckBox extends JToggleButton implements Accessible
Constructor Description
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.
JRadioButton class declaration
public class JRadioButton extends JToggleButton implements Accessible
Constructor Description
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();
}
}
Java JList
The object of JList class represents a list of text items. The list of text items can be set
up so that the user can choose either one item or multiple items. It inherits
JComponent class.
JList class declaration
Constructor Description
Constructor Description
All applets are subclasses of Applet. Thus, all applets must import java.applet.
Applets must also import java.awt. AWT stands for the Abstract Window Toolkit.
Since all applets run in a window, it is necessary to include support for that
window by importing java.awt package.
Applets are not executed by the console-based Java run-time interpreter. Rather,
they are executed by either a Web browser or an applet viewer.
Once an applet has been compiled, it is included in an HTML file using theAPPLET
tag. The applet will be executed by a Java-enabled web browser when it
encounters the APPLET tag within the HTML file.
To view and test an applet more conveniently, simply include a comment at the
head of your
Java source code file that contains the APPLET tag.
Here is an example of such a comment:
/*
<applet code="MyApplet" width=200 height=60>
</applet>
*/
This comment contains an APPLET tag that will run an applet called MyApplet
in a window that is 200 pixels wide and 60 pixels high. Since the
inclusion of an APPLET command makes testing applets easier, all of the
applets shown in this tutorial will contain the appropriate APPLET tag
embedded in a comment.
Applet Architecture: