SlideShare une entreprise Scribd logo
1  sur  13
TEXT RELATED GUI
COMPONENTS
Chapter 11.4:
GUI Classes for Handling
Text
 The Swing GUI classes JLabel, JTextField, and
JTextArea deal with text.
 A JLabel object displays uneditable text (or
image).
 A JTextField object allows the user to enter a
single line of text.
 A JTextArea object allows the user to enter
multiple lines of text. It can also be used for
displaying multiple lines of uneditable text.
JLabel
 We use a JLabel object to display a label.
 A label can be a text or an image.
 When creating an image label, we pass ImageIcon object
instead of a string.
JLabel textLabel = new JLabel("Please enter your name");
contentPane.add(textLabel);
JLabel imgLabel = new JLabel(new ImageIcon("cat.gif"));
contentPane.add(imgLabel);
JTextArea
 We use a JTextField object to accept a single line
to text from a user. An action event is generated
when the user presses the ENTER key.
 The getText method of JTextField is used to
retrieve the text that the user entered.
JTextField input = new JTextField( );
contentPane.add(input);
JLabel
(with an image)
JLabel
(with a text)
JTextField
JTextArea
 We use a JTextArea object to display or allow the
user to enter multiple lines of text.
 The setText method assigns the text to a
JTextArea, replacing the current content.
 The append method appends the text to the
current text.
JTextArea textArea
= new JTextArea( );
. . .
textArea.setText("Hellon");
textArea.append("the lost ");
textArea.append("world");
Hello
the lost world
JTextArea
 TextArea containing six words.
Adding Scroll Bars to JTextArea
 By default a JTextArea does not have any scroll
bars. To add scroll bars, we place a JTextArea in a
JScrollPane object.
JTextArea textArea = new JTextArea();
. . .
JScrollPane scrollText = new JScrollPane(textArea);
. . .
contentPane.add(scrollText);
 A sample window when a JScrollPane is used.
Other Common GUI Components
 JCheckBox
 JRadioButton
 JComboBox
 JList
Menus
 The javax.swing package contains three menu-
related classes: JMenuBar, JMenu, and
JMenuItem.
 JMenuBar is a bar where the menus are placed.
There is one menu bar per frame.
 JMenu (such as File or Edit) is a group of menu
choices. JMenuBar may include many JMenu
objects.
 JMenuItem (such as Copy, Cut, or Paste) is an
individual menu choice in a JMenu object.
 Only the JMenuItem objects generate events.
Menu Components
Edit View Help
JMenuBar Edit View HelpFile
JMenu
JMenuItem
separator
Sequence for Creating
Menus
1. Create a JMenuBar object and attach it
to a frame.
2. Create a JMenu object.
3. Create JMenuItem objects and add
them to the JMenu object.
4. Attach the JMenu object to the
JMenuBar object.

Contenu connexe

Tendances

Inventory management
Inventory managementInventory management
Inventory management
Rajeev Sharan
 

Tendances (16)

Rxjava2 custom operator
Rxjava2 custom operatorRxjava2 custom operator
Rxjava2 custom operator
 
Swing
SwingSwing
Swing
 
Auto-GWT : Better GWT Programming with Xtend
Auto-GWT : Better GWT Programming with XtendAuto-GWT : Better GWT Programming with Xtend
Auto-GWT : Better GWT Programming with Xtend
 
mediator
mediatormediator
mediator
 
Vectors in Java
Vectors in JavaVectors in Java
Vectors in Java
 
Lecture20 vector
Lecture20 vectorLecture20 vector
Lecture20 vector
 
Excel to Use Rest API to communicate with a server
Excel to Use Rest API to communicate with a serverExcel to Use Rest API to communicate with a server
Excel to Use Rest API to communicate with a server
 
Gwt and Xtend
Gwt and XtendGwt and Xtend
Gwt and Xtend
 
Functional Core, Reactive Shell
Functional Core, Reactive ShellFunctional Core, Reactive Shell
Functional Core, Reactive Shell
 
Function Java Vector class
Function Java Vector classFunction Java Vector class
Function Java Vector class
 
Calculator code
Calculator codeCalculator code
Calculator code
 
Inventory management
Inventory managementInventory management
Inventory management
 
Using Affordance for Clearer Source Code
Using Affordance for Clearer Source CodeUsing Affordance for Clearer Source Code
Using Affordance for Clearer Source Code
 
Farhaan Ahmed, BCA 2nd Year
Farhaan Ahmed, BCA 2nd YearFarhaan Ahmed, BCA 2nd Year
Farhaan Ahmed, BCA 2nd Year
 
Java Wrapper Classes and I/O Mechanisms
Java Wrapper Classes and I/O MechanismsJava Wrapper Classes and I/O Mechanisms
Java Wrapper Classes and I/O Mechanisms
 
J Query Presentation of David
J Query Presentation of DavidJ Query Presentation of David
J Query Presentation of David
 

En vedette (8)

Jtextarea
JtextareaJtextarea
Jtextarea
 
Power editor basics
Power editor basicsPower editor basics
Power editor basics
 
011 more swings_adv
011 more swings_adv011 more swings_adv
011 more swings_adv
 
JDBC
JDBCJDBC
JDBC
 
java swing programming
java swing programming java swing programming
java swing programming
 
Java GUI PART II
Java GUI PART IIJava GUI PART II
Java GUI PART II
 
Java Swing
Java SwingJava Swing
Java Swing
 
java2 swing
java2 swingjava2 swing
java2 swing
 

Similaire à Chapter 11.4

PLEASE HELP ME !!IT IS Due Tonight ;(!i have to submit it before.pdf
PLEASE HELP ME !!IT IS Due Tonight ;(!i have to submit it before.pdfPLEASE HELP ME !!IT IS Due Tonight ;(!i have to submit it before.pdf
PLEASE HELP ME !!IT IS Due Tonight ;(!i have to submit it before.pdf
mohammedfootwear
 
Z blue introduction to gui (39023299)
Z blue   introduction to gui (39023299)Z blue   introduction to gui (39023299)
Z blue introduction to gui (39023299)
Narayana Swamy
 
in java(netbeans) create a javaFX GUI that can open and display the co.docx
in java(netbeans) create a javaFX GUI that can open and display the co.docxin java(netbeans) create a javaFX GUI that can open and display the co.docx
in java(netbeans) create a javaFX GUI that can open and display the co.docx
mckerliejonelle
 

Similaire à Chapter 11.4 (20)

DSJ_Unit III.pdf
DSJ_Unit III.pdfDSJ_Unit III.pdf
DSJ_Unit III.pdf
 
Java swing
Java swingJava swing
Java swing
 
PLEASE HELP ME !!IT IS Due Tonight ;(!i have to submit it before.pdf
PLEASE HELP ME !!IT IS Due Tonight ;(!i have to submit it before.pdfPLEASE HELP ME !!IT IS Due Tonight ;(!i have to submit it before.pdf
PLEASE HELP ME !!IT IS Due Tonight ;(!i have to submit it before.pdf
 
Java: GUI
Java: GUIJava: GUI
Java: GUI
 
Swings
SwingsSwings
Swings
 
Z blue introduction to gui (39023299)
Z blue   introduction to gui (39023299)Z blue   introduction to gui (39023299)
Z blue introduction to gui (39023299)
 
Swing basics
Swing basicsSwing basics
Swing basics
 
Advanced java programming
Advanced java programmingAdvanced java programming
Advanced java programming
 
Jp notes
Jp notesJp notes
Jp notes
 
Swing
SwingSwing
Swing
 
Swing
SwingSwing
Swing
 
Quiz app(j tabbed pane,jdialog,container,actionevent,jradiobutton,buttongroup...
Quiz app(j tabbed pane,jdialog,container,actionevent,jradiobutton,buttongroup...Quiz app(j tabbed pane,jdialog,container,actionevent,jradiobutton,buttongroup...
Quiz app(j tabbed pane,jdialog,container,actionevent,jradiobutton,buttongroup...
 
Java swing
Java swingJava swing
Java swing
 
JavaYDL17
JavaYDL17JavaYDL17
JavaYDL17
 
Advance Java Programming (CM5I) 2.Swing
Advance Java Programming (CM5I) 2.SwingAdvance Java Programming (CM5I) 2.Swing
Advance Java Programming (CM5I) 2.Swing
 
Swing jecrc
Swing jecrc Swing jecrc
Swing jecrc
 
Unit-2 swing and mvc architecture
Unit-2 swing and mvc architectureUnit-2 swing and mvc architecture
Unit-2 swing and mvc architecture
 
Awt components
Awt componentsAwt components
Awt components
 
AWT New-3.pptx
AWT New-3.pptxAWT New-3.pptx
AWT New-3.pptx
 
in java(netbeans) create a javaFX GUI that can open and display the co.docx
in java(netbeans) create a javaFX GUI that can open and display the co.docxin java(netbeans) create a javaFX GUI that can open and display the co.docx
in java(netbeans) create a javaFX GUI that can open and display the co.docx
 

Plus de sotlsoc

Chapter 2.0 new
Chapter 2.0 newChapter 2.0 new
Chapter 2.0 new
sotlsoc
 
Chapter 1.0 new
Chapter 1.0 newChapter 1.0 new
Chapter 1.0 new
sotlsoc
 
Chapter 3.0 new
Chapter 3.0 newChapter 3.0 new
Chapter 3.0 new
sotlsoc
 
Chapter 6.5 new
Chapter 6.5 newChapter 6.5 new
Chapter 6.5 new
sotlsoc
 
Chapter 11.1
Chapter 11.1Chapter 11.1
Chapter 11.1
sotlsoc
 
Chapter 10.3
Chapter 10.3Chapter 10.3
Chapter 10.3
sotlsoc
 
Chapter 11.3
Chapter 11.3Chapter 11.3
Chapter 11.3
sotlsoc
 
Chapter 11.5
Chapter 11.5Chapter 11.5
Chapter 11.5
sotlsoc
 
Chapter 11.2
Chapter 11.2Chapter 11.2
Chapter 11.2
sotlsoc
 
Chapter 11.0
Chapter 11.0Chapter 11.0
Chapter 11.0
sotlsoc
 
Chapter 10.2
Chapter 10.2Chapter 10.2
Chapter 10.2
sotlsoc
 
Chapter 10.1
Chapter 10.1Chapter 10.1
Chapter 10.1
sotlsoc
 
Chapter 8.0
Chapter 8.0Chapter 8.0
Chapter 8.0
sotlsoc
 
Chapter 10.0
Chapter 10.0Chapter 10.0
Chapter 10.0
sotlsoc
 
Chapter 9.3
Chapter 9.3Chapter 9.3
Chapter 9.3
sotlsoc
 
Chapter 9.4
Chapter 9.4Chapter 9.4
Chapter 9.4
sotlsoc
 
Chapter 9.1
Chapter 9.1Chapter 9.1
Chapter 9.1
sotlsoc
 
Chapter 8.1
Chapter 8.1Chapter 8.1
Chapter 8.1
sotlsoc
 
Chapter 9.0
Chapter 9.0Chapter 9.0
Chapter 9.0
sotlsoc
 
Chapter 9.2
Chapter 9.2Chapter 9.2
Chapter 9.2
sotlsoc
 

Plus de sotlsoc (20)

Chapter 2.0 new
Chapter 2.0 newChapter 2.0 new
Chapter 2.0 new
 
Chapter 1.0 new
Chapter 1.0 newChapter 1.0 new
Chapter 1.0 new
 
Chapter 3.0 new
Chapter 3.0 newChapter 3.0 new
Chapter 3.0 new
 
Chapter 6.5 new
Chapter 6.5 newChapter 6.5 new
Chapter 6.5 new
 
Chapter 11.1
Chapter 11.1Chapter 11.1
Chapter 11.1
 
Chapter 10.3
Chapter 10.3Chapter 10.3
Chapter 10.3
 
Chapter 11.3
Chapter 11.3Chapter 11.3
Chapter 11.3
 
Chapter 11.5
Chapter 11.5Chapter 11.5
Chapter 11.5
 
Chapter 11.2
Chapter 11.2Chapter 11.2
Chapter 11.2
 
Chapter 11.0
Chapter 11.0Chapter 11.0
Chapter 11.0
 
Chapter 10.2
Chapter 10.2Chapter 10.2
Chapter 10.2
 
Chapter 10.1
Chapter 10.1Chapter 10.1
Chapter 10.1
 
Chapter 8.0
Chapter 8.0Chapter 8.0
Chapter 8.0
 
Chapter 10.0
Chapter 10.0Chapter 10.0
Chapter 10.0
 
Chapter 9.3
Chapter 9.3Chapter 9.3
Chapter 9.3
 
Chapter 9.4
Chapter 9.4Chapter 9.4
Chapter 9.4
 
Chapter 9.1
Chapter 9.1Chapter 9.1
Chapter 9.1
 
Chapter 8.1
Chapter 8.1Chapter 8.1
Chapter 8.1
 
Chapter 9.0
Chapter 9.0Chapter 9.0
Chapter 9.0
 
Chapter 9.2
Chapter 9.2Chapter 9.2
Chapter 9.2
 

Dernier

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
KarakKing
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 

Dernier (20)

Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 

Chapter 11.4

  • 2. GUI Classes for Handling Text  The Swing GUI classes JLabel, JTextField, and JTextArea deal with text.  A JLabel object displays uneditable text (or image).  A JTextField object allows the user to enter a single line of text.  A JTextArea object allows the user to enter multiple lines of text. It can also be used for displaying multiple lines of uneditable text.
  • 3. JLabel  We use a JLabel object to display a label.  A label can be a text or an image.  When creating an image label, we pass ImageIcon object instead of a string. JLabel textLabel = new JLabel("Please enter your name"); contentPane.add(textLabel); JLabel imgLabel = new JLabel(new ImageIcon("cat.gif")); contentPane.add(imgLabel);
  • 4. JTextArea  We use a JTextField object to accept a single line to text from a user. An action event is generated when the user presses the ENTER key.  The getText method of JTextField is used to retrieve the text that the user entered. JTextField input = new JTextField( ); contentPane.add(input);
  • 6. JTextArea  We use a JTextArea object to display or allow the user to enter multiple lines of text.  The setText method assigns the text to a JTextArea, replacing the current content.  The append method appends the text to the current text. JTextArea textArea = new JTextArea( ); . . . textArea.setText("Hellon"); textArea.append("the lost "); textArea.append("world"); Hello the lost world JTextArea
  • 8. Adding Scroll Bars to JTextArea  By default a JTextArea does not have any scroll bars. To add scroll bars, we place a JTextArea in a JScrollPane object. JTextArea textArea = new JTextArea(); . . . JScrollPane scrollText = new JScrollPane(textArea); . . . contentPane.add(scrollText);
  • 9.  A sample window when a JScrollPane is used.
  • 10. Other Common GUI Components  JCheckBox  JRadioButton  JComboBox  JList
  • 11. Menus  The javax.swing package contains three menu- related classes: JMenuBar, JMenu, and JMenuItem.  JMenuBar is a bar where the menus are placed. There is one menu bar per frame.  JMenu (such as File or Edit) is a group of menu choices. JMenuBar may include many JMenu objects.  JMenuItem (such as Copy, Cut, or Paste) is an individual menu choice in a JMenu object.  Only the JMenuItem objects generate events.
  • 12. Menu Components Edit View Help JMenuBar Edit View HelpFile JMenu JMenuItem separator
  • 13. Sequence for Creating Menus 1. Create a JMenuBar object and attach it to a frame. 2. Create a JMenu object. 3. Create JMenuItem objects and add them to the JMenu object. 4. Attach the JMenu object to the JMenuBar object.