Class DBListBox

  • All Implemented Interfaces:
    Scrollable

    public class DBListBox
    extends ListBox
    DBListBox is a simple implementation of a Listbox. You can use the up/down keys to scroll and enter the first letter of an item to select it.

    Note: the color used in the setBackground method will be used in the scrollbar only. The background color of the control will be a lighter version of the given color.

    Note: this is a special version. It accepts a String matrix as the input, and does not allow add/remove/set of elements, since it reflects the database and must be filled again after some row is updated.

    Here is an example showing how it can be used:

    
     public class MyProgram extends totalcross.ui.MainWindow
     {
     DBListBox lb;
    
     public void initUI()
     {
       ResultSet rs = driver.executeQuery("select rowid,name from person where name > 20");
       rs.first();
       lb = new DBListBox(rs.getStrings(-1,true,false),1); // display column 1
       add(lb,LEFT,TOP);
     }
    
     public void onEvent(totalcross.ui.event.Event event)
     {
        switch (event.type)
        {
           case totalcross.ui.event.ControlEvent.PRESSED:
              if (event.target == lb)
                 String[] element = (String[])lb.getSelectedItem(); // in most cases, this is just a String and may be casted to such
        }
     }
     }
     
    The first item has index 0.
    • Field Detail

      • sitems

        protected java.lang.String[][] sitems
        The String items
    • Constructor Detail

      • DBListBox

        public DBListBox()
        Creates an empty Listbox.
      • DBListBox

        public DBListBox​(java.lang.String[][] items,
                         int displayCol)
        Creates a Listbox with the given items.
    • Method Detail

      • setDisplayCol

        public void setDisplayCol​(int displayCol)
        Sets the column of the String matrix that will be used to display the elements.
      • getColumnCount

        public int getColumnCount()
        Returns the number of columns, if there are items.
      • add

        public void add​(java.lang.Object[] items)
        REPLACES the current items with the given ones.
        Overrides:
        add in class ListBox
      • add

        public void add​(java.lang.Object item)
        REPLACES the current items with the given ones.
        Overrides:
        add in class ListBox
      • insert

        public void insert​(java.lang.Object item,
                           int index)
        Does nothing
        Overrides:
        insert in class ListBox
      • removeAll

        public void removeAll()
        Empties this ListBox, setting all elements of the array to null so they can be garbage collected. Attention! If you used the same object array to initialize two ListBoxes (or ComboBoxes), this method will null both ListBoxes ('cos they use the same array reference), and you'll get a null pointer exception!
        Overrides:
        removeAll in class ListBox
      • remove

        public void remove​(int itemIndex)
        Does nothing
        Overrides:
        remove in class ListBox
      • remove

        public void remove​(java.lang.Object item)
        Does nothing
        Overrides:
        remove in class ListBox
      • setItemAt

        public void setItemAt​(int i,
                              java.lang.Object s)
        Does nothing
        Overrides:
        setItemAt in class ListBox
      • getItemAt

        public java.lang.Object getItemAt​(int i)
        Get the Object at the given Index. Returns a String array, or null if i is out of range.
        Overrides:
        getItemAt in class ListBox
      • getSelectedItem

        public java.lang.Object getSelectedItem()
        Returns the selected item of the Listbox or null if none is selected
        Overrides:
        getSelectedItem in class ListBox
      • getSelectedIndex

        public int getSelectedIndex()
        Returns the position of the selected item of the Listbox or -1 if the listbox has no selected index yet.
        Overrides:
        getSelectedIndex in class ListBox
      • getItems

        public java.lang.Object[] getItems()
        Returns all items in this ListBox. The array can be casted to String[][].
        Overrides:
        getItems in class ListBox
      • getItemsArray

        protected java.lang.Object[] getItemsArray()
        Description copied from class: ListBox
        Used internally
        Overrides:
        getItemsArray in class ListBox
      • indexOf

        public int indexOf​(java.lang.Object name,
                           int col)
        Returns the index of the object at the given column.
      • setSelectedIndex

        public void setSelectedIndex​(int i)
        Select an item and scroll to it if necessary. Note: select must be called only after the control has been added to the container and its rect has been set.
        Overrides:
        setSelectedIndex in class ListBox
      • size

        public int size()
        Returns the number of items
        Overrides:
        size in class ListBox
      • add

        public void add​(Control control)
        Does nothing
        Overrides:
        add in class ListBox
      • remove

        public void remove​(Control control)
        Does nothing
        Overrides:
        remove in class ListBox
      • find

        protected void find​(char c)
        Does nothing
        Overrides:
        find in class ListBox
      • drawItem

        protected void drawItem​(Graphics g,
                                int index,
                                int dx,
                                int dy)
        You can extend ListBox and overide this method to draw the items
        Overrides:
        drawItem in class ListBox
      • getItemWidth

        protected int getItemWidth​(int index)
        Returns the width of the given item index with the current fontmetrics. Note: if you overide this class you must implement this method.
        Overrides:
        getItemWidth in class ListBox
      • qsort

        public void qsort()
        Sorts the elements of this ListBox. The current selection is cleared.
        Overrides:
        qsort in class ListBox
      • getText

        public java.lang.String getText()
        Description copied from class: ListBox
        Returns the string of the selected item or "" if none is selected.
        Overrides:
        getText in class ListBox