Java Programming 1

skatechildrenSoftware and s/w Development

Nov 3, 2013 (3 years and 7 months ago)

75 views

1

Java Programming 1

More On GUI


Lesson 5

2

Outline


More Swing GUI Components


JTextArea


JScrollPane


Introduction to Keyboard Input (without
GUI)


DecimalFormat


NumberFormat


Summary

3

Swing:
JTextArea


JTextArea
is a GUI component that is capable of displaying
many lines of text.


The statement shown here is used to construct a
JTextArea

object:


new JTextArea(string, rowNum, colNum)

;


These 3 arguments are optional


strin
g

specify the initial words to display in the
JTextArea
,


rowNum

and
colNum

specify the size of rows and columns of the
JTextArea
.

rowNum

colNum

Text/string

4

Swing: JTextArea


Methods


setText( string )
--

set the words
(string) in the
JTextArea


append( string )
--

append to the
existing text


5

How to use JTextArea

6

JTextAreaExample Class: Output

Set the
rowNum to 2
and

colNum to 2

at Line 15

Set the rowNum to 10
and

colNum to 10

at Line 15

7

Explanation: JTextAreaExample


Line 15 :

creates
JTextArea

object,
theTextArea
.


Display

null

, 10 rows
and

10 columns
respectively


Line 17
-
20 :



uses
append

method to append the contents into the
theTextArea

object


Line 22 :

display the content in a
JOptionPane

using
the
showMessageDialog

method.


JOptionPane.showMessageDialog(null, theTextArea,



“List of Songs",JOptionPane.INFORMATION_MESSAGE);

8

Swing:
JScrollPane


Note that:


The
JTextArea

does not

provide scrollbars, even if you set
the
rowNum

and
colNum

less than

the text inside the
JTextArea
.


To have the scrollbars, we need to use the
JScrollPane

object


By default,
JScrollPane

display the scrollbars only if they are
required.


Programmer can set the
horizontal

and
vertical

scrollbar for
the
JScrollPane
.


set
Vertical
ScrollBarPolicy(
policy

)


set
Horizonta
lScrollBarPolicy(
policy

)

9

JScrollPane

Scrollbar Policies


May use the methods shown here to change
the scrollbars policies at any time. The class
JScrollPane declares the constants:


The scrollbar should always appear:


JScrollPane.VERTICAL_SCROLLBAR_ALWAYS


JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS


The scrollbar should appear only if necessary
(the defaults) and constants:


JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED


JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED


The scrollbar should never appear:


JScrollPane.VERTICAL_SCROLLBAR_NEVER


JScrollPane.HORIZONTAL_SCROLLBAR_NEVER

10

How to use
JScrollPane

11

JScrollPaneExample class: Output

Vertical Scrollbar

The vertical scrollbar appear
when the JTextArea needs to
accommodate more text

12

Horizontal Scrollbar Example

The horizontal scrollbar
appear when the JTextArea
needs to accommodate more
text


theScrollbar.setHorizontalScrollBarPolicy


(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

13

Summary


Introduced two more Java Swing GUI
components


JTextArea


JScrollPane


Read in text from the command
prompt window


Convert the input string to integer


The way to use DecimalFormat and
NumberFormat