Create Software Components 

Using Java Level 2

 

 

Course Info

Scheme

Resources

Tutorials

Java Demos

Utilities

Links


 

   Swing Components

Constructors and Methods

This page lists the constructors and methods of various Swing components..


JComponent Class Methods

Many of the Swing components inherit methods from JComponent or superclasses of JComponent.

Here are some methods of the JComponent class or superclasses of JComponent that are inherited by child classes such as JButton, JLabel, JRadioButton, JCheckBox, JList, etc.

~Common Methods~

  • setToolTiptext(String s)

Add a tool tip that appears when the cursor is over the component

  • setFont(Font f)  

  • Font getFont()

Set or get the font of the text on the component

  • setVisible(boolean) 

  • boolean isVisible()

Set or get whether the component is visible using true or false as the argument

  • setEnabled(boolean) 

  • boolean isEnabled()

Enable or disable the component using true or false as the argument.  

Check to see if a component is enabled or disabled.

  • setOpaque(boolean) 

  • boolean isOpaque()

Set the component so it is filled with a background color.

Check to see if a component is opaque.

  • setBackground (Color c)  

  • Color getBackground ()

Set or get the component's background color.

  • setForeground (Color c)  

  • Color getForeground () 

Set  or get the text color.

  • add (component)  

  • remove(component) 

add or remove a component from the container.

 


Swing Button Constructors & Methods

Here are the constructors for the Swing Buttons - JButton, JRadioButton and JCheckBox.

~JButton Constructors~

  • JButton()

creates a plain button without any text

  • JButton(String s)

creates a button with text

  • JButton(Icon m)

an image is shown on the button

  • JButton(String s, Icon m)

both an image and text is shown on the button

~JCheckBox Constructors~

  • JCheckBox()

Creates a checkbox with no text, no icon, unselected

  • JCheckBox(String s)

Creates a checkbox with the specified text, no icon, unselected

  • JCheckBox(String s, boolean b)

Creates a checkbox with the specified text, no icon, and selected  if b is true

  • JCheckBox(Icon m)

Creates a checkbox with no text, the specified image, unselected

  • JCheckBox(Icon m, boolean b)

Creates a checkbox with no text, the specified image, and selected  if b is true

  • JCheckBox(String s, Icon m)

Creates a checkbox with the specified text, the specified image, unselected

  • JCheckBox(String s, Icon m, boolean b)

Creates a checkbox with the specified text, the specified image, and selected  if b is true

~JRadioButton Constructors~

  • JRadioButton()

Creates a radio button with no text, no icon, unselected

  • JRadioButton(String s)

Creates a radio button with the specified text, no icon, unselected

  • JRadioButton(String s, boolean b)

Creates a radio button with the specified text, no icon, and selected if b is true

  • JRadioButton(Icon m)

Creates a radio button with no text, the specified image, unselected

  • JRadioButton(Icon m, boolean b)

Creates a radio button with no text, the specified image, and selected if b is true

  • JRadioButton(String s, Icon m)

Creates a radio button with the specified text, the specified image, unselected

  • JRadioButton(String s, Icon m, boolean b)

Creates a radio button with the specified text, the specified image, and selected if b is true

 

Many of the Swing button components inherit methods from JAbstractButton.  Here are some methods JButton, JRadioButton and JCheckBox can use - inherited from JAbstractButton

~JAbstractButton Methods~

  • setText(String s)

Set the text displayed by the button.

  • setHorizontalTextPosition(int i)

Set the horizontal position of the button's text relative to the button's image using as the argument: LEFT, CENTER, RIGHT, LEADING, and TRAILING (the default). 

  • setVerticalTextPosition(int i)

Set the vertical position of the button's text relative to the button's image using as the argument: TOP, CENTER (the default), and BOTTOM.

  • setPressedIcon(Icon m)

Set the image displayed by the button when it's being pressed.

  • setRolloverIcon(Icon m)

  • setRolloverEnabled(boolean b)

Use setRolloverIcon(Icon m) with setRolloverEnabled(true)to make the button display the specified icon when the cursor passes over it.

 


JLabel Constructors & Methods

Here are the constructors for JLabel.

~JLabel Constructors~

  • JLabel(String s)

creates a label with text displayed

  • JLabel(String s, int position)

creates a label and justifies the  text according to position.

  • JLabel(Icon m)

an image is shown on the button

  • JLabel(Icon m, int position)

creates an label showing an image and justifies the image according to position.

  • JLabel(String s, Icon m, int position)

creates a label showing text and an image, justified according to position.

 

Here are some methods of the JLabel class.

~JLabel Methods~

  • setText(String s)

  • getText()

 

Set or get the text displayed by the label.

  • setHorizontalTextPosition(int i)

Set the horizontal position of the label's text relative to the label's image using as the argument: JLabel.LEFT, JLabel.CENTER or JLabel.RIGHT

  • setVerticalTextPosition(int i)

Set the vertical position of the label's text relative to the label's image using as the argument: JLabel.TOP, JLabel.CENTER (the default), and JLabel.BOTTOM.

 


JComboBox Constructors & Methods

Here are the constructors for JComboBox.

~JComboBox Constructors~

  • JComboBox()

Creates a combo box with no items in the menu list

  • JComboBox(Object[])

Creates a combo box with items in the menu list specified by the Object array

 

Here are some methods of the JComboBox class.

~JComboBox Methods~

 

  • addItem(Object obj)

  • insertItemAt(Object obj, index i)

Add or insert the specified object into the combo box list.  The insertItemAt method inserts the object at the specified index.

  • Object getSelectedItem()

  • Object getItemAt(index i)

The getSelectedItem method gets the currently selected item from the combo box list.  To retrieve a particular item use the getItemAt method and specify the item's index number.   

  • setEnabled(boolean b)

Enable or disable the combo box using true or false as the argument.  

  • setEditable(boolean b)

Set the combo box to be editable (b is true) or uneditable (b is false). 

  • boolean isEditable()

determine if the combo box is editable.

  • int getItemCount()

Get the number of items in the combo box list. 

  • setSelectedIndex(int index)

Sets the selected item to index

  • removeAllItems()

  • removeItem(index i)

  • removeItemAt(Object obj)

Remove all items from the combo box list or remove a particular item by specifying the item's index number or remove a set of items, specified in the object array. 

 


Swing Text Components

Swing text components can be used to display information or get text based information from a user.  Two useful text components are JTextField and JTextArea.  Both of these components descend from the JTextComponent class from which they inherits many methods.  

~JTextComponent Methods~

  • String getSelectedText()

Returns the selected text contained in this TextComponent.

  • String getText()

Returns the text contained in this TextComponent.

  • setText(String s)

Sets the text in the TextComponent.

  • boolean isEditable()

Returns the boolean indicating whether this TextComponent is editable or not.

  • setEditable(boolean  b)

Set the specified boolean to indicate whether or not this TextComponent should be editable.

  • select(int selectionStart, int selectionEnd)

Selects the text between the specified start and end positions

  • selectAll()

Selects all the text in the TextComponent.

 

JTextField

Here are the constructors for JTextField.

~JTextField Constructors~

  • JTextField()

Creates a text field without any initial text in the field.

  • JTextField(String s)

Creates a text field with the initial text in the field specified by the string s.

  • JTextField(String s, int i)

Creates a text field with the initial text in the field specified by the string s.  The integer argument specifies the desired column width of the text field 

  • JTextField(int i)

Creates a text field with the desired column width specified by the integer argument.  

 

Here are some methods for JTextField.

~JTextField Methods~

  • setFont(Font f)

Sets the current font.

  • setHorizontalAlignment(int alignment)

Sets the horizontal alignment of the text.  Alignment can be set to JTextField.LEFT, JTextField.CENTER or JTextField.RIGHT

 

JTextArea

Here are the constructors for JTextArea.

~JTextArea Constructors~

  • JTextArea()

Creates a text area without any initial text or a specific size.

  • JTextArea(int rows, int columns)

Creates a text area with the specified number of rows and columns.

  • JTextArea(String s)

Creates a text area with the initial text in the area specified by the String s. 

  • JTextArea(String text, int rows, int columns)

Creates a text area with the initial text in the area specified by the String s and the specified number of rows and columns.

 

Here are some useful JTextArea methods:-  

~JTextArea Methods~

  • int getLineCount()

Returns the number of lines contained in the area.

  • insert(String s, int pos)

Inserts the specified text at the specified position.

  • setFont(Font f)

Sets the current font.

  • setLineWrap(boolean b)

Sets the line-wrapping policy of the text area.  If b is true then lines will wrap, if b is false they won't.

  • setWrapStyleWord(boolean b)

Sets the style of wrapping used if the text area is wrapping lines.  If b is true then wrapping will not occur in the middle of words, if b is false it will.

 


JList Constructors & Methods

Here are two constructors for JList.

~JList Constructors~

  • JList()

Constructs a JList with an empty model.

  • JList(Object[])

Constructs a JList that displays the elements in the specified array.

 

Here are some methods of the JList class.

~JList Methods~

  • clearSelection()

 

Clears the selection - after calling this method isSelectionEmpty will return true.

  • int getSelectedIndex()

Returns the first selected index; returns -1 if there is no selected item.

  • object getSelectedValue()

Returns the first selected value, or null if the selection is empty.

  • boolean isSelectedIndex(int index)

Returns true if the specified index is selected.

  • boolean isSelectionEmpty()

Returns true if nothing is selected.

  • setSelectedIndex(int index)

Selects a single list item.

 

 

  Site Home 

Java Home   

  Forum  

Course Info

Welcome

Overview

Assessment

Qualification

Scheme of Work

Assignments

Resources

Information

Blackboard

Learning Center

Web Materials

Reading

Java Demos

Utilities

Links

Lecture Materials

Tutorials & Notes

Exercises

Activities

Quizzes

 

Site Home

Top

Unit Home

ADR 2002