Java Swing
Java Swing
• Swing is a framework that provides more powerful and
flexible GUI components.
• Java Swing tutorial is a part of Java Foundation Classes (JFC)
that is used to create window-based applications - 1997.
• It is built on the top of AWT (Abstract Window Toolkit) API and
entirely written in java. – Java 1.2
• Unlike AWT, Java Swing provides platform-independent and
lightweight components.
• The javax.swing package provides classes for java swing API
such as JButton, JTextField, JTextArea, JRadioButton,
JCheckbox, JMenu, JColorChooser etc.
Difference b/w Java AWT/Java Swing
Two key swing features
• Lightweight
– Entirely written in Java and don’t not map directly to platform-specific
– Lightweight components are more efficient and more flexible
– Each component will work in a consistent manner across all platforms
• Pluggable Look and Feel
– Each component is rendered by Java code rather than native peers
– The look and feel of a component is under the control of Swing
– Separate the look and feel of a component from the logic of the
component
MVC Connection
• The model corresponds
to state information
associated with the
component
• The view determines
how the component is
displayed on the screen
• The controller
determines how the
component reacts to
the user
Hierarchy of Java Swing class
Simple Java Swing
JOptionPane
• The JOptionPane class is used to provide standard dialog
boxes such as message dialog box, confirm dialog box and
input dialog box.
• These dialog boxes are used to display information or get
input from the user.
• The JOptionPane class inherits JComponent class.
• JButton
JButton b=new JButton("Click Here");
b.setBounds(50,100,95,30);
• JLabel
JLabel l1=new JLabel("First Label.");
l1.setBounds(50,50, 100,30);
• JTextField
JTextField t1=new JTextField("Welcome to Javatpoint.");
t1.setBounds(50,100, 200,30);
• JComboBox
String count[]={"India","Aus","U.S.A","England","Newzealand"};
JComboBox cb=new JComboBox(count);
cb.setBounds(50, 50,90,20);
• JRadioButton
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);
• JCheckBox
JCheckBox checkBox1 = new JCheckBox("C++");
checkBox1.setBounds(100,100, 50,50);
Layout
• A layout manager automatically arranges your controls within
a window by using some type of algorithm.
• Each Container object has a layout manager associated with
it.
• A layout manager is an instance of any class that implements
the LayoutManager interface.
• The layout manager is set by the setLayout( ) method.
• If no call to setLayout( ) is made, then the default layout
manager is used.
• Whenever a container is resized (or sized for the first time),
the layout manager is used to position each of the
components within it.
Containers - Layout
14
Events
Event handling
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
Event Handling with anonymous inner class - (modern
technique)
JButton b1 = new JButton("Click Me!");
b1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
//logic
}
});
Set Font and Foreground color
• import java.awt.Color;
• import java.awt.Font;
• l1.setFont(new
Font("Arial",Font.BOLD,24));
• l1.setForeground(Color.green);
Dispose the frame
• frame.dispose();
Swing Components
31
Swing Components
32
import javax.swing.*;
public class Swing_table
{
JFrame table_f;
JTable
Swing_table(){
table_f=new JFrame();
String table_column[]={"SID","SNAME"}; //Table header
String table_data[][]={ {"1001","Cherry"},
{"1002","Candy"},
{"1003","Coco" }};
JTable table_jt=new JTable(table_data,table_column);
table_jt.setBounds(30,40,200,300);
JScrollPane table_sp=new JScrollPane(table_jt);
table_f.add(table_sp);
table_f.setSize(300,400);
table_f.setVisible(true);
}
public static void main(String[] args)
{
new Swing_table();
}
}
JTabbedPane JButton b2=new JButton("I am in Third tab");
p3.add(b2);
import javax.swing.*;
import java.awt.*; JButton b1=new JButton("Click Me");
import java.awt.event.*; p2.add(b1);
public class JTabbed_Ex {
JFrame f; tp.add("main",p1);
tp.add("visit",p2);
JTabbed_Ex(){ tp.add("Last",p3);
f=new JFrame(); f.add(tp);
JTabbedPane tp=new JTabbedPane(); f.setSize(400,400);
tp.setBounds(50,50,200,200); f.setLayout(null);
f.setVisible(true);
JTextArea ta=new JTextArea(200,200); }
JPanel p1=new JPanel(); public static void main(String[] args) {
p1.add(ta); new JTabbed_Ex ();
JPanel p2=new JPanel(); }
JPanel p3=new JPanel(); }
BorderLayout
import javax.swing.*;
//for borderlayout int hgap, int vgap
import java.awt.*;
f.setLayout(new BorderLayout(10,20));
f.add(b1, BorderLayout.NORTH);
public class Layout_manager {
JFrame f;
f.add(b2, BorderLayout.SOUTH);
f.add(b3, BorderLayout.EAST);
Layout_manager(){
f.add(b4, BorderLayout.WEST);
f.add(b5, BorderLayout.CENTER);
f = new JFrame();
// creating buttons
f.setSize(300, 300);
JButton b1 = new JButton("NORTH");
f.setVisible(true);
JButton b2 = new JButton("SOUTH");
}
JButton b3 = new JButton("EAST");
public static void main(String[] args) {
JButton b4 = new JButton("WEST");
new Layout_manager();
JButton b5 = new JButton("CENTER");
}
}
import javax.swing.*;
import java.awt.*; GridLayout
import java.awt.event.*;
frameObj.add(btn1); frameObj.add(btn2);
public class Grid_Layout { frameObj.add(btn3);
frameObj.add(btn4); frameObj.add(btn5);
JFrame frameObj; frameObj.add(btn6);
frameObj.add(btn7); frameObj.add(btn8);
Grid_Layout(){ frameObj.add(btn9);
frameObj = new JFrame();
frameObj.setLayout(new GridLayout(3,3, 20, 25));
JButton btn1 = new JButton("1"); frameObj.setSize(300, 300);
JButton btn2 = new JButton("2"); frameObj.setVisible(true);
JButton btn3 = new JButton("3"); }
JButton btn4 = new JButton("4");
JButton btn5 = new JButton("5");
JButton btn6 = new JButton("6"); public static void main(String[] args) {
JButton btn7 = new JButton("7"); new Grid_Layout();
JButton btn8 = new JButton("8");
JButton btn9 = new JButton("9"); }
btn1.addActionListener(new ActionListener() { }
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(frameObj, "Hello dear students");
}
});
import javax.swing.*;
import java.awt.*;
CardLayout
import java.awt.event.*;
public class CardLayout_Simple extends JFrame implements ActionListener
{
CardLayout crd;
// JFrame f; public void actionPerformed(ActionEvent e)
JButton btn1, btn2, btn3; {
Container cPane;
crd.next(cPane);
CardLayout_Simple(){
}
cPane = getContentPane();
public static void main(String[] args) {
crd = new CardLayout(40,30); CardLayout_Simple crdl = new CardLayout_Simple();
cPane.setLayout(crd);
btn1 = new JButton("Apple");
btn2 = new JButton("Boy");
crdl.setSize(300, 300);
btn3 = new JButton("Cat");
crdl.setVisible(true);
btn1.addActionListener(this);
btn2.addActionListener(this); }
btn3.addActionListener(this);
}
cPane.add("a", btn1);
cPane.add("b", btn2);
cPane.add("c", btn3);
}
import javax.swing.*; ImageIcon
import java.awt.*;
import java.awt.event.*;
public class ImageIcon_Simple {
JLabel l1;
ImageIcon im;
JFrame f;
ImageIcon_Simple(){
f=new JFrame("Image Icon Example ");
im=new ImageIcon(getClass().getResource("ball.jpg"));
l1=new JLabel(im);
f.add(l1);
f.setSize(600,500);
f.setVisible(true);
}
public static void main(String[] args) {
new ImageIcon_Simple();