SlideShare a Scribd company logo
1 of 34
Download to read offline
CHAPTER SEVEN
JAVA
GRAPHICAL USER INTERFACE
GUI
Introduction to GUI
A graphical user interface (GUI) presents a user-friendly
mechanism for interacting with an application.
A GUI (pronounced "goo-ee") gives an application a distinctive
"look" and "feel."
It provides different applications with consistent, intuitive user
interface components allows users to be somewhat familiar with an
application, so that they can learn it more quickly and use it
more productively.
Overview Of Swing Components
Most Swing components are pure Java components they are
written, manipulated and displayed completely in Java.
They are part of the Java Foundation Classes (JFC)Java's
libraries for cross-platform GUI development. There are actually two
sets of GUI components in Java. Before Swing was introduced in J2SE
1.2, Java GUIs were built with components from the Abstract
Window Toolkit (AWT) in package java.awt.
When a Java application with an AWT GUI executes on
different Java platforms, the application's GUI components display
differently on each platform. i.e. GUI app in Windows differs from
Mac.
Together, the appearance and the way in which the user interacts with
the application are known as that application's look-and-feel.
Swing GUI components allow you to specify a uniform look-
and-feel for your application across all platforms or to use
each platform's custom look-and-feel.
Overview Of Swing Components
Overview Of Swing Components
An application can even change the look-and-feel during execution
to enable users to choose their own preferred look-and-feel.
Most Swing components are not tied to actual GUI components
supported by the underlying platform on which an application
executes.
Such GUI components are known as lightweight components. For
this reason, Swing GUI components are generally preferred.
Overview Of Swing Components
In Swing, classes that represent GUI components have names
beginning with the letter J.
Some examples are JButton, JLabel, JFrame, JTextField, JCheckBox,
JComboBox, JRadioButton, JMenu, Jslider and so on.
Altogether there are more than 250 new classes and 75 interfaces in
Swing, the figure shown below depicts the Java Swing class hierarchy.
Overview Of Swing Components
Overview Of Swing Components
Overview Of Swing Components
Overview Of Swing Components
1. JPanel is swing's version of the AWT class panel and uses the same
default layout, Flowlayout and it is descended directly from JComponent.
2. JFrame is swing's version of frame and is descended directly from that
class. The components added to the frame are referred to as its contents;
these are managed by the contentPane. To add a component to a
JFrame, we must use its contentPane instead.
3. FlowLayout are used to arrange swing components from left to right
until there's no more space available. Then it begins a new row below it
and moves from left to right again. Each component in a FlowLayout gets
as much space as it needs and no more.
Overview Of Swing Components
4. GridLayout is a layout manager that lays out a container's components in
a rectangular grid in row by column manner as per provided by the user.
The container is divided into equal-sized rectangles, and one component
is placed in each rectangle.
5. JLabel descended from JComponent, is used to create text labels that
can not be edited live but are capable of modified in order.
6. AbstractButton is an abstract class which extends class JComponent and
provides a foundation for a family of button classes, including JButton.
JButton is a component the user clicks to trigger a specific action.
Overview Of Swing Components
7. JTextField allows editing of a single line of text. New features include
the ability to justify the text left, right, or center, and to set the text's
font.
8. JPasswordField (a direct subclass of JTextField ) you can suppress the
display of input. Each character entered can be replaced by echo
character. This allows confidential input for passwords, for example. By
default, the asterisk, *.
9. JTextArea allows editing of multiple lines of text. JTextArea can
be used in conjunction with class JScrollPane to achieve scrolling.
Overview Of Swing Components
The underlying JScrollPane can be forced to always or never have
either the vertical or horizontal scrollbar;
10.JRadioButton is similar to JCheckbox, except for the default
icon for each class. A set of radio buttons can be associated
as a group in which only one button at a time can be selected.
11.JCheckBox is not a member of a checkbox group. A checkbox can
be selected and deselected, AND it also displays its current state.
Overview Of Swing Components
12. JComboBox is like a drop down box. You can click a drop-down
arrow and select an option from a list. For example, when the
component has focus, pressing a key that corresponds to the first
character in some entry’s name selects that entry. A vertical scrollbar is
used for longer lists.
13. JMenubar can contain several JMenu’s. Each of the JMenu’s can
contain a series of JMenuItem ’s that it can let you select one. Swing
provides support for pull-down and popup menus.
Overview Of Swing Components
Java Swing Example:
Below is a java swing code for the traditional Hello World program.
import javax.swing.JFrame;
import javax.swing.JLabel; OUTPUT:
public class HelloWorldFrame extends JFrame {
HelloWorldFrame(){
JLabel jlbHelloWorld = new JLabel("Hello World");
add(jlbHelloWorld);
this.setSize(200, 100);
setVisible(true);
public static void main(String args[]) {
new HelloWorldFrame();
} } }
JFrame
JFrame is a Window with border, title and buttons. When JFrame is set
visible, an event dispatching thread is started.
JFrame objects store several objects including a Container object
known as the content pane. To add a component to a JFrame, add it to
the content pane.
JFrame Constructors
JFrame() : Constructs a new frame that is initially invisible.
JFrame(String title) : Creates a new, initially invisible Frame with the
specified title.
JFrame Contnd…
Steps to create a JFrame windows are as follows:
Step 1: Construct an object of the JFrame class.
Step 2: Set the size of the JFrame.
Step 3: Set the title of the JFrame to appear in the title bar (title bar will be
blank if no title is set).
Step 4: Set the default close operation. When the user clicks the close
button, the program stops running.
Step 5: Make the JFrame visible.
JFlowLayout
It is an AWT component used to arrange
swing components from left to right until
there’s no more space available.
import java.awt.FlowLayout.*;
import javax.swing.*;
public class FlowLayoutDemo extends JFrame{
FlowLayoutDemo() {
super("FlowLayout Source Demo");
JPanel panel = new JPanel();
add(panel);
panel.setLayout(new FlowLayout());
panel.add(new JLabel("JLabel 1"));
panel.add(new JButton("JButton 2"));
panel.add(new JCheckBox("JCheckBox 3"));
panel.add(new JTextField("Long-Named JTextField
4"));
panel.add(new JButton("JButton 5"));
setVisible(true);
setSize(300,350); }
public static void main(String[] args) {
FlowLayoutDemo flowLayout = new FlowLayoutDemo();
flowLayout.setDefaultCloseOperation(JFrame.EXIT_ON_
CLOSE);
}
}
JFlowLayout Contnd…
OUTPUT:
GridLayout
It is a layout manager that lays out a container’s components in a
rectangular grid. The container is divided into equal-sized rectangles,
and one component is placed in each rectangle.
GridLayout Constructors
GridLayout() : Creates a grid layout with a default of one column per component,
in a single row.
GridLayout(int rows, int cols) : Creates a grid layout with the specified number of
rows and columns.
JFrame Contnd…
Steps to create a JFrame windows with predefined layout are as follows:
Step 1: Construct an object of the JFrame class.
Step 2: Set the size of the JFrame.
Step 3: Set the title of the JFrame to appear in the title bar (title bar will be
blank if no title is set).
Step 4: Set up the content pane and components in a layout FlowLayout,
GridLayout etc.
Step 5: Set the default close operation.
Step 6: Make the JFrame visible.
JLabel
JLabel, descended from JComponent, is used to create text labels.
A JLabel object provides text instructions or information on a GUI;
display a single line of read-only text, an image or both text and image.
We use a Swing JLabel when we need a user interface component
that displays a message or an image.
JLabel Constructor
JLabel() : Creates a JLabel instance with no image and with an empty string for the title.
JLabel(Icon image) :Creates a JLabel instance with the specified image.
JLabel(String text) : Creates a JLabel instance with the specified text.
Introduction to Event Handling
Normally, a user interacts with an application’s GUI to indicate the tasks that the
application should perform.
For example, when you write an e-mail in an e-mail application, clicking the send button
tells the application to send the e-mail to the specified e-mail addresses.
GUIs are event driven. When the user interacts with a GUI component, the interaction
known as an event drives the program to perform a task.
Some common events (user interactions) that might cause an application to perform a task
include clicking a button, typing in a text field, selecting an item from a menu, closing a
window and moving the mouse.
 The code that performs a task in response to an event is called an event handler and the
overall process of responding to events is known as event handling.
More on Event Handling
Both AWT and Swing applications uses the AWT event-handling classes (in package java.awt.event).
Swing added a few new event handling classes (in package javax.swing.event), but they are not
frequently used.
AWT GUI Components (such as Button, TextField, ComboBox, CheckBox and Window) can trigger an
AWTEvent upon user’s activation.
More on Event Handling
ActionEvent & ActionListener
An ActionEvent is fired, when an action has been performed by the user.
For examples, when the user clicks a button, chooses a menu item, presses enter key in a text field.
The associated ActionListener interface declares only one abstract method, as follows:
ActionListner Method Definition:
public interface ActionListener extends java.util.eventlistener {
public void actionPerformed(ActionEvent evt); // called-back when an action
//has been performed
}
More on Event Handling
Example source code:
The example illustrates a source code fragment which clears a text in text field when the clear button pressed.
JPanel panel = new JPanel();
JTextfield1 jTextField1 = new JTextField();
JButton jBtnClear new JButton(“CLEAR”)
panel.setLayout(new GridLayout(1,2));
add(panel, BorderLayout.CENTER);
panel.add(jBtnClear);
panel.add(jTextField1);
jBtnClear.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
jTextfield1.setText("");
}});
JTextField
It allows editing/displaying of a single line of text.
When the user types data into them and presses the Enter key, an action
event occurs.
If the program registers an event listener, the listener processes the event
and can use the data in the text field at the time of the event in the program.
JTextField is an input area where the user can type in characters.
JTextField Contnd…
If you want to let the user enter multiple lines of text, you cannot use
JTextfield’s unless you create several of them.
The solution is to use JTextArea which enables the user to enter multiple lines
of text.
JTextField Constructor
JTextField() : Constructs a new TextField.
JTextField(String text): Constructs a new TextField initialized with the specified
text.
All search engine applications are of typical examples of JTextFields.
JButton
JButton is a component that the user clicks to trigger a specific action.
A Java application can use several types of buttons, including command
buttons, checkboxes, toggle buttons and radio buttons.
JButton Constructors
JButton() : Creates a button with no set text or icon.
JButton(Action a) : Creates a button where properties are taken from the
Action supplied.
JButton(String text) : Creates a button with text.
JComboBox
JComboBox is like a drop down box that you can click a drop-down arrow and select
an option from a list.
It generates ItemEvent. For example, when the component has focus, pressing
a key that corresponds to the first character in some entry’s name selects that
entry. A vertical scrollbar is used for longer lists.
JComboBox Constructors
JComboBox() : Creates a JComboBox with a default data model.
JComboBox(Object[] items) :Creates a JComboBox that contains the elements in the
specified array.
JComboBox(Vector items) : Creates a JComboBox that contains the elements in the
specified Vector.
JMenus
Swing provides support for pull-down and popup menus.
A JMenubar can contain several JMenus. And each of the JMenus can contain a series
of JMenuItems that you can select.
How Menus are created?
1. First, a JMenubar is created
2. Then, we attach all of the menus to this JMenubar.
3. Then we add JMenuItems to the JMenus.
4. The JMenubar is then added to the frame. By default, each JMenuItem added to a JMenu is
enabled, that is, it can be selected.
JMenus
import java.awt.*;
import java.awt.event.*;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JFrame;
public class JMenuDemo extends JFrame implements
ActionListener{
JTextArea jtAreaOutput;
JMenuBar mainMenuBar;
JMenu menu1, menu2, submenu;
JMenuItem New,subMenuItem;
JMenuDemo() {
super("Microsoft Word");
mainMenuBar = new JMenuBar();
menu1 = new JMenu("File");
menu1.setMnemonic(KeyEvent.VK_F);
mainMenuBar.add(menu1);
New = new JMenuItem("New");
New.addActionListener(this);
menu1.add(New);
menu1.addSeparator();
JMenus
// Sub Menu follows a seperator
submenu = new JMenu("Send To:");
submenu.setMnemonic(KeyEvent.VK_D);
subMenuItem = new JMenuItem("My Document");
subMenuItem.addActionListener(this);
submenu.add(subMenuItem);
menu1.add(submenu);
// Build second menu in the menu bar.
menu2 = new JMenu("Edit");
menu2.setMnemonic(KeyEvent.VK_E);
mainMenuBar.add(menu2);
// return mainMenuBar;
JPanel jplContentPane = new JPanel();
jplContentPane.setLayout(new FlowLayout());
jtAreaOutput = new JTextArea(5, 30);
jplContentPane.add(jtAreaOutput);
add(jplContentPane);
setSize(400, 300);
setVisible(true);
}
JMenus
public void actionPerformed(ActionEvent e) {
JMenuItem source = (JMenuItem) (e.getSource());
String s = "Menu Item source: " + source.getText();
jtAreaOutput.setText(s);
}
public static void main(String[] args) {
JMenuDemo frame= new JMenuDemo();
frame.setJMenuBar(frame.mainMenuBar);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_
CLOSE);
} }
The End

More Related Content

What's hot (20)

Java Swing JFC
Java Swing JFCJava Swing JFC
Java Swing JFC
 
Swing
SwingSwing
Swing
 
Gui
GuiGui
Gui
 
Graphical User Interface (Gui)
Graphical User Interface (Gui)Graphical User Interface (Gui)
Graphical User Interface (Gui)
 
Swings
SwingsSwings
Swings
 
Complete java swing
Complete java swingComplete java swing
Complete java swing
 
Jdbc
JdbcJdbc
Jdbc
 
Swing and Graphical User Interface in Java
Swing and Graphical User Interface in JavaSwing and Graphical User Interface in Java
Swing and Graphical User Interface in Java
 
Java: GUI
Java: GUIJava: GUI
Java: GUI
 
Java swing and events
Java swing and eventsJava swing and events
Java swing and events
 
Java swing
Java swingJava swing
Java swing
 
GUI components in Java
GUI components in JavaGUI components in Java
GUI components in Java
 
GUI Programming in JAVA (Using Netbeans) - A Review
GUI Programming in JAVA (Using Netbeans) -  A ReviewGUI Programming in JAVA (Using Netbeans) -  A Review
GUI Programming in JAVA (Using Netbeans) - A Review
 
Swing and AWT in java
Swing and AWT in javaSwing and AWT in java
Swing and AWT in java
 
swingbasics
swingbasicsswingbasics
swingbasics
 
Awt and swing in java
Awt and swing in javaAwt and swing in java
Awt and swing in java
 
java2 swing
java2 swingjava2 swing
java2 swing
 
Java Swing
Java SwingJava Swing
Java Swing
 
Java Swing
Java SwingJava Swing
Java Swing
 
Java- GUI- Mazenet solution
Java- GUI- Mazenet solutionJava- GUI- Mazenet solution
Java- GUI- Mazenet solution
 

Similar to Z blue introduction to gui (39023299)

Swingpre 150616004959-lva1-app6892
Swingpre 150616004959-lva1-app6892Swingpre 150616004959-lva1-app6892
Swingpre 150616004959-lva1-app6892renuka gavli
 
SWING USING JAVA WITH VARIOUS COMPONENTS
SWING USING  JAVA WITH VARIOUS COMPONENTSSWING USING  JAVA WITH VARIOUS COMPONENTS
SWING USING JAVA WITH VARIOUS COMPONENTSbharathiv53
 
Awt, Swing, Layout managers
Awt, Swing, Layout managersAwt, Swing, Layout managers
Awt, Swing, Layout managersswapnac12
 
Ajp notes-chapter-02
Ajp notes-chapter-02Ajp notes-chapter-02
Ajp notes-chapter-02Ankit Dubey
 
Advance Java Programming (CM5I) 2.Swing
Advance Java Programming (CM5I) 2.SwingAdvance Java Programming (CM5I) 2.Swing
Advance Java Programming (CM5I) 2.SwingPayal Dungarwal
 
Java AWT and Java FX
Java AWT and Java FXJava AWT and Java FX
Java AWT and Java FXpratikkadam78
 
java presentation on Swings chapter java presentation on Swings
java presentation on Swings chapter java presentation on Swingsjava presentation on Swings chapter java presentation on Swings
java presentation on Swings chapter java presentation on SwingsMohanYedatkar
 
Computer Programming NC III - Java Swing.pptx
Computer Programming NC III - Java Swing.pptxComputer Programming NC III - Java Swing.pptx
Computer Programming NC III - Java Swing.pptxjonathancapitulo2
 
Chapter iv(modern gui)
Chapter iv(modern gui)Chapter iv(modern gui)
Chapter iv(modern gui)Chhom Karath
 
GUI Programming In Java
GUI Programming In JavaGUI Programming In Java
GUI Programming In Javayht4ever
 
DSJ_Unit III.pdf
DSJ_Unit III.pdfDSJ_Unit III.pdf
DSJ_Unit III.pdfArumugam90
 

Similar to Z blue introduction to gui (39023299) (20)

swings.pptx
swings.pptxswings.pptx
swings.pptx
 
Swingpre 150616004959-lva1-app6892
Swingpre 150616004959-lva1-app6892Swingpre 150616004959-lva1-app6892
Swingpre 150616004959-lva1-app6892
 
SWING USING JAVA WITH VARIOUS COMPONENTS
SWING USING  JAVA WITH VARIOUS COMPONENTSSWING USING  JAVA WITH VARIOUS COMPONENTS
SWING USING JAVA WITH VARIOUS COMPONENTS
 
Awt, Swing, Layout managers
Awt, Swing, Layout managersAwt, Swing, Layout managers
Awt, Swing, Layout managers
 
Swing
SwingSwing
Swing
 
Swing
SwingSwing
Swing
 
Chap1 1 1
Chap1 1 1Chap1 1 1
Chap1 1 1
 
Chap1 1.1
Chap1 1.1Chap1 1.1
Chap1 1.1
 
Ajp notes-chapter-02
Ajp notes-chapter-02Ajp notes-chapter-02
Ajp notes-chapter-02
 
Advance Java Programming (CM5I) 2.Swing
Advance Java Programming (CM5I) 2.SwingAdvance Java Programming (CM5I) 2.Swing
Advance Java Programming (CM5I) 2.Swing
 
Java AWT and Java FX
Java AWT and Java FXJava AWT and Java FX
Java AWT and Java FX
 
SWING.pptx
SWING.pptxSWING.pptx
SWING.pptx
 
java presentation on Swings chapter java presentation on Swings
java presentation on Swings chapter java presentation on Swingsjava presentation on Swings chapter java presentation on Swings
java presentation on Swings chapter java presentation on Swings
 
Computer Programming NC III - Java Swing.pptx
Computer Programming NC III - Java Swing.pptxComputer Programming NC III - Java Swing.pptx
Computer Programming NC III - Java Swing.pptx
 
14a-gui.ppt
14a-gui.ppt14a-gui.ppt
14a-gui.ppt
 
SwingApplet.pptx
SwingApplet.pptxSwingApplet.pptx
SwingApplet.pptx
 
Chapter iv(modern gui)
Chapter iv(modern gui)Chapter iv(modern gui)
Chapter iv(modern gui)
 
GUI Programming In Java
GUI Programming In JavaGUI Programming In Java
GUI Programming In Java
 
DSJ_Unit III.pdf
DSJ_Unit III.pdfDSJ_Unit III.pdf
DSJ_Unit III.pdf
 
L11cs2110sp13
L11cs2110sp13L11cs2110sp13
L11cs2110sp13
 

More from Narayana Swamy

AICTE GUIDLINES AICTE GUIDLINESAICTE GUIDLINES
AICTE GUIDLINES  AICTE GUIDLINESAICTE GUIDLINESAICTE GUIDLINES  AICTE GUIDLINESAICTE GUIDLINES
AICTE GUIDLINES AICTE GUIDLINESAICTE GUIDLINESNarayana Swamy
 
Z blue interfaces and packages (37129912)
Z blue   interfaces and  packages (37129912)Z blue   interfaces and  packages (37129912)
Z blue interfaces and packages (37129912)Narayana Swamy
 
Oop inheritance chapter 3
Oop inheritance chapter 3Oop inheritance chapter 3
Oop inheritance chapter 3Narayana Swamy
 

More from Narayana Swamy (7)

AICTE GUIDLINES AICTE GUIDLINESAICTE GUIDLINES
AICTE GUIDLINES  AICTE GUIDLINESAICTE GUIDLINESAICTE GUIDLINES  AICTE GUIDLINESAICTE GUIDLINES
AICTE GUIDLINES AICTE GUIDLINESAICTE GUIDLINES
 
Files io
Files ioFiles io
Files io
 
Exceptions
ExceptionsExceptions
Exceptions
 
Z blue interfaces and packages (37129912)
Z blue   interfaces and  packages (37129912)Z blue   interfaces and  packages (37129912)
Z blue interfaces and packages (37129912)
 
Z blue exception
Z blue   exceptionZ blue   exception
Z blue exception
 
Oop inheritance chapter 3
Oop inheritance chapter 3Oop inheritance chapter 3
Oop inheritance chapter 3
 
Exceptions
ExceptionsExceptions
Exceptions
 

Recently uploaded

Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxpranjaldaimarysona
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)Suman Mia
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZTE
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
High Profile Call Girls Nashik Megha 7001305949 Independent Escort Service Na...
High Profile Call Girls Nashik Megha 7001305949 Independent Escort Service Na...High Profile Call Girls Nashik Megha 7001305949 Independent Escort Service Na...
High Profile Call Girls Nashik Megha 7001305949 Independent Escort Service Na...Call Girls in Nagpur High Profile
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxupamatechverse
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 

Recently uploaded (20)

Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
High Profile Call Girls Nashik Megha 7001305949 Independent Escort Service Na...
High Profile Call Girls Nashik Megha 7001305949 Independent Escort Service Na...High Profile Call Girls Nashik Megha 7001305949 Independent Escort Service Na...
High Profile Call Girls Nashik Megha 7001305949 Independent Escort Service Na...
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptx
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 

Z blue introduction to gui (39023299)

  • 2. Introduction to GUI A graphical user interface (GUI) presents a user-friendly mechanism for interacting with an application. A GUI (pronounced "goo-ee") gives an application a distinctive "look" and "feel." It provides different applications with consistent, intuitive user interface components allows users to be somewhat familiar with an application, so that they can learn it more quickly and use it more productively.
  • 3. Overview Of Swing Components Most Swing components are pure Java components they are written, manipulated and displayed completely in Java. They are part of the Java Foundation Classes (JFC)Java's libraries for cross-platform GUI development. There are actually two sets of GUI components in Java. Before Swing was introduced in J2SE 1.2, Java GUIs were built with components from the Abstract Window Toolkit (AWT) in package java.awt.
  • 4. When a Java application with an AWT GUI executes on different Java platforms, the application's GUI components display differently on each platform. i.e. GUI app in Windows differs from Mac. Together, the appearance and the way in which the user interacts with the application are known as that application's look-and-feel. Swing GUI components allow you to specify a uniform look- and-feel for your application across all platforms or to use each platform's custom look-and-feel. Overview Of Swing Components
  • 5. Overview Of Swing Components An application can even change the look-and-feel during execution to enable users to choose their own preferred look-and-feel. Most Swing components are not tied to actual GUI components supported by the underlying platform on which an application executes. Such GUI components are known as lightweight components. For this reason, Swing GUI components are generally preferred.
  • 6. Overview Of Swing Components In Swing, classes that represent GUI components have names beginning with the letter J. Some examples are JButton, JLabel, JFrame, JTextField, JCheckBox, JComboBox, JRadioButton, JMenu, Jslider and so on. Altogether there are more than 250 new classes and 75 interfaces in Swing, the figure shown below depicts the Java Swing class hierarchy.
  • 7. Overview Of Swing Components
  • 8. Overview Of Swing Components
  • 9. Overview Of Swing Components
  • 10. Overview Of Swing Components 1. JPanel is swing's version of the AWT class panel and uses the same default layout, Flowlayout and it is descended directly from JComponent. 2. JFrame is swing's version of frame and is descended directly from that class. The components added to the frame are referred to as its contents; these are managed by the contentPane. To add a component to a JFrame, we must use its contentPane instead. 3. FlowLayout are used to arrange swing components from left to right until there's no more space available. Then it begins a new row below it and moves from left to right again. Each component in a FlowLayout gets as much space as it needs and no more.
  • 11. Overview Of Swing Components 4. GridLayout is a layout manager that lays out a container's components in a rectangular grid in row by column manner as per provided by the user. The container is divided into equal-sized rectangles, and one component is placed in each rectangle. 5. JLabel descended from JComponent, is used to create text labels that can not be edited live but are capable of modified in order. 6. AbstractButton is an abstract class which extends class JComponent and provides a foundation for a family of button classes, including JButton. JButton is a component the user clicks to trigger a specific action.
  • 12. Overview Of Swing Components 7. JTextField allows editing of a single line of text. New features include the ability to justify the text left, right, or center, and to set the text's font. 8. JPasswordField (a direct subclass of JTextField ) you can suppress the display of input. Each character entered can be replaced by echo character. This allows confidential input for passwords, for example. By default, the asterisk, *. 9. JTextArea allows editing of multiple lines of text. JTextArea can be used in conjunction with class JScrollPane to achieve scrolling.
  • 13. Overview Of Swing Components The underlying JScrollPane can be forced to always or never have either the vertical or horizontal scrollbar; 10.JRadioButton is similar to JCheckbox, except for the default icon for each class. A set of radio buttons can be associated as a group in which only one button at a time can be selected. 11.JCheckBox is not a member of a checkbox group. A checkbox can be selected and deselected, AND it also displays its current state.
  • 14. Overview Of Swing Components 12. JComboBox is like a drop down box. You can click a drop-down arrow and select an option from a list. For example, when the component has focus, pressing a key that corresponds to the first character in some entry’s name selects that entry. A vertical scrollbar is used for longer lists. 13. JMenubar can contain several JMenu’s. Each of the JMenu’s can contain a series of JMenuItem ’s that it can let you select one. Swing provides support for pull-down and popup menus.
  • 15. Overview Of Swing Components Java Swing Example: Below is a java swing code for the traditional Hello World program. import javax.swing.JFrame; import javax.swing.JLabel; OUTPUT: public class HelloWorldFrame extends JFrame { HelloWorldFrame(){ JLabel jlbHelloWorld = new JLabel("Hello World"); add(jlbHelloWorld); this.setSize(200, 100); setVisible(true); public static void main(String args[]) { new HelloWorldFrame(); } } }
  • 16. JFrame JFrame is a Window with border, title and buttons. When JFrame is set visible, an event dispatching thread is started. JFrame objects store several objects including a Container object known as the content pane. To add a component to a JFrame, add it to the content pane. JFrame Constructors JFrame() : Constructs a new frame that is initially invisible. JFrame(String title) : Creates a new, initially invisible Frame with the specified title.
  • 17. JFrame Contnd… Steps to create a JFrame windows are as follows: Step 1: Construct an object of the JFrame class. Step 2: Set the size of the JFrame. Step 3: Set the title of the JFrame to appear in the title bar (title bar will be blank if no title is set). Step 4: Set the default close operation. When the user clicks the close button, the program stops running. Step 5: Make the JFrame visible.
  • 18. JFlowLayout It is an AWT component used to arrange swing components from left to right until there’s no more space available. import java.awt.FlowLayout.*; import javax.swing.*; public class FlowLayoutDemo extends JFrame{ FlowLayoutDemo() { super("FlowLayout Source Demo"); JPanel panel = new JPanel(); add(panel); panel.setLayout(new FlowLayout()); panel.add(new JLabel("JLabel 1")); panel.add(new JButton("JButton 2")); panel.add(new JCheckBox("JCheckBox 3")); panel.add(new JTextField("Long-Named JTextField 4")); panel.add(new JButton("JButton 5")); setVisible(true); setSize(300,350); } public static void main(String[] args) { FlowLayoutDemo flowLayout = new FlowLayoutDemo(); flowLayout.setDefaultCloseOperation(JFrame.EXIT_ON_ CLOSE); } }
  • 20. GridLayout It is a layout manager that lays out a container’s components in a rectangular grid. The container is divided into equal-sized rectangles, and one component is placed in each rectangle. GridLayout Constructors GridLayout() : Creates a grid layout with a default of one column per component, in a single row. GridLayout(int rows, int cols) : Creates a grid layout with the specified number of rows and columns.
  • 21. JFrame Contnd… Steps to create a JFrame windows with predefined layout are as follows: Step 1: Construct an object of the JFrame class. Step 2: Set the size of the JFrame. Step 3: Set the title of the JFrame to appear in the title bar (title bar will be blank if no title is set). Step 4: Set up the content pane and components in a layout FlowLayout, GridLayout etc. Step 5: Set the default close operation. Step 6: Make the JFrame visible.
  • 22. JLabel JLabel, descended from JComponent, is used to create text labels. A JLabel object provides text instructions or information on a GUI; display a single line of read-only text, an image or both text and image. We use a Swing JLabel when we need a user interface component that displays a message or an image. JLabel Constructor JLabel() : Creates a JLabel instance with no image and with an empty string for the title. JLabel(Icon image) :Creates a JLabel instance with the specified image. JLabel(String text) : Creates a JLabel instance with the specified text.
  • 23. Introduction to Event Handling Normally, a user interacts with an application’s GUI to indicate the tasks that the application should perform. For example, when you write an e-mail in an e-mail application, clicking the send button tells the application to send the e-mail to the specified e-mail addresses. GUIs are event driven. When the user interacts with a GUI component, the interaction known as an event drives the program to perform a task. Some common events (user interactions) that might cause an application to perform a task include clicking a button, typing in a text field, selecting an item from a menu, closing a window and moving the mouse.  The code that performs a task in response to an event is called an event handler and the overall process of responding to events is known as event handling.
  • 24. More on Event Handling Both AWT and Swing applications uses the AWT event-handling classes (in package java.awt.event). Swing added a few new event handling classes (in package javax.swing.event), but they are not frequently used. AWT GUI Components (such as Button, TextField, ComboBox, CheckBox and Window) can trigger an AWTEvent upon user’s activation.
  • 25. More on Event Handling ActionEvent & ActionListener An ActionEvent is fired, when an action has been performed by the user. For examples, when the user clicks a button, chooses a menu item, presses enter key in a text field. The associated ActionListener interface declares only one abstract method, as follows: ActionListner Method Definition: public interface ActionListener extends java.util.eventlistener { public void actionPerformed(ActionEvent evt); // called-back when an action //has been performed }
  • 26. More on Event Handling Example source code: The example illustrates a source code fragment which clears a text in text field when the clear button pressed. JPanel panel = new JPanel(); JTextfield1 jTextField1 = new JTextField(); JButton jBtnClear new JButton(“CLEAR”) panel.setLayout(new GridLayout(1,2)); add(panel, BorderLayout.CENTER); panel.add(jBtnClear); panel.add(jTextField1); jBtnClear.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { jTextfield1.setText(""); }});
  • 27. JTextField It allows editing/displaying of a single line of text. When the user types data into them and presses the Enter key, an action event occurs. If the program registers an event listener, the listener processes the event and can use the data in the text field at the time of the event in the program. JTextField is an input area where the user can type in characters.
  • 28. JTextField Contnd… If you want to let the user enter multiple lines of text, you cannot use JTextfield’s unless you create several of them. The solution is to use JTextArea which enables the user to enter multiple lines of text. JTextField Constructor JTextField() : Constructs a new TextField. JTextField(String text): Constructs a new TextField initialized with the specified text. All search engine applications are of typical examples of JTextFields.
  • 29. JButton JButton is a component that the user clicks to trigger a specific action. A Java application can use several types of buttons, including command buttons, checkboxes, toggle buttons and radio buttons. JButton Constructors JButton() : Creates a button with no set text or icon. JButton(Action a) : Creates a button where properties are taken from the Action supplied. JButton(String text) : Creates a button with text.
  • 30. JComboBox JComboBox is like a drop down box that you can click a drop-down arrow and select an option from a list. It generates ItemEvent. For example, when the component has focus, pressing a key that corresponds to the first character in some entry’s name selects that entry. A vertical scrollbar is used for longer lists. JComboBox Constructors JComboBox() : Creates a JComboBox with a default data model. JComboBox(Object[] items) :Creates a JComboBox that contains the elements in the specified array. JComboBox(Vector items) : Creates a JComboBox that contains the elements in the specified Vector.
  • 31. JMenus Swing provides support for pull-down and popup menus. A JMenubar can contain several JMenus. And each of the JMenus can contain a series of JMenuItems that you can select. How Menus are created? 1. First, a JMenubar is created 2. Then, we attach all of the menus to this JMenubar. 3. Then we add JMenuItems to the JMenus. 4. The JMenubar is then added to the frame. By default, each JMenuItem added to a JMenu is enabled, that is, it can be selected.
  • 32. JMenus import java.awt.*; import java.awt.event.*; import javax.swing.JMenu; import javax.swing.JMenuItem; import javax.swing.JMenuBar; import javax.swing.JPanel; import javax.swing.JTextArea; import javax.swing.JFrame; public class JMenuDemo extends JFrame implements ActionListener{ JTextArea jtAreaOutput; JMenuBar mainMenuBar; JMenu menu1, menu2, submenu; JMenuItem New,subMenuItem; JMenuDemo() { super("Microsoft Word"); mainMenuBar = new JMenuBar(); menu1 = new JMenu("File"); menu1.setMnemonic(KeyEvent.VK_F); mainMenuBar.add(menu1); New = new JMenuItem("New"); New.addActionListener(this); menu1.add(New); menu1.addSeparator();
  • 33. JMenus // Sub Menu follows a seperator submenu = new JMenu("Send To:"); submenu.setMnemonic(KeyEvent.VK_D); subMenuItem = new JMenuItem("My Document"); subMenuItem.addActionListener(this); submenu.add(subMenuItem); menu1.add(submenu); // Build second menu in the menu bar. menu2 = new JMenu("Edit"); menu2.setMnemonic(KeyEvent.VK_E); mainMenuBar.add(menu2); // return mainMenuBar; JPanel jplContentPane = new JPanel(); jplContentPane.setLayout(new FlowLayout()); jtAreaOutput = new JTextArea(5, 30); jplContentPane.add(jtAreaOutput); add(jplContentPane); setSize(400, 300); setVisible(true); }
  • 34. JMenus public void actionPerformed(ActionEvent e) { JMenuItem source = (JMenuItem) (e.getSource()); String s = "Menu Item source: " + source.getText(); jtAreaOutput.setText(s); } public static void main(String[] args) { JMenuDemo frame= new JMenuDemo(); frame.setJMenuBar(frame.mainMenuBar); frame.setDefaultCloseOperation(JFrame.EXIT_ON_ CLOSE); } } The End