Title: Advanced GUIs and Applets
1- Advanced GUIs and Applets
2Inheritance Hierarchy of GUI Classes
3Applets
- Applet a Java program that is embedded within a
Web page and executed by a Web browser - Create an applet by extending the class JApplet
- class JApplet contained in package javax.swing
4Members of class JApplet
5Members of class JApplet
6Applets
- No main method
- Methods init, start, and paint guaranteed to be
invoked in sequence - To develop an applet
- Override any/all of the methods above
7Applet Methods
- init Method
- Initializes variables
- Gets data from user
- Places various GUI components
- paint Method
- Performs output
8Skeleton of a Java Applet
import java.awt.Graphics import
javax.swing.JApplet public class WelcomeApplet
extends JApplet
9Applet Displaying Welcome Message
//Welcome Applet import java.awt.Graphics impor
t javax.swing.JApplet public class
WelcomeApplet extends JApplet public
void paint(Graphics g)
super.paint(g)
//Line 1 g.drawString(Welcome to
Java Programming,
30, 30) //Line2
10HTML to Run Applet
11Differences Between Applets and GUI Applications
- Applets
- Derived from JApplet
- No main method
- Uses init method
- Displayed by HTML
- Sets title in HTML
- Size set in HTML
- Applet closes when HTML doc closes
- GUI applications
- class extends JFrame
- Invokes main method
- Uses constructors
- Uses method setVisible
- Uses setTitle method
- Uses method setSize
- Closes with Exit button
12Converting a GUI Application to an Applet
- Change JFrame to JApplet
- Change constructor to method init
- Remove method calls such as setVisible, setTitle,
setSize - Remove the method main
- If applicable, remove Exit button/all code
associated with it (e.g. action listener)
13Additional GUI Components
- JTextArea
- JCheckBox
- JRadioButton
- JComboBox
- JList
14JTextArea
- Can collect multiple lines of input from user
- Can display multiple lines of output
- Pressing Enter key separates lines of text
- Each line ends with newline character \n
- Derived from class JTextComponent
15Constructors and Methods of class JTextArea
16Methods Inherited by class JTextArea from Parent
class JTextComponent
17JCheckBox
- User selects from predefined values
- Example of a toggle button
- Clicking JCheckBox generates item event
- Use interface ItemListener and its abstract
method itemStateChanged to handle event
18Constructors and Methods of class JCheckBox
19Constructors and Methods of class JCheckBox
20Constructors and Methods of class JCheckBox
21JRadioButton
- Created same way as check boxes
- Placed in content pane of applet
- Forces user to select only one radion button at a
time - You create a button group to group radio buttons
- Generates an ItemEvent
- interface ItemListener and method
itemStateChanged used to handle events
22Constructors and Methods of class JRadioButton
23Constructors and Methods of class JRadioButton
24Constructors and Methods of class JRadioButton
25JComboBox
- Commonly known as a drop-down list
- Used to select an item from a list of
possibilities - Generates an ItemEvent
- Event monitored by ItemListener
- ItemListener invokes method itemStateChanged
26Constructors of class JComboBox
27Applet with JCheckBox, JComboBox, and JRadioButton
28Constructors of class JList
29Methods of class JList