ListBox and TextViewer

The GUI component ListBox shows a list of strings and enables the user to graphically choose one entry. If the list contains more entries than it is possible to show at one time, a ScrollBar on the components' right side can be enabled. Currently selected entries are marked by inverting the correspondent line. The ListBox component fires ActionEvents of type ITEM_SELECTED. Following methods are available to create a ListBox:

MethodDescription
ListBox(String[] items, int x, int y, int width, int height, int style)Creates a new GUI component ListBox with the given position an size. The parameter items represents the initial content of the ListBox. The style parameter specifies look and behaviour. All possible values are specified by constant fields in the class ListBox STYLE_NONE, STYLE_SHOW_BORDER, STYLE_SHOW_SCROLLBAR, STYLE_ALIGN_TOP and STYLE_ALIGN_CENTER. The values may even be combined by | in cases were it makes sense.
ListBox(int x, int y, int width, int height, boolean style)This constructor works like the one above, with the difference that the Listbox is left empty.
add(String item)Adds a new entry to the ListBox.
remove(String item)Removes an entry from the ListBox.
Tabelle 5: Methods of class jcontrol.ui.wombat.ListBox

The TextViewer can display texts with an optional scroll functionality. Its content may be added statically, by using the appropriate constructor, or dynamically, by using the set method. Invoking the method add will add a string to the TextViewer. Table 6 sums up the methods of the TextViewer component:

MethodDescription
TextViewer(int x, int y, int width, int height, int style)Creates a new TextViewer with the given coordinates. width and height specify the components' size. The style parameter specifies look and behaviour. All possible values are specified by constant fields in the class TextViewer STYLE_NONE, STYLE_SHOW_BORDER, STYLE_SHOW_SCROLLBAR.
TextViewer(String text, int x, int y, int width, int height, int style)This TextViewer constructor additionally equips the component with an initial text.
set(String text)Sets the given text into the TextViewer. Any existing text will be discarded.
add(String item)Adds the given text to the TextViewer's content. If the TextViewer doesn't contain any text yet, the method is equal to set(String text).
Table 6: Methods of class jcontrol.ui.wombat.TextViewer

The following source code example includes both a ListBox object and a TextViewer, that will be filled with entries and drawn to the display. Open the project file WombatListTextviewerExample located in the directory "demos/cobra5329/WombatTutorial/WombatListTextviewerExample" (on Windows "C:\Program Files\JControl\demos\cobra5329\WombatTutorial\WombatListTextviewerExample"). Figure 4 shows a screenshot of this program.

Figure 4: The WombatListTextviewerExample

1    /**
2     * <p>This example demonstrates how to use the
3     * components ListBox and TextViewer within the GUI
4     * framework JControl/Wombat.</p>
5     *
6     * <p>(C) DOMOLOGIC Home Automation GmbH 2007</p>
7     */
8    import jcontrol.ui.wombat.Frame;
9    import jcontrol.ui.wombat.Container;
10    import jcontrol.ui.wombat.event.ActionListener;
11    import jcontrol.ui.wombat.TextViewer;
12    import jcontrol.io.Resource;
13    import java.io.IOException;
14    import jcontrol.ui.wombat.ListBox;
15    import jcontrol.ui.wombat.event.ActionEvent;
16    
17    
18    public class WombatListTextviewerExample extends Frame implements ActionListener  {
19        /**
20         * Constructor WombatListTextviewerExample
21         */
22        public WombatListTextviewerExample() {
23            // create a container for the content of this frame
24            Container content = new Container();
25            try {
26                // set a larger Font to the content, all components in the
27              // content will get this font, too
28                content.setFont(new Resource("Arial.jcfd"));
29            } catch(IOException ioe) {}    
30           
31            // create a TextViewer
32            TextViewer textViewer = new TextViewer(
33                "The TextViewer can show any text you want.\n" +
34                "Line-breaks are performed autmatically.",
35                70, 40, 180, 70, TextViewer.STYLE_SHOW_SCROLLBAR);
36            content.add(textViewer);
37           
38            // create a ListBox
39            ListBox listBox = new ListBox(new String[]{
40                "North", "Eath", "South", "West",
41                "North-Eath", "South-West", "South-Eath", "North-West"},
42                70, 110, 180, 70, ListBox.STYLE_SHOW_SCROLLBAR);
43            content.add(listBox);
44            listBox.setActionListener(this);
45           
46            // set the content to this frame
47            setContent(content);
48            // finally, make the frame visible     
49            setVisible(true);
50        }
51    
52        /**
53         * This method is called every time an item in the listbox declared
54         * above is selected be the user.
55         *
56         * @param e the ActionEvent fired by the listbox
57         */
58        public void onActionEvent(ActionEvent e) {
59            // add some code if you want to
60        }
61    
62        /**
63         * The main method.
64         *
65         * @param args
66         *        The main arguments
67         */
68        public static void main(String[] args) {
69            new WombatListTextviewerExample();
70        }
71    }
Listing 4: WombatListTextviewerExample.java