Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java Swing Controls. JButton One of the most commonly used swing component is Push Button or simply button which is created through JButton class of Swing.

Similar presentations


Presentation on theme: "Java Swing Controls. JButton One of the most commonly used swing component is Push Button or simply button which is created through JButton class of Swing."— Presentation transcript:

1 Java Swing Controls

2 JButton One of the most commonly used swing component is Push Button or simply button which is created through JButton class of Swing API. Buttons are action components ie, these can be programmed for an action. When user clicks on a button, during runtime, corresponding action takes place.

3 COMMONLY USED COMPONENT METHODS MethodDescription String getText()Returns the text displayed by the button void setText(String s)Sets the text displayed by the button to String value specified in parenthesis. boolean isSelected()Returns True if the button is selected. Makes sense only for buttons that have on /off state, such as check boxes. void setSelected(boolean)Sets the button to appear as selected. Makes sense only for buttons that have on /off state, such as check boxes. void setIcon(Icon)Sets the Image displayed by the button when the button isn’t selected or pressed.

4 JButton Events The most common event of Push button is Action Event, The most common event of Push button is Action Event, which is handled by actionPerformed() method of event listener ActionListener.

5 CHECK BOXES A check box is a control with a rectangular area that can be checked or unchecked. A check box indicates whether a particular condition is On or Off. You can use check boxes in an application to give users True / False or Yes/ No options. As check boxes work independently of each other, a user can select any number of check boxes at the same time.

6 COMMONLY USED COMPONENT METHODS MethodDescription String getText()Returns the text displayed by the check box boolean isEnabled()Returns True if the check box is enabled. boolean isSelected()Returns True if the button is selected boolean isVisible()Returns True if the button is visible. void setEnabled(boolean b)Enables the button if True is passed otherwise disables the check box. void setVisible(boolean b)Makes the button Visible if True is passed otherwise hides the check box. void setText(String s)Sets the text displayed by the check box to String value specified in parenthesis.

7 JCheckbox Events The most commonly responded event is Item event which gets fired whenever the check box is selected or unselected. An Item event is handled through event listener Item Listener whose interface has method itemStateChanged() that handles the Item event.

8 JRADIO BUTTON Radio buttons present a set of two or more choices to the user. Unlike check boxes, radio buttons should always work as part of a group. They provide mutually exclusive options. ie, one out of a group gets selected. To make the radio buttons mutually exclusive, the radio buttons should be made part of a button group. A button group allows only one of its components to be selected.

9 COMMONLY USED COMPONENT METHODS MethodDescription String getText()Returns the text displayed by the radio button boolean isEnabled()Returns True if the radio button is enabled. boolean isSelected()Returns True if the checkbox is selected boolean isVisible()Returns True if the checkbox is visible. void setEnabled(boolean b)Enables the checkbox if True is passed otherwise disables the radio button. void setVisible(boolean b)Makes the checkbox Visible if True is passed otherwise hides the radio button. void setText(String s)Sets the text displayed by the radio button to String value specified in parenthesis.

10 JRadio Button Events The most commonly responded event is Item event which gets fired whenever the radio button is selected or unselected. An Item event is handled through event listener Item Listener whose interface has method itemStateChanged() that handles the Item event

11 JLIST BOX A list is a box shaped control containing a list of values from which single or multiple selections can be made. In addition a list control allows you to display a list of text-items or graphics. Selection model of a list By default, a list selection model allows any combination of items to be selected at a time. You can specify a different selection mode by setting the selectionMode property of the list. It can be set to any one of the following modes: SINGLE -User can select a single item SINGLE_INTERVAL -User can select single range of items (shift+select) MULTIPLE_INTERVAL -User can select multiple ranges of items within a list. (ctrl+select) The model property of a list is an associated object that stores the data displayed by the list.

12 COMMONLY USED COMPONENT METHODS void addElement(object entry)Adds an item to the item list, in the end of the list box void insertElementAt(object entry,int position)Adds an entry at the position specified int size()Returns the number of entries in the list model int getSelected Index()Returns the index of the selected Item. object getSelected Value()Returns the selected value when only a single item is selected. Boolean isEmpty()Returns true if the list is empty, false otherwise boolean isEditable()Indicates whether the combo box’s text field is editable or not void removeAllItems()Removes all items from the combo box boolean removeElement(object entry)Removes the specified item, returns true if successful. void removeElementAt(int index)Removes the item at the specified index void setEditable(boolean value)Sets the combo box’s text field editable. void setMaximumRowCount(int count)Sets the number of rows displayed when the combo box list is dropped down void setSelectedIndex(int index)Selects the item at specified index void setSelectedItem(object item)Selects the specified item

13 JCOMBO BOX A combo box appears as a text field along with a drop down list arrow from which the user can choose a value. It is a cross between the text field and a list. By default the text field of a combo box is uneditable, but you can change it to be editable by setting its editable property to true. ie, checked. If the text field portion of the control is editable, the user can enter a value into the field or edit a value retrieved from the drop down list. You can define the elements by clicking the model property and then typing the elements in the model editor dialog.

14 COMMONLY USED COMPONENT METHODS void addItem(object anobject)Adds an item to the item list, in the end of the combo box Object getItemAt(int index)Returns the item at the specified index. int getItemCount()Returns the number of items in the combo box int getSelected Index()Returns the index of the selected Item. object getSelected Item()Returns the selected item void insertItemAt(object Item, int index)Inserts an item at the specified index boolean isEditable()Indicates whether the combo box’s text field is editable or not void removeAllItems()Removes all items from the combo box void removeItem(object item)Removes the specified item void removeItemAt(int index)Removes the item at the specified index void setEditable(boolean value)Sets the combo box’s text field editable. void setMaximumRowCount(int count)Sets the number of rows displayed when the combo box list is dropped down void setSelectedIndex(int index)Selects the item at specified index void setSelectedItem(object item)Selects the specified item

15 JCombo Box Events The most commonly used event is Action event when the user selects item. To write action event handler you need to write code in combobox’s Action performed() method


Download ppt "Java Swing Controls. JButton One of the most commonly used swing component is Push Button or simply button which is created through JButton class of Swing."

Similar presentations


Ads by Google