-1

I've created a Renderer in Resource Editor with three labels(Icon,Destination,lblDate).

And used it in the list with Listmodel event in StateMachine.java

cmp.setModel(new DefaultListModel(payments));
         cmp.setRenderer(new  DefaultListCellRenderer(false) {
        public Component getCellRendererComponent(Component list, Object model, Object value, int index, boolean isSelected) {
               if(value instanceof Payment) { 
                   Payment r = (Payment)value;
                 super.getCellRendererComponent(list, model, r.getPhoneNumber(), index, isSelected);                     
                 setText( r.toString());
                return this;
               }
               return super.getCellRendererComponent(list, model, value, index, isSelected);
        }     
    });

In this code I've could only change the Destination label of renderer

setText( r.toString());

but how to use other labels for example lblDate? How to set them values?

AzizD
  • 103
  • 10

2 Answers2

0

You aren't using the GUI builder for the renderer, you can see a detailed video on how to use that in the Codename One Blog. You are using a DefaultListCellRenderer which is a label so it can only contain a String and an Image, you need to use a more elaborate base and implement the ListCellRenderer interface or use the standard GUI builder support with the Hashtable data model.

Shai Almog
  • 51,749
  • 5
  • 35
  • 65
  • I do use Resource Editor like in the video Codename One Blog except I want to fill the list from the code. Could you please show me any sample of using listrenderer created by Resource Editor in the code. Or I should override DefaultListCellRenderer class and not use the Resource Editor in this case? – AzizD Apr 09 '12 at 05:37
  • The moment you call setRenderer that means you AREN'T using the GUI builder for that... Filling the list is easy just like you did. Only you used Payment objects instead of Hashtable's which the list renderer in the GUI builder can't render. So you need to either: use a Hashtable for your data (and don't use the setRenderer). Or implement a Renderer properly, see the Scroll demo in the LWUITDemo. – Shai Almog Apr 12 '12 at 04:24
  • Payment is an object but payments is Vector of Payment objects. – AzizD Apr 16 '12 at 09:29
  • I said vector of Hashtables! If you aren't using that you should build a renderer like its done in the LWUIT demo. Did you look at the LWUIT demo source code for the Scroll demo? – Shai Almog Apr 19 '12 at 17:53
  • can I create a Hashtable and bind it to the renderer in the code not in GUI? Main idea is to fill the list(with renderer that created in GUI) inside the code. To use the complex renderer that designed in GUI. – AzizD Apr 27 '12 at 03:59
  • 1
    That's the whole idea. The hashtable keys map directly to the names of the elements within the renderer. With a class of your own you would have to implement the logic yourself. – Shai Almog Apr 29 '12 at 12:08
  • but I've got another problem. When I change the item of the vector the list stays unchanged. How do I refresh the list items? – AzizD May 01 '12 at 10:39
-1

I've just came up to the answer of Shai Almog. Thanks. The answer is to use Vector of Hashtable that structured like renderer in GUI.

GUI:

enter image description here

sample code:

  Vector PaymentsHashVec = new Vector() ;
   for(int iter = 0 ; iter < getPayments().size() ; iter++) {
      Hashtable hsh = new Hashtable();
      Payment p = (Payment)getPayments().elementAt(iter);
      hsh.put("phn", p.getPhoneNumber()) ;
      hsh.put("ant",p.getOriginalAmount()+"/ " + p.getAmount()) ;
      hsh.put("btm",p.getDateSubmitted()) ;
      PaymentsHashVec.addElement(hsh);                                  
   }
 cmp.setModel(new DefaultListModel(PaymentsHashVec));
AzizD
  • 103
  • 10