Question

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");

                        }

}


        );
Était-ce utile?

La 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.

Autres conseils

To change the colour of a ComboBox you should modify the ComboBoxFocusstyle from the ResourceEditor.

If you are adding the list to the ComboBox, I think that you should put the ActionListener to the ComboBox not to the List as you are doing. Try this facts.

You can work with ListCellRenderer. Its helpful tool , look here for example

You can implement getListCellRendererComponent(..)- this function return the compenents that display on screen and responsible on UI.

If you work with ListCellRenderer you can use actionLisiner like this:

mylist.setRenderer(getListCellRenderer());
    ActionListener chooseItemActionListener = new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            doAction(getSelected());
        }
    };
    mylist.addActionListener(chooseItemActionListener);
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top