Layout Event
Layout Event
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"};
14
Events
Event handling
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
• 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();
table_jt.setBounds(30,40,200,300);
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);
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);
}
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();