0% found this document useful (0 votes)
37 views

Java Swing

Java Swing is a GUI widget toolkit for Java that is built on top of the AWT (Abstract Window Toolkit). It contains lightweight components like JButton and JLabel that are easier to program than the heavyweight components of AWT. Swing provides features for creating window components, laying out components, and handling events. Events in Swing are handled through interfaces like ActionListener and key and mouse events can be detected. Examples are provided showing how to create simple Swing applications using JFrames and adding basic components like labels and buttons.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

Java Swing

Java Swing is a GUI widget toolkit for Java that is built on top of the AWT (Abstract Window Toolkit). It contains lightweight components like JButton and JLabel that are easier to program than the heavyweight components of AWT. Swing provides features for creating window components, laying out components, and handling events. Events in Swing are handled through interfaces like ActionListener and key and mouse events can be detected. Examples are provided showing how to create simple Swing applications using JFrames and adding basic components like labels and buttons.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Java

Swing
Introduc)on
•  Java AWT (Abstract Window Toolkit) contains
numerous classes and methods for crea)ng and
managing windows
•  Java Swing (javax.swing) is built on the AWT
•  Swing Concepts
–  Container
•  Heavyweight: JFrame, JApplet etc.
•  Lightweight: JPanel
–  Component
•  JbuIon, JLabel etc.
2
A Simple Swing
•  Two ways to create a simple swing applica)on
•  By crea(ng an object of JFrame class
–  Example: SimpleFrame1.java
•  By extending the JFrame class
–  Example: SimpleFrame2.java

3
JFrame with Simple Components
•  Simple JFrame
–  with a JLabel
–  with a JBuIon
–  with Default Layout
–  Example: LabelFrame1.java
•  Simple JFrame
–  with a JLabel
–  with a JBuIon
–  with FlowLayout
–  Example: LabelFrame2.java
4
Event Handling
•  Events are generated when user do some ac)ons
with the components (buIon click)
•  Event handling are same for Swing and AWT
•  The interface which is generally used for event
handling - Ac4onListener
•  The class that implements the Ac)onListener
interface must implement the following method
public void ac4onPerformed (Ac4onEvent ae)

5
Event Handling
•  The event name is Ac4onEvent
–  getSource()
–  getAc)onCommand()
•  Components registered to handle event by
addAc4onListener (Ac4onListener al)
•  Example: EventFrame(1-3).java

6
Some Components
•  JLabel
–  Example: TestJLabelFrame.java
•  JTextField and JPasswordField
–  Example: TestJTextFieldFrame.java
•  JBu6on
–  Example: TestJBuHonFrame.java

7
Some Components
•  JCheckBox
–  Example: TestJCheckBoxFrame.java
•  JRadioBu6on
–  Example: TestJRadioBuHonFrame.java
•  JComboBox
–  Example: TestJComboBoxFrame.java

8
Keyboard Events
•  In Swing we can also detect key and mouse events
•  Interface for key event handling - KeyListener
•  The name of the func)ons are
–  keyTyped(KeyEvent ke)
–  keyPressed(KeyEvent ke)
–  keyReleased(KeyEvent ke)
•  The event name is KeyEvent
–  getKeyChar(), getKeyCode()
•  Example: TestKeyListener.java

9
Mouse Events
•  Interface for mouse event handling - MouseListener
•  The name of the func)ons are
–  mouseClicked(MouseEvent me)
–  mousePressed(MouseEvent me)
–  mouseReleased(MouseEvent me)
–  mouseEntered(MouseEvent me)
–  mouseExited(MouseEvent me)
•  The event name is MouseEvent
–  getX(), getY()
•  Example: TestMouseListener.java
10

You might also like