Copyright © 1999-2011 Curt Hill More Widgets In Abstract Window Toolbox.

40
Copyright © 1999-2011 Curt Hill More Widgets In Abstract Window Toolbox

description

Copyright © Curt Hill TextArea or JTextArea A multi-line edit box May have scrollbars May specify the visible number of rows and columns Methods and events similar to TextField

Transcript of Copyright © 1999-2011 Curt Hill More Widgets In Abstract Window Toolbox.

Page 1: Copyright © 1999-2011 Curt Hill More Widgets In Abstract Window Toolbox.

Copyright © 1999-2011 Curt Hill

More Widgets

In Abstract Window Toolbox

Page 2: Copyright © 1999-2011 Curt Hill More Widgets In Abstract Window Toolbox.

Copyright © 1999-2011 Curt Hill

AWT Widgets• Widget can be:

– Button– Label– Text Field– Text Area– Check boxes– Radio buttons– Lists– Scroll Bars

Page 3: Copyright © 1999-2011 Curt Hill More Widgets In Abstract Window Toolbox.

Copyright © 1999-2011 Curt Hill

TextArea or JTextArea• A multi-line edit box• May have scrollbars• May specify the visible number of

rows and columns• Methods and events similar to

TextField

Page 4: Copyright © 1999-2011 Curt Hill More Widgets In Abstract Window Toolbox.

Copyright © 1999-2011 Curt Hill

TextArea Constructors• TextArea()• TextArea(String) // initial contents• TextArea(String,int,int) // plus size• TextArea(int,int)• TextArea(String,int,int,int) //

scrollbars

Page 5: Copyright © 1999-2011 Curt Hill More Widgets In Abstract Window Toolbox.

TextArea Manipulation• Main methods are:• setText(String)

– Sets entire contents • getText()

– Returns a string • append(String)

– Adds to the end of contents– No newline is appended

Copyright © 1999-2011 Curt Hill

Page 6: Copyright © 1999-2011 Curt Hill More Widgets In Abstract Window Toolbox.

Escape Sequence• Since append will not

automatically send contents to a new line, this must be done with an escape sequence

• An escape sequence is a means to display characters not normally displayable

• Two character representation for a single actual character

• Prefixed with a backslashCopyright © 1999-2011 Curt Hill

Page 7: Copyright © 1999-2011 Curt Hill More Widgets In Abstract Window Toolbox.

Escape Characters• \n – newline• \t – tab• \” – double quote• \’ – apostrophe • \\ - the slash itself

Copyright © 1999-2011 Curt Hill

Page 8: Copyright © 1999-2011 Curt Hill More Widgets In Abstract Window Toolbox.

Copyright © 1999-2011 Curt Hill

Check Boxes• Class is Checkbox or JCheckBox• Three constructors:

– Checkbox()– Checkbox(String) - (the label of the check box)– Checkbox(String, boolean) - (label and initial

value)• Checkbox:

– getState returns the value– setLabel changes the label

• JCheckBox: – isSelected returns the value– setText changes the label

Page 9: Copyright © 1999-2011 Curt Hill More Widgets In Abstract Window Toolbox.

Copyright © 1999-2011 Curt Hill

Check Box Listener• ItemListener• itemStateChanged(ItemEvent) is

the function• Called whenever state changes• Both Checkbox or JCheckBox

Page 10: Copyright © 1999-2011 Curt Hill More Widgets In Abstract Window Toolbox.

Copyright © 1999-2011 Curt Hill

Radio Buttons• AWT did not have one• A radio button is any check box

that is part of a CheckboxGroup • Same action listener

– Although changing one clears another, need one for each button

• Same methods• Different constructors

Page 11: Copyright © 1999-2011 Curt Hill More Widgets In Abstract Window Toolbox.

Copyright © 1999-2011 Curt Hill

Constructors• Three parameters in two orders:

– String, boolean, CheckboxGroup– String, CheckboxGroup, boolean– String is label– boolean is initial value– CheckboxGroup is the grouping box

Page 12: Copyright © 1999-2011 Curt Hill More Widgets In Abstract Window Toolbox.

Copyright © 1999-2011 Curt Hill

CheckboxGroup• Similar to a predefined event

handler• Only one check box can be on in

any CheckboxGroup• Not a GUI, Widget or descendent of

Component– No physical or display representation– The radio buttons do not have to be

near each other

Page 13: Copyright © 1999-2011 Curt Hill More Widgets In Abstract Window Toolbox.

Copyright © 1999-2011 Curt Hill

Checkboxes• Two additional methods:• Allows dynamic adding / changing

of CheckboxGroups• setCheckboxGroup(CheckboxGroup

)– If null is passed it is no longer part of

a check box– There can be initial problems

• getCheckboxGroup()

Page 14: Copyright © 1999-2011 Curt Hill More Widgets In Abstract Window Toolbox.

Copyright © 1999-2011 Curt Hill

JRadioButton• Similar to check box in Check box

group– However, it is its own class

• ButtonGroup performs same function of CheckboxGroups

• They have a property that selects the ButtonGroup– ButtonGroup has an add method also

• isSelected gets the value

Page 15: Copyright © 1999-2011 Curt Hill More Widgets In Abstract Window Toolbox.

Radio Button Problems• Radio buttons are nice for small

number of choices• Typically three to five• Larger than that takes up too much

space• For larger use a List or JList

Copyright © 1999-2011 Curt Hill

Page 16: Copyright © 1999-2011 Curt Hill More Widgets In Abstract Window Toolbox.

Copyright © 1999-2011 Curt Hill

Lists and JLists• A list is a box displaying multiple

items• Three modes:

– Single item can be selected– Single interval– Multiple intervals

• Clicking on an item selects it• Similar to Radio buttons but

scrollable

Page 17: Copyright © 1999-2011 Curt Hill More Widgets In Abstract Window Toolbox.

Copyright © 1999-2011 Curt Hill

List Constructor• List()• List(int rows)

– Single mode• List(int rows, boolean multiple)

Page 18: Copyright © 1999-2011 Curt Hill More Widgets In Abstract Window Toolbox.

Copyright © 1999-2011 Curt Hill

Adding and Removing• add(String)

– Adds the string to list box• add(String, int index)

– Adds the string to list box at position indicated by index

– index is zero based• remove(String)

– removes first occurrence of String• remove(int index)

– removes positionally

Page 19: Copyright © 1999-2011 Curt Hill More Widgets In Abstract Window Toolbox.

Copyright © 1999-2011 Curt Hill

What is Selected?• int getSelectedIndex()

– returns -1 if zero or more than one item is selected

• int[] getSelectedIndexes()– returns a potentially zero length array

of selected items• String[] getSelectedItems()

– returns an array of selected strings• String getItem(int index)

gets any string

Page 20: Copyright © 1999-2011 Curt Hill More Widgets In Abstract Window Toolbox.

Copyright © 1999-2011 Curt Hill

Other methods• select(int index)

– highlights a selection• deselect(int index)• getItemCount()• getVisibleIndex()

– Last item visible• isIndexSelected(int index)

Page 21: Copyright © 1999-2011 Curt Hill More Widgets In Abstract Window Toolbox.

Copyright © 1999-2011 Curt Hill

List Listeners• ActionListener

– Triggered by double clicks• ItemListener

– When something is selected or deselected

– Cursor motion or click or double click

Page 22: Copyright © 1999-2011 Curt Hill More Widgets In Abstract Window Toolbox.

Copyright © 1999-2011 Curt Hill

JLists• Javax version of Lists• Constructors:

– Default– Array of Object or Vector

• Also uses getSelectedIndex(es) but not the getSelectedItems, instead getSelectedValue(s)

Page 23: Copyright © 1999-2011 Curt Hill More Widgets In Abstract Window Toolbox.

Copyright © 1999-2011 Curt Hill

Drop Down Lists• Class name Choice• Condense a list into one line with

an activation button• Current value is all that is visible• No multi-selection is possible• Only constructor is default

Page 24: Copyright © 1999-2011 Curt Hill More Widgets In Abstract Window Toolbox.

Copyright © 1999-2011 Curt Hill

Choice Methods• Most of same methods as List

– add– getSelectedIndex

• The multiples returns of List are not allowed– getSelectedIndexes

• Uses same listeners

Page 25: Copyright © 1999-2011 Curt Hill More Widgets In Abstract Window Toolbox.

Copyright © 1999-2011 Curt Hill

Scroll Bar• Class name Scrollbar or JScrollBar• Come automatically with some

widgets, such as a list too large to fit• They can be separate as well• They allow a numerical value to be

entered graphically• Horizontal or Vertical

Page 26: Copyright © 1999-2011 Curt Hill More Widgets In Abstract Window Toolbox.

Copyright © 1999-2011 Curt Hill

Scroll Bar Constructors• Default• One parameter (HORIZONTAL or

VERTICAL)• Five parameter

– Orientation– Initial position– Indicator size – Minimum– Maximum

Page 27: Copyright © 1999-2011 Curt Hill More Widgets In Abstract Window Toolbox.

Copyright © 1999-2011 Curt Hill

Scroll Bar Methods• getValue

– returns the integer value• setValue(int)• All the other values can be set or

get

Page 28: Copyright © 1999-2011 Curt Hill More Widgets In Abstract Window Toolbox.

Copyright © 1999-2011 Curt Hill

Scroll Bar Listeners• AdjustmentListener is the class• adjustmentValueChanged(Adjustm

entEvent) is the method

Page 29: Copyright © 1999-2011 Curt Hill More Widgets In Abstract Window Toolbox.

Copyright © 1999-2011 Curt Hill

JScrollPane• A container into which another

component is placed• Does not need the listeners like

ScrollBars• Has a margin and a viewport• The viewport is itself a container

Page 30: Copyright © 1999-2011 Curt Hill More Widgets In Abstract Window Toolbox.

Copyright © 1999-2011 Curt Hill

JViewPort• A JViewPort may only hold one item• No real need for layout manager

– Must have one because it is a container

• The one item may be another container such as a panel

Page 31: Copyright © 1999-2011 Curt Hill More Widgets In Abstract Window Toolbox.

Copyright © 1999-2011 Curt Hill

JScrollPane again• Method getViewport returns the

Viewport• Use the viewport to add the

component: x.getViewport.add(comp)– Where x is the JScrollPane and comp

is the component to add

Page 32: Copyright © 1999-2011 Curt Hill More Widgets In Abstract Window Toolbox.

Copyright © 1999-2011 Curt Hill

JScrollPane scrollbars• There are two:

– horizontalScrollbar– verticalScrollbar

• Each of these have a policy which may be:– x_SCROLLBAR_AS_NEEDED– x_SCROLLBAR_NEVER– x_SCROLLBAR_ALWAYS

• The x may be:– HORIZONTAL– VERTICAL

• Set by setHorizontalScrollBarPolicy or setVerticalScrollBarPolicy

Page 33: Copyright © 1999-2011 Curt Hill More Widgets In Abstract Window Toolbox.

Copyright © 1999-2011 Curt Hill

Text• There are several objects that are used

for text• JTextField

– A control for entry of plain text• JPasswordField

– Single line masked text similar to JTextField• JTextArea

– Multiline, but still plain text• JEditorPane

– May edit plain text, HTML, RTF• JTextPane

– Derivation of JEditorPane

Page 34: Copyright © 1999-2011 Curt Hill More Widgets In Abstract Window Toolbox.

Copyright © 1999-2011 Curt Hill

JEditorPane• Formatting and text

– Allows display of text with the usual formatting

• Each paragraph has formatting characteristics

• Each paragraph is composed of sequences of formatted text

• This requires more set up to make a real editor

Page 35: Copyright © 1999-2011 Curt Hill More Widgets In Abstract Window Toolbox.

Copyright © 1999-2011 Curt Hill

Related Objects• There are several objects that are

used in formatted text objects• Style• StyledDocument• EditorKit

Page 36: Copyright © 1999-2011 Curt Hill More Widgets In Abstract Window Toolbox.

Copyright © 1999-2011 Curt Hill

Style• An interface

– Derived from Attribute which contains name value pairs

• A collection of attributes to associate with an element in a formatted text object

• The implemented methods include:– addChangeListener– getChangeListener– getName

Page 37: Copyright © 1999-2011 Curt Hill More Widgets In Abstract Window Toolbox.

Copyright © 1999-2011 Curt Hill

Styled Document• The parameter of one of the

JTextPane constructors• Describes formatting:

– Fonts– Colors– CharacterAttributes– ParagraphAttributes

Page 38: Copyright © 1999-2011 Curt Hill More Widgets In Abstract Window Toolbox.

Copyright © 1999-2011 Curt Hill

EditorKit• Establishes the tools needed for an

editor• Factory for the policies of an editor• Actions

– Sets the Caret– Reads and writes files into the pane

• Use getEditorKit to initialize

Page 39: Copyright © 1999-2011 Curt Hill More Widgets In Abstract Window Toolbox.

Copyright © 1999-2011 Curt Hill

JTextPane• Derived from JEditorPane• Two constructors

– Default– StyledDocument

Page 40: Copyright © 1999-2011 Curt Hill More Widgets In Abstract Window Toolbox.

Conclusion• Much more here that you should

need for any assignment• Using these widgets will allow

flexible GUIs

Copyright © 1999-2011 Curt Hill