Package totalcross.ui

Class ListContainer

  • All Implemented Interfaces:
    Scrollable

    public class ListContainer
    extends ScrollContainer
    ListContainer is a ListBox where each item is a Container.

    The correct way to create a ListContainer item is by subclassing a Container and adding the controls in the initUI method. Adding directly using getContainer(i).add will not work. Below is an example of how to use it, taken from the UIGadgets sample.

       class LCItem extends ScrollContainer
       {
          Label lDate,lPrice,lDesc;
          Check chPaid;
    
          public LCItem()
          {
             super(false); // VERY IMPORTANT (a RuntimeException will be thrown if this is not used).
          }
    
          public void initUI()
          {
             add(chPaid = new Check("Paid"),LEFT,TOP);
             add(lDate = new Label("99/99/9999"),RIGHT,TOP); 
             add(new Label("US$"),LEFT,AFTER);
             add(lPrice = new Label("999.999.99"),AFTER,SAME);
             add(lDesc = new Label("",RIGHT),AFTER+10,SAME);
             lDesc.setText("description");
          }
       }
    
       private void testListContainer()
       {
          ListContainer lc;
          add(lc = new ListContainer(),LEFT,TOP,FILL,FILL);
          for (int i =0; i < 10; i++)
             lc.addContainer(new LCItem());
       }
      
    When an item is selected, a PRESSED event is dispatched. Check the ListContainerSample in the sdk for a bunch of ideas of what can be done with this component. The ListContainer supports navigation using the keys up/down, page-up/down, and enter. The left and right keys acts like clicking in the left or right buttons (if any).
    Since:
    TotalCross 1.14
    • Field Detail

      • lastSelBack

        protected int lastSelBack
      • lastSelIndex

        protected int lastSelIndex
      • highlightColor

        public int highlightColor
        Color used to highlight a container. Based on the background color.
      • drawHLine

        public boolean drawHLine
        If true (default), draws a horizontal line between each container.
    • Constructor Detail

      • ListContainer

        public ListContainer()
    • Method Detail

      • getLayout

        public ListContainer.Layout getLayout​(int itemCount,
                                              int itemsPerLine)
        Creates a Layout object with the given parameters.
      • onColorsChanged

        public void onColorsChanged​(boolean colorsChanged)
        Description copied from class: Control
        Called after a setEnabled, setForeColor and setBackColor and when a control has been added to a Container. If colorsChanged is true, it was called from setForeColor/setBackColor/Container.add; otherwise, it was called from setEnabled
        Overrides:
        onColorsChanged in class ScrollContainer
      • size

        public int size()
        Returns the number of items of this list
      • addContainers

        public void addContainers​(Container[] all)
        Adds an array of Containers to this list. Adding hundreds of containers is hundred times faster using this method instead of adding one at a time. Consider also to increase the value of Flick.defaultLongestFlick BEFORE creating the ListContainer, otherwise the user may take forever to flick. You can set it to 3 * all.length, if all.length is above 1000.
        See Also:
        Flick.defaultLongestFlick
      • removeAllContainers

        public void removeAllContainers()
        Removes all containers of this ListContainer. Note that onRemove is not called in the containers.
      • getSelectedItem

        public Container getSelectedItem()
        Returns the selected container, or null if none is selected.
      • getSelectedIndex

        public int getSelectedIndex()
        Returns the selected index, or -1 if none is selected.
      • setSelectedIndex

        public void setSelectedIndex​(int idx)
        Sets the selected container based on its index.
        Parameters:
        idx - The index or -1 to unselect all containers.
      • setSelectedItem

        public void setSelectedItem​(Container c)
        Sets the selected container.
      • getContainer

        public Container getContainer​(int idx)
        Returns the given container number or null if its invalid.
      • setBackColor

        public void setBackColor​(Container c,
                                 int back)
        Changes the color of all controls inside the given container that matches the background color of this ListContainer.
      • scrollToControl

        public void scrollToControl​(Control c)
        Positions the given Container (that should be a control added to this ListContainer) at the top of the list.
        Overrides:
        scrollToControl in class ScrollContainer