SlideShare uma empresa Scribd logo
1 de 29
Chapter 17
Creating User Interfaces




Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All
                                     rights reserved.
                                                                                               1
Motivations
A graphical user interface (GUI) makes a system
user-friendly and easy to use. Creating a GUI
requires creativity and knowledge of how GUI
components work. Since the GUI components in
Java are very flexible and versatile, you can create
a wide assortment of useful user interfaces.

Previous chapters briefly introduced several GUI
components. This chapter introduces the
frequently used GUI components in detail.
         Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All
                                              rights reserved.
                                                                                                        2
Objectives
   To create graphical user interfaces with various user-
    interface components (§§17.2–17.8).
   To create listeners for JCheckBox, JRadioButton, and
    JTextField (§17.2).
   To enter multiple-line texts using JTextArea (§17.3).
   To select a single item using JComboBox (§17.4).
   To select a single or multiple items using JList (§17.5).
   To select a range of values using JScrollBar (§17.6).
   To select a range of values using JSlider and explore
    differences between JScrollBar and JSlider (§17.7).
   To display multiple windows in an application (§17.8).

             Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All
                                                  rights reserved.
                                                                                                            3
Events for JCheckBox,
JRadioButton, and JTextField




                                                                     GUIEventDemo

                                                                                   Run
  Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All
                                       rights reserved.
                                                                                                 4
JTextArea
If you want to let the user enter multiple lines of text, you cannot use
text fields unless you create several of them. The solution is to use
JTextArea, which enables the user to enter multiple lines of text.

     javax.swing.text.JTextComponent                           The get and set methods for these data fields are provided in
                                                               the class, but omitted in the UML diagram for brevity.
               javax.swing.JTextArea
  -columns: int                                        The number of columns in this text area.
  -rows: int                                           The number of rows in this text area.
  -tabSize: int                                        The number of characters used to expand tabs (default: 8).
  -lineWrap: boolean                                   Indicates whether the line in the text area is automatically wrapped (default:
                                                        false).
  -wrapStyleWord: boolean                              Indicates whether the line is wrapped on words or characters (default: false).
  +JTextArea()                                         Creates a default empty text area.
  +JTextArea(rows: int, columns: int)                  Creates an empty text area with the specified number of rows and columns.
  +JTextArea(text: String)                             Creates a new text area with the specified text displayed.
  +JTextArea(text: String, rows: int, columns: int) Creates a new text area with the specified text and number of rows and columns.
  +append(s: String): void                          Appends the string to text in the text area.
  +insert(s: String, pos: int): void                   Inserts string s in the specified position in the text area.
  +replaceRange(s: String, start: int, end: int):      Replaces partial text in the range from position start to end with string s.
    void
  +getLineCount(): int                                 Returns the actual number of lines contained in the text area.

                        Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All
                                                             rights reserved.
                                                                                                                                        5
JTextArea Constructors
   JTextArea(int rows, int columns)
    Creates a text area with the specified number of
    rows and columns.

   JTextArea(String s, int rows, int
    columns)
    Creates a text area with the initial text and
    the number of rows and columns specified.




          Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All
                                               rights reserved.
                                                                                                         6
JTextArea Properties
 text
 editable
 columns
 lineWrap
 wrapStyleWord
 rows
 lineCount
 tabSize
    Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All
                                         rights reserved.
                                                                                                   7
Example: Using Text Areas
 This example gives a program that displays
 an image in a label, a title in a label, and a
 text in a text area.

            javax.swing.JPanel                                               javax.swing.JFrame
    -char token                                                           -char token

     +getToken                                    1                  1    +getToken
             DescriptionPanel                                                   TextAreaDemo
     +setToken                                                            +setToken
     +paintComponet                                                       +paintComponet
    -jlblImageTitle: JLabel
     +mouseClicked                                                        +mouseClicked
    -jtaTextDescription: JTextArea

    +setImageIcon(icon: ImageIcon): void
    +setTitle(title: String): void
    +setTextDescription(text: String): void
    +getMinimumSize(): Dimension



         Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All
                                              rights reserved.
                                                                                                        8
Example, cont.




DiscriptionPanel                                TextAreaDemo                                   Run

Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All
                                     rights reserved.
                                                                                                     9
JComboBox
A combo box is a simple list of items from which the user can
choose. It performs basically the same function as a list, but
can get only one value.
       javax.swing.JComponent

       javax.swing.JComboBox
    +JComboBox()                                 Creates a default empty combo box.
    +JComboBox(items: Object[])                  Creates a combo box that contains the elements in the specified array.
    +addItem(item: Object): void                 Adds an item to the combo box.
    +getItemAt(index: int): Object               Returns the item at the specified index.
    +getItemCount(): int                         Returns the number of items in the combo box.
    +getSelectedIndex(): int                     Returns the index of the selected item.
    +setSelectedIndex(index: int): void          Sets the selected index in the combo box.
    +getSelectedItem(): Object                   Returns the selected item.
    +setSelectedItem(item: Object): void         Sets the selected item in the combo box.
    +removeItem(anObject: Object): void Removes an item from the item list.
    +removeItemAt(anIndex: int): void   Removes the item at the specified index in the combo box.
    +removeAllItems(): void                      Removes all items in the combo box.

                 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All
                                                      rights reserved.
                                                                                                                  10
JComboBox Methods
To add an item to a JComboBox jcbo, use
jcbo.addItem(Object item)

To get an item from JComboBox jcbo, use
jcbo.getItem()




     Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All
                                          rights reserved.
                                                                                                    11
Using the
   itemStateChanged Handler
  When a choice is checked or unchecked,
  itemStateChanged() for ItemEvent is
  invoked as well as the actionPerformed()
  handler for ActionEvent.
public void itemStateChanged(ItemEvent e) {
  // Make sure the source is a combo box
  if (e.getSource() instanceof JComboBox)
    String s = (String)e.getItem();
}

       Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All
                                            rights reserved.
                                                                                                      12
Example: Using Combo Boxes
This example lets
users view an
image and a
description of a
country's flag by
selecting the
country from a
combo box.

       ComboBoxDemo                                                              Run

        Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All
                                             rights reserved.
                                                                                                       13
JList
A list is a component that performs basically the same function as a combo
box, but it enables the user to choose a single value or multiple values.
        javax.swing.JComponent

              javax.swing.JList
   +JList()                                     Creates a default empty list.
   +JList(items: Object[])                      Creates a list that contains the elements in the specified array.
   +getSelectedIndex(): int                     Returns the index of the first selected item.
   +setSelectedIndex(index: int): void          Selects the cell at the specified index.
   +getSelectedIndices(): int[]                 Returns an array of all of the selected indices in increasing order.
   +setSelectedIndices(indices: int[]): void Selects the cells at the specified indices.
   +getSelectedValue(): Object               Returns the first selected item in the list.
   +getSelectedValues(): Object[]               Returns an array of the values for the selected cells in increasing index order.
   +getVisibleRowCount(): int                   Returns the number of visible rows displayed without a scrollbar. (default: 8)
   +setVisibleRowCount(count: int): void        Sets the preferred number of visible rows displayed without a scrollbar.
   +getSelectionBackground(): Color             Returns the background color of the selected cells.
   +setSelectionBackground(c: Color): void Sets the background color of the selected cells.
   +getSelectionForeground(): Color        Returns the foreground color of the selected cells.
   +setSelectionForeground(c: Color): void Sets the foreground color of the selected cells.
   +getSelectionMode(): int                Returns the selection mode for the list.
   +setSelectionMode(selectionMode: int): Sets the selection mode for the list.
                    Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All
                                                         rights reserved.
                                                                                                                             14
JList Constructors
   JList()

    Creates an empty list.

   JList(Object[] stringItems)
    Creates a new list initialized with items.




          Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All
                                               rights reserved.
                                                                                                         15
JList Properties
 selectedIndexd

 selectedIndices

 selectedValue

 selectedValues

 selectionMode

 visibleRowCount
    Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All
                                         rights reserved.
                                                                                                   16
Example: Using Lists
This example gives
a program that lets
users select
countries in a list
and display the flags
of the selected
countries in the
labels.


                   ListDemo                                                              Run
         Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All
                                              rights reserved.
                                                                                                        17
JScrollBar
A scroll bar is a control that enables the user to select from a range of values. The
scrollbar appears in two styles: horizontal and vertical.
               javax.swing.JComponent
                                                        The get and set methods for these data fields are provided in
                                                        the class, but omitted in the UML diagram for brevity.
                javax.swing.JScrollBar
             -orientation: int                     Specifies horizontal or vertical style, default is horizontal.
             -maximum: int                         Specifies the maximum value the scroll bar represents when the bubble
                                                     reaches the right end of the scroll bar for horizontal style or the
                                                     bottom of the scroll bar for vertical style.
             -minimum: int                         Specifies the minimum value the scroll bar represents when the bubble
                                                     reaches the left end of the scroll bar for horizontal style or the top of
                                                     the scroll bar for vertical style.
             -visibleAmount: int                   Specifies the relative width of the scroll bar's bubble. The actual width
                                                     appearing on the screen is determined by the maximum value and the
                                                     value of visibleAmount.
             -value: int                           Represents the current value of the scroll bar.
             -blockIncrement: int                  Specifies value added (subtracted) when the user activates the block-
                                                     increment (decrement) area of the scroll bar, as shown in Figure
                                                     13.30.
             -unitIncrement: int                   Specifies the value added (subtracted) when the user activates the unit-
                                                     increment (decrement) area of the scroll bar, as shown in Figure
                                                     13.30.

             +JScrollBar()                         Creates a default vertical scroll bar.
             +JScrollBar(orientation: int)         Creates a scroll bar with the specified orientation.
             +JScrollBar(orientation: int, value: Creates a scrollbar with the specified orientation, value, extent,
               int, extent: int, min: int, max: int) minimum, and maximum.

                   Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All
                                                        rights reserved.
                                                                                                                                 18
Scroll Bar Properties
Minimal value                                                                     Maximal value

             Block decrement                                   Block increment




                                            Bubble
 Unit decrement                                                           Unit increment



     Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All
                                          rights reserved.
                                                                                                    19
Example: Using Scrollbars
This example uses
horizontal and vertical
scrollbars to control a
message displayed on a
panel. The horizontal
scrollbar is used to move
the message to the left or
the right, and the vertical
scrollbar to move it up and
down.


            ScrollBarDemo                                                                       Run
          Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All
                                               rights reserved.
                                                                                                         20
JSlider
JSlider is similar to JScrollBar, but JSlider has more
properties and can appear in many forms.
            javax.swing.JComponent
                                                          The get and set methods for these data fields are provided in
                                                          the class, but omitted in the UML diagram for brevity.
                javax.swing.JSlider
       -maximum: int                                 The maximum value represented by the slider (default: 100).
       -minimum: int                                 The minimum value represented by the slider (default: 0).
       -value: int                                   The current value represented by the slider.
       -orientation: int                             The orientation of the slider (default: JSlider.HORIZONTAL).
       -paintLabels: boolean                         True if the labels are painted at tick marks (default: false).
       -paintTicks: boolean                          True if the ticks are painted on the slider (default: false).
       -paintTrack: boolean                          True if the track is painted on the slider (default: true).
       -majorTickSpacing: int                        The number of units between major ticks (default: 0).
       -minorTickSpacing: int                        The number of units between minor ticks (default: 0).
       -inverted: boolean                            True to reverse the value-range, and false to put the value range in the
                                                      normal order (default: false).
       +JSlider()                                    Creates a default horizontal slider.
       +JSlider(min: int, max: int)                  Creates a horizontal slider using the specified min and max.
       +JSlider(min: int, max: int, value: int)      Creates a horizontal slider using the specified min, max, and value.
       +JSlider(orientation: int)                    Creates a slider with the specified orientation.
       +JSlider(orientation: int, min: int, max:     Creates a slider with the specified orientation, min, max, and value.
         int, value: int)

                     Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All
                                                          rights reserved.
                                                                                                                                21
Example: Using Sliders
Rewrite the preceding
program using the sliders
to control a message
displayed on a panel
instead of using scroll
bars.




                SliderDemo                                                                      Run
          Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All
                                               rights reserved.
                                                                                                         22
Creating Multiple Windows
The following slides show step-by-step how to
create an additional window from an application
or applet.




     Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All
                                          rights reserved.
                                                                                                    23
Creating Additional Windows, Step 1

Step 1: Create a subclass of JFrame (called a
SubFrame) that tells the new window what
to do. For example, all the GUI application
programs extend JFrame and are subclasses
of JFrame.




     Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All
                                          rights reserved.
                                                                                                    24
Creating Additional Windows, Step 2

Step 2: Create an instance of SubFrame in the
application or applet.

Example:
SubFrame subFrame = new
  SubFrame("SubFrame Title");




     Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All
                                          rights reserved.
                                                                                                    25
Creating Additional Windows, Step 3

Step 3: Create a JButton for activating the
subFrame.
add(new JButton("Activate SubFrame"));




     Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All
                                          rights reserved.
                                                                                                    26
Creating Additional Windows, Step 4

Step 4: Override the actionPerformed()
method as follows:
public actionPerformed(ActionEvent e) {
  String actionCommand = e.getActionCommand();
  if (e.target instanceof Button) {
    if ("Activate SubFrame".equals(actionCommand)) {
      subFrame.setVisible(true);
    }
  }
}




           Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All
                                                rights reserved.
                                                                                                          27
Example: Creating Multiple
           Windows
 This example creates a main window with a
 text area in the scroll pane, and a button
 named "Show Histogram." When the user
 clicks the button, a new window appears
 that displays a histogram to show the
 occurrence of the letters in the text area.




         Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All
                                              rights reserved.
                                                                                                        28
Example, cont.




MultipleWindowsDemo                                                                   Run

       Histogram

    Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All
                                         rights reserved.
                                                                                                   29

Mais conteúdo relacionado

Mais procurados

javaimplementation
javaimplementationjavaimplementation
javaimplementationFaRaz Ahmad
 
Introduction to Client-Side Javascript
Introduction to Client-Side JavascriptIntroduction to Client-Side Javascript
Introduction to Client-Side JavascriptJulie Iskander
 
Kotlin- Basic to Advance
Kotlin- Basic to Advance Kotlin- Basic to Advance
Kotlin- Basic to Advance Coder Tech
 
Xbase - Implementing Domain-Specific Languages for Java
Xbase - Implementing Domain-Specific Languages for JavaXbase - Implementing Domain-Specific Languages for Java
Xbase - Implementing Domain-Specific Languages for Javameysholdt
 
Objectiveccheatsheet
ObjectiveccheatsheetObjectiveccheatsheet
Objectiveccheatsheetiderdelzo
 
String handling(string buffer class)
String handling(string buffer class)String handling(string buffer class)
String handling(string buffer class)Ravi Kant Sahu
 
Java Reference
Java ReferenceJava Reference
Java Referencekhoj4u
 
Generic Programming in java
Generic Programming in javaGeneric Programming in java
Generic Programming in javaGarik Kalashyan
 
Sdtl manual
Sdtl manualSdtl manual
Sdtl manualqaz8989
 
C# Summer course - Lecture 3
C# Summer course - Lecture 3C# Summer course - Lecture 3
C# Summer course - Lecture 3mohamedsamyali
 
Java class 4
Java class 4Java class 4
Java class 4Edureka!
 
DISE - Windows Based Application Development in Java
DISE - Windows Based Application Development in JavaDISE - Windows Based Application Development in Java
DISE - Windows Based Application Development in JavaRasan Samarasinghe
 
Programming in Java: Storing Data
Programming in Java: Storing DataProgramming in Java: Storing Data
Programming in Java: Storing DataMartin Chapman
 

Mais procurados (20)

Fast Forward To Scala
Fast Forward To ScalaFast Forward To Scala
Fast Forward To Scala
 
javaimplementation
javaimplementationjavaimplementation
javaimplementation
 
Introduction to Client-Side Javascript
Introduction to Client-Side JavascriptIntroduction to Client-Side Javascript
Introduction to Client-Side Javascript
 
Kotlin- Basic to Advance
Kotlin- Basic to Advance Kotlin- Basic to Advance
Kotlin- Basic to Advance
 
Xbase - Implementing Domain-Specific Languages for Java
Xbase - Implementing Domain-Specific Languages for JavaXbase - Implementing Domain-Specific Languages for Java
Xbase - Implementing Domain-Specific Languages for Java
 
Objectiveccheatsheet
ObjectiveccheatsheetObjectiveccheatsheet
Objectiveccheatsheet
 
Wrapper class
Wrapper classWrapper class
Wrapper class
 
First fare 2010 java-introduction
First fare 2010 java-introductionFirst fare 2010 java-introduction
First fare 2010 java-introduction
 
String handling(string buffer class)
String handling(string buffer class)String handling(string buffer class)
String handling(string buffer class)
 
Vectors in Java
Vectors in JavaVectors in Java
Vectors in Java
 
Java Reference
Java ReferenceJava Reference
Java Reference
 
08slide
08slide08slide
08slide
 
Wrapper classes
Wrapper classesWrapper classes
Wrapper classes
 
Generic Programming in java
Generic Programming in javaGeneric Programming in java
Generic Programming in java
 
Sdtl manual
Sdtl manualSdtl manual
Sdtl manual
 
Obvious Secrets of JavaScript
Obvious Secrets of JavaScriptObvious Secrets of JavaScript
Obvious Secrets of JavaScript
 
C# Summer course - Lecture 3
C# Summer course - Lecture 3C# Summer course - Lecture 3
C# Summer course - Lecture 3
 
Java class 4
Java class 4Java class 4
Java class 4
 
DISE - Windows Based Application Development in Java
DISE - Windows Based Application Development in JavaDISE - Windows Based Application Development in Java
DISE - Windows Based Application Development in Java
 
Programming in Java: Storing Data
Programming in Java: Storing DataProgramming in Java: Storing Data
Programming in Java: Storing Data
 

Destaque (16)

09slide
09slide09slide
09slide
 
03slide
03slide03slide
03slide
 
12slide
12slide12slide
12slide
 
06slide
06slide06slide
06slide
 
JavaYDL16
JavaYDL16JavaYDL16
JavaYDL16
 
04slide
04slide04slide
04slide
 
JavaYDL18
JavaYDL18JavaYDL18
JavaYDL18
 
01slide
01slide01slide
01slide
 
JavaYDL14
JavaYDL14JavaYDL14
JavaYDL14
 
Ch2 Liang
Ch2 LiangCh2 Liang
Ch2 Liang
 
JavaYDL19
JavaYDL19JavaYDL19
JavaYDL19
 
JavaYDL6
JavaYDL6JavaYDL6
JavaYDL6
 
10slide
10slide10slide
10slide
 
13slide graphics
13slide graphics13slide graphics
13slide graphics
 
JavaYDL20
JavaYDL20JavaYDL20
JavaYDL20
 
9781285852744 ppt ch02
9781285852744 ppt ch029781285852744 ppt ch02
9781285852744 ppt ch02
 

Semelhante a JavaYDL17

extragui.ppt
extragui.pptextragui.ppt
extragui.pptZeenatJ1
 
DSJ_Unit III.pdf
DSJ_Unit III.pdfDSJ_Unit III.pdf
DSJ_Unit III.pdfArumugam90
 
String handling session 5
String handling session 5String handling session 5
String handling session 5Raja Sekhar
 
Introduction to objective c
Introduction to objective cIntroduction to objective c
Introduction to objective cMayank Jalotra
 
Chapter 2 - Getting Started with Java
Chapter 2 - Getting Started with JavaChapter 2 - Getting Started with Java
Chapter 2 - Getting Started with JavaEduardo Bergavera
 
Chap 1.5 - Java Documentation.pdf
Chap 1.5 - Java Documentation.pdfChap 1.5 - Java Documentation.pdf
Chap 1.5 - Java Documentation.pdfssuser2c5ec41
 
Collections and generic class
Collections and generic classCollections and generic class
Collections and generic classifis
 
LiangChapter4 Unicode , ASCII Code .ppt
LiangChapter4 Unicode , ASCII Code  .pptLiangChapter4 Unicode , ASCII Code  .ppt
LiangChapter4 Unicode , ASCII Code .pptzainiiqbal761
 
Basics java programing
Basics java programingBasics java programing
Basics java programingDarshan Gohel
 
Introduction to Intermediate Java
Introduction to Intermediate JavaIntroduction to Intermediate Java
Introduction to Intermediate JavaPhilip Johnson
 
JavaProgramming 17321_CS202-Chapter8.ppt
JavaProgramming 17321_CS202-Chapter8.pptJavaProgramming 17321_CS202-Chapter8.ppt
JavaProgramming 17321_CS202-Chapter8.pptMohammedNouh7
 
Std 12 Computer Chapter 7 Java Basics (Part 1)
Std 12 Computer Chapter 7 Java Basics (Part 1)Std 12 Computer Chapter 7 Java Basics (Part 1)
Std 12 Computer Chapter 7 Java Basics (Part 1)Nuzhat Memon
 
James Jesus Bermas on Crash Course on Python
James Jesus Bermas on Crash Course on PythonJames Jesus Bermas on Crash Course on Python
James Jesus Bermas on Crash Course on PythonCP-Union
 

Semelhante a JavaYDL17 (20)

extragui.ppt
extragui.pptextragui.ppt
extragui.ppt
 
DSJ_Unit III.pdf
DSJ_Unit III.pdfDSJ_Unit III.pdf
DSJ_Unit III.pdf
 
Java swing
Java swingJava swing
Java swing
 
String handling session 5
String handling session 5String handling session 5
String handling session 5
 
Introduction to objective c
Introduction to objective cIntroduction to objective c
Introduction to objective c
 
Chapter 2 - Getting Started with Java
Chapter 2 - Getting Started with JavaChapter 2 - Getting Started with Java
Chapter 2 - Getting Started with Java
 
Chap 1.5 - Java Documentation.pdf
Chap 1.5 - Java Documentation.pdfChap 1.5 - Java Documentation.pdf
Chap 1.5 - Java Documentation.pdf
 
JavaYDL12
JavaYDL12JavaYDL12
JavaYDL12
 
Collections and generic class
Collections and generic classCollections and generic class
Collections and generic class
 
Strings in Java
Strings in JavaStrings in Java
Strings in Java
 
LiangChapter4 Unicode , ASCII Code .ppt
LiangChapter4 Unicode , ASCII Code  .pptLiangChapter4 Unicode , ASCII Code  .ppt
LiangChapter4 Unicode , ASCII Code .ppt
 
Basics java programing
Basics java programingBasics java programing
Basics java programing
 
Java quickref
Java quickrefJava quickref
Java quickref
 
Introduction to Intermediate Java
Introduction to Intermediate JavaIntroduction to Intermediate Java
Introduction to Intermediate Java
 
JavaProgramming 17321_CS202-Chapter8.ppt
JavaProgramming 17321_CS202-Chapter8.pptJavaProgramming 17321_CS202-Chapter8.ppt
JavaProgramming 17321_CS202-Chapter8.ppt
 
String handling
String handlingString handling
String handling
 
JavaAdvUnit-1.pptx
JavaAdvUnit-1.pptxJavaAdvUnit-1.pptx
JavaAdvUnit-1.pptx
 
Std 12 Computer Chapter 7 Java Basics (Part 1)
Std 12 Computer Chapter 7 Java Basics (Part 1)Std 12 Computer Chapter 7 Java Basics (Part 1)
Std 12 Computer Chapter 7 Java Basics (Part 1)
 
CH1 ARRAY (1).pptx
CH1 ARRAY (1).pptxCH1 ARRAY (1).pptx
CH1 ARRAY (1).pptx
 
James Jesus Bermas on Crash Course on Python
James Jesus Bermas on Crash Course on PythonJames Jesus Bermas on Crash Course on Python
James Jesus Bermas on Crash Course on Python
 

Mais de Terry Yoast

9781305078444 ppt ch12
9781305078444 ppt ch129781305078444 ppt ch12
9781305078444 ppt ch12Terry Yoast
 
9781305078444 ppt ch11
9781305078444 ppt ch119781305078444 ppt ch11
9781305078444 ppt ch11Terry Yoast
 
9781305078444 ppt ch10
9781305078444 ppt ch109781305078444 ppt ch10
9781305078444 ppt ch10Terry Yoast
 
9781305078444 ppt ch09
9781305078444 ppt ch099781305078444 ppt ch09
9781305078444 ppt ch09Terry Yoast
 
9781305078444 ppt ch08
9781305078444 ppt ch089781305078444 ppt ch08
9781305078444 ppt ch08Terry Yoast
 
9781305078444 ppt ch07
9781305078444 ppt ch079781305078444 ppt ch07
9781305078444 ppt ch07Terry Yoast
 
9781305078444 ppt ch06
9781305078444 ppt ch069781305078444 ppt ch06
9781305078444 ppt ch06Terry Yoast
 
9781305078444 ppt ch05
9781305078444 ppt ch059781305078444 ppt ch05
9781305078444 ppt ch05Terry Yoast
 
9781305078444 ppt ch04
9781305078444 ppt ch049781305078444 ppt ch04
9781305078444 ppt ch04Terry Yoast
 
9781305078444 ppt ch03
9781305078444 ppt ch039781305078444 ppt ch03
9781305078444 ppt ch03Terry Yoast
 
9781305078444 ppt ch02
9781305078444 ppt ch029781305078444 ppt ch02
9781305078444 ppt ch02Terry Yoast
 
9781305078444 ppt ch01
9781305078444 ppt ch019781305078444 ppt ch01
9781305078444 ppt ch01Terry Yoast
 
9781337102087 ppt ch13
9781337102087 ppt ch139781337102087 ppt ch13
9781337102087 ppt ch13Terry Yoast
 
9781337102087 ppt ch18
9781337102087 ppt ch189781337102087 ppt ch18
9781337102087 ppt ch18Terry Yoast
 
9781337102087 ppt ch17
9781337102087 ppt ch179781337102087 ppt ch17
9781337102087 ppt ch17Terry Yoast
 
9781337102087 ppt ch16
9781337102087 ppt ch169781337102087 ppt ch16
9781337102087 ppt ch16Terry Yoast
 
9781337102087 ppt ch15
9781337102087 ppt ch159781337102087 ppt ch15
9781337102087 ppt ch15Terry Yoast
 
9781337102087 ppt ch14
9781337102087 ppt ch149781337102087 ppt ch14
9781337102087 ppt ch14Terry Yoast
 
9781337102087 ppt ch12
9781337102087 ppt ch129781337102087 ppt ch12
9781337102087 ppt ch12Terry Yoast
 
9781337102087 ppt ch11
9781337102087 ppt ch119781337102087 ppt ch11
9781337102087 ppt ch11Terry Yoast
 

Mais de Terry Yoast (20)

9781305078444 ppt ch12
9781305078444 ppt ch129781305078444 ppt ch12
9781305078444 ppt ch12
 
9781305078444 ppt ch11
9781305078444 ppt ch119781305078444 ppt ch11
9781305078444 ppt ch11
 
9781305078444 ppt ch10
9781305078444 ppt ch109781305078444 ppt ch10
9781305078444 ppt ch10
 
9781305078444 ppt ch09
9781305078444 ppt ch099781305078444 ppt ch09
9781305078444 ppt ch09
 
9781305078444 ppt ch08
9781305078444 ppt ch089781305078444 ppt ch08
9781305078444 ppt ch08
 
9781305078444 ppt ch07
9781305078444 ppt ch079781305078444 ppt ch07
9781305078444 ppt ch07
 
9781305078444 ppt ch06
9781305078444 ppt ch069781305078444 ppt ch06
9781305078444 ppt ch06
 
9781305078444 ppt ch05
9781305078444 ppt ch059781305078444 ppt ch05
9781305078444 ppt ch05
 
9781305078444 ppt ch04
9781305078444 ppt ch049781305078444 ppt ch04
9781305078444 ppt ch04
 
9781305078444 ppt ch03
9781305078444 ppt ch039781305078444 ppt ch03
9781305078444 ppt ch03
 
9781305078444 ppt ch02
9781305078444 ppt ch029781305078444 ppt ch02
9781305078444 ppt ch02
 
9781305078444 ppt ch01
9781305078444 ppt ch019781305078444 ppt ch01
9781305078444 ppt ch01
 
9781337102087 ppt ch13
9781337102087 ppt ch139781337102087 ppt ch13
9781337102087 ppt ch13
 
9781337102087 ppt ch18
9781337102087 ppt ch189781337102087 ppt ch18
9781337102087 ppt ch18
 
9781337102087 ppt ch17
9781337102087 ppt ch179781337102087 ppt ch17
9781337102087 ppt ch17
 
9781337102087 ppt ch16
9781337102087 ppt ch169781337102087 ppt ch16
9781337102087 ppt ch16
 
9781337102087 ppt ch15
9781337102087 ppt ch159781337102087 ppt ch15
9781337102087 ppt ch15
 
9781337102087 ppt ch14
9781337102087 ppt ch149781337102087 ppt ch14
9781337102087 ppt ch14
 
9781337102087 ppt ch12
9781337102087 ppt ch129781337102087 ppt ch12
9781337102087 ppt ch12
 
9781337102087 ppt ch11
9781337102087 ppt ch119781337102087 ppt ch11
9781337102087 ppt ch11
 

Último

Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxCarlos105
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxChelloAnnAsuncion2
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfSpandanaRallapalli
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfMr Bounab Samir
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
Science 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxScience 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxMaryGraceBautista27
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONHumphrey A Beña
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxDr.Ibrahim Hassaan
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 
Q4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptxQ4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptxnelietumpap1
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptxmary850239
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4MiaBumagat1
 

Último (20)

Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
 
Raw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptxRaw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptx
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdf
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
Science 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxScience 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptx
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptx
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 
Q4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptxQ4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptx
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx
 
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptxYOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptxLEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4
 

JavaYDL17

  • 1. Chapter 17 Creating User Interfaces Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1
  • 2. Motivations A graphical user interface (GUI) makes a system user-friendly and easy to use. Creating a GUI requires creativity and knowledge of how GUI components work. Since the GUI components in Java are very flexible and versatile, you can create a wide assortment of useful user interfaces. Previous chapters briefly introduced several GUI components. This chapter introduces the frequently used GUI components in detail. Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 2
  • 3. Objectives  To create graphical user interfaces with various user- interface components (§§17.2–17.8).  To create listeners for JCheckBox, JRadioButton, and JTextField (§17.2).  To enter multiple-line texts using JTextArea (§17.3).  To select a single item using JComboBox (§17.4).  To select a single or multiple items using JList (§17.5).  To select a range of values using JScrollBar (§17.6).  To select a range of values using JSlider and explore differences between JScrollBar and JSlider (§17.7).  To display multiple windows in an application (§17.8). Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 3
  • 4. Events for JCheckBox, JRadioButton, and JTextField GUIEventDemo Run Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 4
  • 5. JTextArea If you want to let the user enter multiple lines of text, you cannot use text fields unless you create several of them. The solution is to use JTextArea, which enables the user to enter multiple lines of text. javax.swing.text.JTextComponent The get and set methods for these data fields are provided in the class, but omitted in the UML diagram for brevity. javax.swing.JTextArea -columns: int The number of columns in this text area. -rows: int The number of rows in this text area. -tabSize: int The number of characters used to expand tabs (default: 8). -lineWrap: boolean Indicates whether the line in the text area is automatically wrapped (default: false). -wrapStyleWord: boolean Indicates whether the line is wrapped on words or characters (default: false). +JTextArea() Creates a default empty text area. +JTextArea(rows: int, columns: int) Creates an empty text area with the specified number of rows and columns. +JTextArea(text: String) Creates a new text area with the specified text displayed. +JTextArea(text: String, rows: int, columns: int) Creates a new text area with the specified text and number of rows and columns. +append(s: String): void Appends the string to text in the text area. +insert(s: String, pos: int): void Inserts string s in the specified position in the text area. +replaceRange(s: String, start: int, end: int): Replaces partial text in the range from position start to end with string s. void +getLineCount(): int Returns the actual number of lines contained in the text area. Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 5
  • 6. JTextArea Constructors  JTextArea(int rows, int columns) Creates a text area with the specified number of rows and columns.  JTextArea(String s, int rows, int columns) Creates a text area with the initial text and the number of rows and columns specified. Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 6
  • 7. JTextArea Properties  text  editable  columns  lineWrap  wrapStyleWord  rows  lineCount  tabSize Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 7
  • 8. Example: Using Text Areas  This example gives a program that displays an image in a label, a title in a label, and a text in a text area. javax.swing.JPanel javax.swing.JFrame -char token -char token +getToken 1 1 +getToken DescriptionPanel TextAreaDemo +setToken +setToken +paintComponet +paintComponet -jlblImageTitle: JLabel +mouseClicked +mouseClicked -jtaTextDescription: JTextArea +setImageIcon(icon: ImageIcon): void +setTitle(title: String): void +setTextDescription(text: String): void +getMinimumSize(): Dimension Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 8
  • 9. Example, cont. DiscriptionPanel TextAreaDemo Run Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 9
  • 10. JComboBox A combo box is a simple list of items from which the user can choose. It performs basically the same function as a list, but can get only one value. javax.swing.JComponent javax.swing.JComboBox +JComboBox() Creates a default empty combo box. +JComboBox(items: Object[]) Creates a combo box that contains the elements in the specified array. +addItem(item: Object): void Adds an item to the combo box. +getItemAt(index: int): Object Returns the item at the specified index. +getItemCount(): int Returns the number of items in the combo box. +getSelectedIndex(): int Returns the index of the selected item. +setSelectedIndex(index: int): void Sets the selected index in the combo box. +getSelectedItem(): Object Returns the selected item. +setSelectedItem(item: Object): void Sets the selected item in the combo box. +removeItem(anObject: Object): void Removes an item from the item list. +removeItemAt(anIndex: int): void Removes the item at the specified index in the combo box. +removeAllItems(): void Removes all items in the combo box. Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 10
  • 11. JComboBox Methods To add an item to a JComboBox jcbo, use jcbo.addItem(Object item) To get an item from JComboBox jcbo, use jcbo.getItem() Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 11
  • 12. Using the itemStateChanged Handler When a choice is checked or unchecked, itemStateChanged() for ItemEvent is invoked as well as the actionPerformed() handler for ActionEvent. public void itemStateChanged(ItemEvent e) { // Make sure the source is a combo box if (e.getSource() instanceof JComboBox) String s = (String)e.getItem(); } Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 12
  • 13. Example: Using Combo Boxes This example lets users view an image and a description of a country's flag by selecting the country from a combo box. ComboBoxDemo Run Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 13
  • 14. JList A list is a component that performs basically the same function as a combo box, but it enables the user to choose a single value or multiple values. javax.swing.JComponent javax.swing.JList +JList() Creates a default empty list. +JList(items: Object[]) Creates a list that contains the elements in the specified array. +getSelectedIndex(): int Returns the index of the first selected item. +setSelectedIndex(index: int): void Selects the cell at the specified index. +getSelectedIndices(): int[] Returns an array of all of the selected indices in increasing order. +setSelectedIndices(indices: int[]): void Selects the cells at the specified indices. +getSelectedValue(): Object Returns the first selected item in the list. +getSelectedValues(): Object[] Returns an array of the values for the selected cells in increasing index order. +getVisibleRowCount(): int Returns the number of visible rows displayed without a scrollbar. (default: 8) +setVisibleRowCount(count: int): void Sets the preferred number of visible rows displayed without a scrollbar. +getSelectionBackground(): Color Returns the background color of the selected cells. +setSelectionBackground(c: Color): void Sets the background color of the selected cells. +getSelectionForeground(): Color Returns the foreground color of the selected cells. +setSelectionForeground(c: Color): void Sets the foreground color of the selected cells. +getSelectionMode(): int Returns the selection mode for the list. +setSelectionMode(selectionMode: int): Sets the selection mode for the list. Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 14
  • 15. JList Constructors  JList() Creates an empty list.  JList(Object[] stringItems) Creates a new list initialized with items. Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 15
  • 16. JList Properties  selectedIndexd  selectedIndices  selectedValue  selectedValues  selectionMode  visibleRowCount Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 16
  • 17. Example: Using Lists This example gives a program that lets users select countries in a list and display the flags of the selected countries in the labels. ListDemo Run Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 17
  • 18. JScrollBar A scroll bar is a control that enables the user to select from a range of values. The scrollbar appears in two styles: horizontal and vertical. javax.swing.JComponent The get and set methods for these data fields are provided in the class, but omitted in the UML diagram for brevity. javax.swing.JScrollBar -orientation: int Specifies horizontal or vertical style, default is horizontal. -maximum: int Specifies the maximum value the scroll bar represents when the bubble reaches the right end of the scroll bar for horizontal style or the bottom of the scroll bar for vertical style. -minimum: int Specifies the minimum value the scroll bar represents when the bubble reaches the left end of the scroll bar for horizontal style or the top of the scroll bar for vertical style. -visibleAmount: int Specifies the relative width of the scroll bar's bubble. The actual width appearing on the screen is determined by the maximum value and the value of visibleAmount. -value: int Represents the current value of the scroll bar. -blockIncrement: int Specifies value added (subtracted) when the user activates the block- increment (decrement) area of the scroll bar, as shown in Figure 13.30. -unitIncrement: int Specifies the value added (subtracted) when the user activates the unit- increment (decrement) area of the scroll bar, as shown in Figure 13.30. +JScrollBar() Creates a default vertical scroll bar. +JScrollBar(orientation: int) Creates a scroll bar with the specified orientation. +JScrollBar(orientation: int, value: Creates a scrollbar with the specified orientation, value, extent, int, extent: int, min: int, max: int) minimum, and maximum. Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 18
  • 19. Scroll Bar Properties Minimal value Maximal value Block decrement Block increment Bubble Unit decrement Unit increment Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 19
  • 20. Example: Using Scrollbars This example uses horizontal and vertical scrollbars to control a message displayed on a panel. The horizontal scrollbar is used to move the message to the left or the right, and the vertical scrollbar to move it up and down. ScrollBarDemo Run Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 20
  • 21. JSlider JSlider is similar to JScrollBar, but JSlider has more properties and can appear in many forms. javax.swing.JComponent The get and set methods for these data fields are provided in the class, but omitted in the UML diagram for brevity. javax.swing.JSlider -maximum: int The maximum value represented by the slider (default: 100). -minimum: int The minimum value represented by the slider (default: 0). -value: int The current value represented by the slider. -orientation: int The orientation of the slider (default: JSlider.HORIZONTAL). -paintLabels: boolean True if the labels are painted at tick marks (default: false). -paintTicks: boolean True if the ticks are painted on the slider (default: false). -paintTrack: boolean True if the track is painted on the slider (default: true). -majorTickSpacing: int The number of units between major ticks (default: 0). -minorTickSpacing: int The number of units between minor ticks (default: 0). -inverted: boolean True to reverse the value-range, and false to put the value range in the normal order (default: false). +JSlider() Creates a default horizontal slider. +JSlider(min: int, max: int) Creates a horizontal slider using the specified min and max. +JSlider(min: int, max: int, value: int) Creates a horizontal slider using the specified min, max, and value. +JSlider(orientation: int) Creates a slider with the specified orientation. +JSlider(orientation: int, min: int, max: Creates a slider with the specified orientation, min, max, and value. int, value: int) Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 21
  • 22. Example: Using Sliders Rewrite the preceding program using the sliders to control a message displayed on a panel instead of using scroll bars. SliderDemo Run Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 22
  • 23. Creating Multiple Windows The following slides show step-by-step how to create an additional window from an application or applet. Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 23
  • 24. Creating Additional Windows, Step 1 Step 1: Create a subclass of JFrame (called a SubFrame) that tells the new window what to do. For example, all the GUI application programs extend JFrame and are subclasses of JFrame. Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 24
  • 25. Creating Additional Windows, Step 2 Step 2: Create an instance of SubFrame in the application or applet. Example: SubFrame subFrame = new SubFrame("SubFrame Title"); Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 25
  • 26. Creating Additional Windows, Step 3 Step 3: Create a JButton for activating the subFrame. add(new JButton("Activate SubFrame")); Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 26
  • 27. Creating Additional Windows, Step 4 Step 4: Override the actionPerformed() method as follows: public actionPerformed(ActionEvent e) { String actionCommand = e.getActionCommand(); if (e.target instanceof Button) { if ("Activate SubFrame".equals(actionCommand)) { subFrame.setVisible(true); } } } Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 27
  • 28. Example: Creating Multiple Windows  This example creates a main window with a text area in the scroll pane, and a button named "Show Histogram." When the user clicks the button, a new window appears that displays a histogram to show the occurrence of the letters in the text area. Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 28
  • 29. Example, cont. MultipleWindowsDemo Run Histogram Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 29