Copyright © Curt Hill More Widgets In Abstract Window Toolbox
Copyright © Curt Hill AWT Widgets Widget can be: –Button –Label –Text Field –Text Area –Check boxes –Radio buttons –Lists –Scroll Bars
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
Copyright © Curt Hill TextArea Constructors TextArea() TextArea(String) // initial contents TextArea(String,int,int) // plus size TextArea(int,int) TextArea(String,int,int,int) // scrollbars
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 © Curt Hill
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 backslash Copyright © Curt Hill
Escape Characters \n – newline \t – tab \” – double quote \’ – apostrophe \\ - the slash itself Copyright © 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
Copyright © Curt Hill Check Box Listener ItemListener itemStateChanged(ItemEvent) is the function Called whenever state changes Both Checkbox or JCheckBox
Copyright © 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
Copyright © 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
Copyright © 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
Copyright © 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()
Copyright © 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
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 © 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
Copyright © Curt Hill List Constructor List() List(int rows) –Single mode List(int rows, boolean multiple)
Copyright © 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
Copyright © 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
Copyright © Curt Hill Other methods select(int index) –highlights a selection deselect(int index) getItemCount() getVisibleIndex() –Last item visible isIndexSelected(int index)
Copyright © Curt Hill List Listeners ActionListener –Triggered by double clicks ItemListener –When something is selected or deselected –Cursor motion or click or double click
Copyright © 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)
Copyright © 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
Copyright © Curt Hill Choice Methods Most of same methods as List –add –getSelectedIndex The multiples returns of List are not allowed –getSelectedIndexes Uses same listeners
Copyright © 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
Copyright © Curt Hill Scroll Bar Constructors Default One parameter (HORIZONTAL or VERTICAL) Five parameter –Orientation –Initial position –Indicator size –Minimum –Maximum
Copyright © Curt Hill Scroll Bar Methods getValue –returns the integer value setValue(int) All the other values can be set or get
Copyright © Curt Hill Scroll Bar Listeners AdjustmentListener is the class adjustmentValueChanged(Adjustme ntEvent) is the method
Copyright © 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
Copyright © 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
Copyright © 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
Copyright © 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
Copyright © 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
Copyright © 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
Copyright © Curt Hill Related Objects There are several objects that are used in formatted text objects Style StyledDocument EditorKit
Copyright © 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
Copyright © Curt Hill Styled Document The parameter of one of the JTextPane constructors Describes formatting: –Fonts –Colors –CharacterAttributes –ParagraphAttributes
Copyright © 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
Copyright © Curt Hill JTextPane Derived from JEditorPane Two constructors –Default –StyledDocument
Conclusion Much more here that you should need for any assignment Using these widgets will allow flexible GUIs Copyright © Curt Hill