1

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

                        }

}


        );
PHPFan
  • 756
  • 3
  • 12
  • 46

3 Answers3

1

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);
blay
  • 464
  • 9
  • 23
  • It seems that I will be forced to reuse that code http://lwuit.blogspot.com/2008/07/lwuit-list-renderer-by-chen-fishbein.html, but what getListCellRenderer() method ? – PHPFan Jul 27 '13 at 07:32
  • It seems that I will be forced to reuse that code http://lwuit.blogspot.com/2008/07/lwuit-list-renderer-by-chen-fishbein.html#comment-form_8868619825488006175, but what getListCellRenderer() method (from where you get it) ? – – PHPFan Jul 28 '13 at 06:56
  • 1
    No. you use in your code how you want. The object list that you work is what you put in mylist.setRenderer(getListCellRenderer()). in function you do casting to specific object and build the component with container,labels and ect' , how do you like. – blay Jul 29 '13 at 07:18
  • I have wrote this method >`public Component getListCellRendererComponent(List list,java.lang.Object value, int index, boolean isSelected) { com.sun.lwuit.ComboBox myCombo = new com.sun.lwuit.ComboBox(list.getModel()); value = list.getModel(); if (isSelected) { myCombo.setFocus(true); myCombo.getStyle().setBgTransparency(100); } else { myCombo.setFocus(false); myCombo.getStyle().setBgTransparency(0); } return myCombo; } `I am correct till now ? – PHPFan Jul 30 '13 at 11:37
  • 1
    No. its helpful to create class and wotk with him. in the constructor you intliaize your components like checkbox. you don't need to call list.getMode() because you get your value at java.lang.Object. value(you need cast to your object) . you load your list in mylist.setRenderer(getListCellRenderer()); – blay Jul 31 '13 at 07:44
1

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.

Mun0n
  • 4,438
  • 4
  • 28
  • 46
1

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.

  • @blay **only before your statements Ishould write this statement** `final com.sun.lwuit.ComboBox Combo = new com.sun.lwuit.ComboBox(mylist.getModel() );` – PHPFan Aug 05 '13 at 10:06