MODULE 3 - Java Swing Tutorial
MODULE 3 - Java Swing Tutorial
Method Description
import javax.swing.*;
public class FirstSwingExample {
public static void main(String[] args) {
JFrame f=new JFrame();//creating instance of JF
rame
JButton b=new JButton("click");//creating instan
ce of JButton
b.setBounds(130,100,100, 40);//x axis, y axis, wid
th, height
2. JDialogBox
{"102","Jai","780000"},
{"101","Sachin","700000"}};
String column[]={"ID","NAME","SALARY"};
scrollableTextArea.setHorizontalScrollB
arPolicy(JScrollPane.HORIZONTAL_SCROLLB
AR_ALWAYS);
scrollableTextArea.setVerticalScrollBarP
olicy(JScrollPane.VERTICAL_SCROLLBAR_AL
WAYS);
frame.getContentPane().add(scrollableT
extArea);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(n
ew Runnable() {
Java LayoutManagers
The LayoutManagers are used to arrange
components in a particular manner. The Java
LayoutManagers facilitates us to control the
positioning and size of the components in GUI
forms. LayoutManager is an interface that is
implemented by all the classes of layout
managers. There are the following classes that
represent the layout managers:
1. java.awt.BorderLayout
2. java.awt.FlowLayout
3. java.awt.GridLayout
4. java.awt.CardLayout
5. java.awt.GridBagLayout
6. javax.swing.BoxLayout
7. javax.swing.GroupLayout
8. javax.swing.ScrollPaneLayout
9. javax.swing.SpringLayout etc.
Java BorderLayout
The BorderLayout is used to arrange the
components in five regions: north, south, east,
west, and center. Each region (area) may
contain one component only. It is the default
layout of a frame or window. The BorderLayout
provides five constants for each region:
1. public static final int NORTH
2. public static final int SOUTH
3. public static final int EAST
4. public static final int WEST
5. public static final int CENTER
Constructors
BorderLayout(): creates a border layout but
with no gaps between the components.
BorderLayout(int hgap, int vgap): creates a
border layout with the given horizontal and
vertical gaps between the components.
import java.awt.*;
import javax.swing.*;
// creating buttons
JButton b1 = new JButton("NORTH");; // the
button will be labeled as NORTH
JButton b2 = new JButton("SOUTH");; // the b
utton will be labeled as SOUTH
JButton b3 = new JButton("EAST");; // the but
ton will be labeled as EAST
JButton b4 = new JButton("WEST");; // the bu
tton will be labeled as WEST
JButton b5 = new JButton("CENTER");; // the
button will be labeled as CENTER
f.setSize(300, 300);
f.setVisible(true);
}
public static void main(String[] args) {
new Border();
}
}
// import statement
import java.awt.*;
import javax.swing.*;
public class BorderLayoutExample
{
JFrame jframe;
// constructor
BorderLayoutExample()
{
// creating a Frame
jframe = new JFrame();
// create buttons
JButton btn1 = new JButton("NORTH");
JButton btn2 = new JButton("SOUTH");
JButton btn3 = new JButton("EAST");
JButton btn4 = new JButton("WEST");
JButton btn5 = new JButton("CENTER");
// creating an object of the BorderLayout cla
ss using
// the parameterized constructor where the h
orizontal gap is 20
// and vertical gap is 15. The gap will be evid
ent when buttons are placed
// in the frame
jframe.setLayout(new BorderLayout(20, 15));
jframe.add(btn1, BorderLayout.NORTH);
jframe.add(btn2, BorderLayout.SOUTH);
jframe.add(btn3, BorderLayout.EAST);
jframe.add(btn4, BorderLayout.WEST);
jframe.add(btn5, BorderLayout.CENTER);
jframe.setSize(300,300);
jframe.setVisible(true);
}
// main method
public static void main(String argvs[])
{
new BorderLayoutExample();
}
}
ava FlowLayout
The Java FlowLayout class is used to arrange
the components in a line, one after another (in
a flow). It is the default layout of the applet or
panel.
Fields of FlowLayout class
1. public static final int LEFT
2. public static final int RIGHT
3. public static final int CENTER
4. public static final int LEADING
5. public static final int TRAILING
Constructors of FlowLayout class
1. FlowLayout(): creates a flow layout with
centered alignment and a default 5 unit
horizontal and vertical gap.
2. FlowLayout(int align): creates a flow
layout with the given alignment and a
default 5 unit horizontal and vertical gap.
3. FlowLayout(int align, int hgap, int
vgap): creates a flow layout with the given
alignment and the given horizontal and
vertical gap.
Example of FlowLayout class: Using
FlowLayout() constructor
FileName: FlowLayoutExample.java
// import statements
import java.awt.*;
import javax.swing.*;
JFrame frameObj;
// constructor
FlowLayoutExample()
{
// creating a frame object
frameObj = new JFrame();
frameObj.setSize(300, 300);
frameObj.setVisible(true);
}
// main method
public static void main(String argvs[])
{
new FlowLayoutExample();
}
}
Example of FlowLayout class: Using
FlowLayout(int align, int hgap, int vgap)
constructor
FileName: FlowLayoutExample1.java
// import statement
import java.awt.*;
import javax.swing.*;
frameObj.setSize(300, 300);
frameObj.setVisible(true);
}
// main method
public static void main(String argvs[])
{
new FlowLayoutExample1();
}
}
Output:
Event and Listener (Java Event Handling)
Changing the state of an object is known as an
event. For example, click on button, dragging
mouse etc. The java.awt.event package provides
many event classes and Listener interfaces for
event handling.
Java Event classes and Listener interfaces
ActionEvent ActionListener
MouseWheelEvent MouseWheelListener
KeyEvent KeyListener
ItemEvent ItemListener
TextEvent TextListener
AdjustmentEvent AdjustmentListener
WindowEvent WindowListener
ComponentEvent ComponentListener
ContainerEvent ContainerListener
FocusEvent FocusListener
//create components
tf=new TextField();
tf.setBounds(60,50,170,20);
Button b=new Button("click me");
b.setBounds(100,120,80,30);
//register listener
b.addActionListener(this);//passing current insta
nce
b.addActionListener(new ActionListener(){
public void actionPerformed(){
tf.setText("hello");
}
});
add(b);add(tf);
setSize(300,300);
setLayout(null);
setVisible(true);
}
public static void main(String args[]){
new AEvent3();
}
}
Java Adapter Classes
Java adapter classes provide the default implementation of
listener interfaces. If you inherit the adapter class, you will
not be forced to provide the implementation of all the
methods of listener interfaces. So it saves code.
MouseAdapterExample.java
import java.awt.FlowLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JFrame;
import javax.swing.JToggleButton;
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.