Presentation is loading. Please wait.

Presentation is loading. Please wait.

12-Jul-15 Components. 2 Types of Components Button Canvas Checkbox Choice Label List Scrollbar TextComponent TextArea TextField.

Similar presentations


Presentation on theme: "12-Jul-15 Components. 2 Types of Components Button Canvas Checkbox Choice Label List Scrollbar TextComponent TextArea TextField."— Presentation transcript:

1 12-Jul-15 Components

2 2 Types of Components Button Canvas Checkbox Choice Label List Scrollbar TextComponent TextArea TextField

3 3 Button new Button() constructs a Button with no text new Button( String ) constructs a Button with the specified text void addActionListener( ActionListener ) void removeActionListener( ActionListener ) String getLabel() void setLabel( String ) String actionEvent.getActionCommand() Returns the “action command” associated with this Button By default, the action command = the text on the Button

4 4 A Button listener button.addActionListener (new MyButtonListener ()); class MyButtonListener implements ActionListener { public void actionPerformed (ActionEvent event) { showStatus ("Button clicked."); } }

5 5 Label constructors new Label() constructs a Label with no text new Label( String ) constructs a Label with the specified text new Label( String, alignment ) constructs a label with the specified text and alignment: Label.LEFT Label.RIGHT Label.CENTER (These are int constants defined in the Label class)

6 6 Label methods String getText() void setText( String ) int getAlignment() void setAlignment( int )

7 7 Scrollbar constructors new Scrollbar() creates a vertical Scrollbar new Scrollbar(int orientation ) Scrollbar.HORIZONTAL or Scrollbar.VERTICAL new Scrollbar(int orientation, int initialValue, int bubbleSize, int minimum, int maximum ) bubbleSize: size of the bubble (sometimes called elevator) min value of Scrollbar = minimum max value of Scrollbar = maximum - bubbleSize ( value of scrollbar is given by left or top edge of bubble) initialValue: the initial position setting of the bubble

8 8 Scrollbars bubbleSize maximum maximum — bubbleSize minimum The units for bubbleSize are the same as those for minimum and maximum The scrollbar reading is taken from the left edge of the bubble

9 9 Scrollbar methods void addAdjustmentListener( AdjustmentListener ) void removeAdjustmentListener( AdjustmentListener ) int getMinimum() void setMinimum( int ) int getMaximum() void setMaximum( int ) int getValue() void setValue( int )

10 10 A Scrollbar listener scrollbar.addAdjustmentListener(new MyScrollbarListener ()); class MyScrollbarListener implements AdjustmentListener { public void adjustmentValueChanged (AdjustmentEvent event) { showStatus ("Scrollbar: " + scrollbar.getValue ()); } };

11 11 Checkbox constructors new Checkbox() new Checkbox( String ) new Checkbox( String, boolean ) new Checkbox( String, boolean, CheckboxGroup ) new Checkbox( String, CheckboxGroup, boolean )

12 12 Checkbox methods void addItemListener( ItemListener ) void removeItemListener( ItemListener ) CheckboxGroup getCheckboxGroup () void setCheckboxGroup( CheckboxGroup ) String getLabel() void setLabel( String )

13 13 A Checkbox listener checkbox.addItemListener (new MyCheckboxListener ()); class MyCheckboxListener implements ItemListener { public void itemStateChanged (ItemEvent event) { showStatus ("Checkbox status is now " + (event.getStateChange () == ItemEvent.SELECTED)); } };

14 14 CheckboxGroup methods Checkbox getSelectedCheckbox() Suggested use: myCG.getSelectedCheckbox().getLabel() void setSelectedCheckbox( Checkbox )

15 15 A CheckboxGroup listener No such thing—listen to individual Checkboxes checkbox1.addItemListener (myCheckboxGroupListener); checkbox2.addItemListener (myCheckboxGroupListener); class MyCheckboxGroupListener implements ItemListener { public void itemStateChanged (ItemEvent event) { Checkbox d = checkboxGroup.getSelectedCheckbox (); showStatus ("CheckboxGroup: " + d.getLabel ()); } }

16 16 Choice constructor new Choice() After constructing a Choice, use add( String ) to add items to it

17 17 Choice methods addItemListener( ItemListener ) removeItemListener( ItemListener ) void add ( String ) void remove( String ) void removeAll() String getSelectedItem() void select( String )

18 18 A Choice listener choice.addItemListener (new MyChoiceListener ()); class MyChoiceListener implements ItemListener { public void itemStateChanged (ItemEvent event) { showStatus ("Choice: " + choice.getSelectedItem ()); } };

19 19 List constructors new List() constructs an empty list new List( int ) int : the number of items visible (a scrollbar is automatically added if needed) new List(int, boolean ) boolean: true if multiple selection is allowed

20 20 List methods I void addItemListener( ItemListener ) void removeItemListener( ItemListener ) void add( String ) void remove( String ) void removeAll()

21 21 List methods II String getSelectedItem() String[] getSelectedItems() int getSelectedIndex() int[] getSelectedIndexes() void select(int) // can only select by index There are other methods for adding, removing, etc. by index

22 22 A List listener list.addItemListener (new MyListListener ()); class MyListListener implements ItemListener { public void itemStateChanged (ItemEvent event) { showStatus ("List: " + list.getSelectedItem ()); } };

23 23 TextComponent No constructors Instead, construct a TextField or TextArea void addTextListener( TextListener ) void removeTextListener( TextListener ) String getText() void setText( String ) boolean isEditable() void setEditable( boolean )

24 24 TextField constructors new TextField() new TextField( int ) int : the approximate desired number of columns new TextField( String ) String : the initial text in the TextField new TextField( String, int )

25 25 TextField methods void addActionListener( ActionListener ) void removeActionListener( ActionListener ) The ActionListener for a TextField responds only when the Enter key is pressed

26 26 TextArea constructors new TextArea() new TextArea(int rows, int columns ) new TextArea(int rows, int columns, int scroll ) TextArea.SCROLLBARS_VERTICAL_ONLY TextArea.SCROLLBARS_HORIZONTAL_ONLY TextArea.SCROLLBARS_BOTH TextArea.SCROLLBARS_NONE new TextArea( String ) new TextArea( String, int rows, int columns )

27 27 TextComponent listeners textField.addActionListener (new MyTextFieldListeners ()); textField.addTextListener (new MyTextFieldListeners ()); class MyTextFieldListeners implements ActionListener,TextListener { public void actionPerformed (ActionEvent event) { showStatus ("TextField (ActionListener): " + textField.getText ()); } public void textValueChanged (TextEvent event) { showStatus ("TextField (TextListener): " + textField.getText ()); } };

28 28 Suggestion: Don’t use text listeners! Text fields are intended to be passive; that is, nothing should happen immediately as a result of typing into them Consequently, I’ve seen a lot of programs in which text listeners do their work at the wrong time It usually works much better to ignore text fields until an active control (such as a Button ) is used, and then do getText() to find the current value in the text field

29 29 Canvas This is a rectangular blank area that you can paint on Constructor: new Canvas() Don't use Canvas directly; instead, subclass it and override its void paint( Graphics ) method Or even simpler: Paint directly on a Panel

30 30 The End


Download ppt "12-Jul-15 Components. 2 Types of Components Button Canvas Checkbox Choice Label List Scrollbar TextComponent TextArea TextField."

Similar presentations


Ads by Google