Java provides another set of components that are useful in developing GUI applications.
List
components objects are useful for holding a large number of data items. They
are quite similar to the Choice
component, however they can allow for the multiple selection
of items. The list automatically adds a scrollbar if it is required. The list constructor takes two
arguments, the int
number of items to be displayed and a boolean
to
allow/disallow multiple item selection.
1 2 3 List aList = new List(3, true); 4 aList.addItem("Some Item 1"); 5 aList.addItem("Some Item 2"); 6 aList.addItem("Some Item 3"); 7 aList.addItem("Some Item 4"); 8 //etc. 9 10
Clicking on an item selects it and pressing again on the same item deselects it. The selection works the same
way as Windows explorer file selection. i.e. if you hold the SHIFT
key and select with the mouse
you can select from one item in
The getSelectedItem() (or getSelectedItems() for multiple selections) method can be used to retrieve the currently selected item.
Are used for creating areas of screen input where we want to provide large amounts of text
data, either for displaying a text file, or implementing a text editor. TextArea
components do not generate events. They can be set read-only or editable by using the
setEditable()
method.
The following are the constructors for a TextArea
component
TextArea()
// empty string as text.TextArea(int rows, int columns)
// number of rows and columns specifiedTextArea(String text)
// with the specified text.TextArea(String text, int rows, int columns)
// with the specified text, and with the specified number of // rows and columns.TextArea(String text, int rows, int columns, int scrollbars)
// with the specified text, and with the rows, columns, and // scroll bar visibility as specified either asSCROLLBARS_BOTH
, //SCROLLBARS_HORIZONTAL_ONLY
,SCROLLBARS_NONE
// orSCROLLBARS_VERTICAL_ONLY
The TextArea
object can be created as follows:
TextArea t = new TextArea("Test TextArea", 30, 5, TextArea.SCROLLBARS_VERTICAL_ONLY);
Scrollbar
objects are used to develop sliders that choose values
in a certain range of data. We can use a Scrollbar
to control an input
value, scroll an image, and numerous other applications. The constructor of a
Scrollbar
takes five parameters:
The orientation of the scrollbar, HORIZONTAL or VERTICAL.
The initial value of the slider.
The thickness of the slider drag component.
The minimum value of the scrollbar.
The maximum value of the scrollbar.
Use the getValue()
method to read the current value of the
Scrollbar
object. Use the setValue()
method to
set the slider position in the scrollbar.
So an example use could be:
Scrollbar sb = new Scrollbar(Scrollbar.HORIZONTAL, 50, 10, 0, 100);
© 2006
Dr. Derek Molloy
(DCU).