java-melwuitmidletlwuit-listlwuit-combobox

How to do an action when a LWUIT List item clicked


I have a LWUIT application that has a list which involving some items.

The list itself has been added to a Combobox .

1/ How I change the colour of an item of list when I focus on it?

      final com.sun.lwuit.List mylist =  new com.sun.lwuit.List();

      mylist.addItem("one");

      mylist.addItem("two");

      mylist.addItem("three");

      mylist.addItem("four");

       final com.sun.lwuit.ComboBox  combo = new  com.sun.lwuit.ComboBox (mylist.getModel());

      final com.sun.lwuit.Form ff = new com.sun.lwuit.Form();

       ff.addComponent(combo);

2/ I want to do an action when I click ( or double click ) on an item ,

ActionListener interface didn't make that for me , can someone guide me?

         mylist.addActionListener(new ActionListener()

         {

           public void actionPerformed(ActionEvent ev)

                       {

               System.out.println("java");

                        }

}


        );

Solution

  • You Should set a renderer to ComboBox and can use both of setRenderer and setListCellRenderer but setListCellRenderer is deprecated than use setRenderer:

        combo.setRenderer(new ListCellRenderer() {
            public Component getListCellRendererComponent(List list, Object value, int index, boolean isSelected) {
                Label l = new Label(String.valueOf(value));
                l.getStyle().setBgColor(0xffaa00);
                return l;
            }
    
        public Component getListFocusComponent(List list) {
                Label l = new Label(String.valueOf(list.getSelectedItem()));
                l.getStyle().setBgColor(0x00ff00);
                return l;
            }
        });
    

    this working well.