Last time, we mastered some basic UI components and worked on how to add them to panels, organize their layout, and manage their events. Having covered all the basic techniques that we’ll need to get started with Java interface programming in our first article, Java's Basic User Interface Components, we will move on to some advanced UI components. Today, I’ll be covering scrolling lists, scrollbars, and canvases, and their functionality. It is amazing to see how many of complex tasks are simplified with the help of these advanced user interface components.
The following example is a simple applet that creates a scrolling list and populates it with items, the list is added to the container in this case Applet using the add() method. In the example below we have a list of seven dairy items with multi-select option set to true.
Scrolling lists generate actions when the user double-clicks a list item, for example a single mouse click generates a LIST_SELECT or LIST_DESELECT event ID. A scrolling list action has the argument of the string of the item that was double-clicked. The following table shows some of the methods available to scrolling lists. See the API documentation for a complete set.
Method
Action
getItem(int)
Returns the string item at the given position (items inside a list begin at 0, just like arrays)
countItems()
Returns the number of items in the menu
getSelectedIndex()
Returns the index position of the item that's selected
getSelectedIndexes()
Returns an array of index positions (used for lists that allow multiple selections)
getSelectedItem()
Returns the currently selected item as a string
getSelectedItems()
Returns an array of strings containing all the selected items
select(int)
Selects the item at the given position
select(String)
Selects the item with the given string
The following example creates a slightly complex Applet which contains the following UI components: A TextField to accept user input; a Label, to inform the user of the contents that is to be entered in the TextField; a List, which displays a number of items; a “but_add” button, which let’s the user to add text in the TextField to the List. When the applet is started, the UI components specified are added to the applet. The addItem() method is used to add the items to the list. The text entered in the TextField is added to the List on clicking the add button. The ActionListener() method enables the program to listen to the button click events. The items are added to the list inside the actionPerformed() method.
Public class ListTest1 extends Applet { List acts = new List(); TextField tx = new TextField(10); Button but_add = new Button("ADD"); String stringlist[] = {"One", "Two", "Three", “Four”};
Public void start () { add(new Label("Text")); add(tx); for(int i = 0; i < stringlist.length; ++i) acts.addItem(stringlist[i]); add(acts); but_add.addActionListener(new AddLisn()); add(but_add); } Class AddLisn implements ActionListener { public void actionPerformed(ActionEvent e) { acts.addItem(tx.getText()); } }