Hi Armin,

What you wish to do can be achieved with a client side renderer. Renderers
take values from the model. You can make the client side renderer manipulate
and display values returned by the model. Please see the snippet below.

BTW,to be able to revalidate the model after setSelectedItem :

1. The model should inherit from AbstractListModel and implement
IComboBoxModel
2. In setSelectedItem you can call fireContentsChanged(this, -1, -1);
3. The combobox should be editable, only then a value not in the model will
be set in the selection. If the combobox is not editable and if you do
setSelectedItem with a value that is not in the model, the combobox will set
selected item to the first value in the list.

I hope this helps

Thanks and regards,

Janak

>-----Original Message-----
>From: [EMAIL PROTECTED]
>[mailto:[EMAIL PROTECTED] Behalf Of Armin Kurz
>Sent: Tuesday, January 30, 2007 10:31 AM
>To: [email protected]
>Subject: [ULC-developer] ULCComboBox: Using key/value pairs
>
>
>Hello.
>
>I try to selected a key value out of map of key/value-pairs.
>Everything works fine, but the ComboBox shows after the selection of a
>key/value pair the key/value pair and not just the selected key.
>Why?
>How can I force the ComboBox to completely revalidate the model?
>
>Here is my code:
>
>/**
>  *
>  */
>package gui;
>
>import java.util.HashMap;
>import java.util.Map;
>
>import shared.Pair;
>
>import com.ulcjava.base.application.IComboBoxModel;
>import com.ulcjava.base.application.ULCComboBox;
>import com.ulcjava.base.application.event.ActionEvent;
>import com.ulcjava.base.application.event.IActionListener;
>import com.ulcjava.base.application.event.IListDataListener;
>
>/**
>  * @author armin.kurz
>  *
>  */
>public class MyComboBox extends ULCComboBox
>{
>       private class MyComboBoxModel implements IComboBoxModel
>       {
>               private final Map m_map;
>               private Object m_selected = null;
>
>               /**
>                * @param m_map
>                */
>               public MyComboBoxModel(final Map map)
>               {
>                       this.m_map = new HashMap(map);
>               }
>
>               /* (non-Javadoc)
>                * @see
>com.ulcjava.base.application.IComboBoxModel#getSelectedItem()
>                */
>               public Object getSelectedItem()
>               {
>                       return m_selected;
>               }
>
>               /* (non-Javadoc)
>                * @see
>com.ulcjava.base.application.IComboBoxModel#setSelectedItem(java.la
>ng.Object)
>                */
>               public void setSelectedItem(Object item)
>               {
>                       Object selected = item;
>
>                       if (item instanceof Pair)
>                       {
>                               selected = ((Pair)item).getKey();
>                       }
>                       else
>                       {
>                               selected = item;
>                       }
>                       m_selected = selected;
>               }
>
>               /* (non-Javadoc)
>                * @see
>com.ulcjava.base.application.IListModel#getElementAt(int)
>                */
>               public Object getElementAt(int index)
>               {
>                       Object key = this.m_map.keySet().toArray()[index];
>                       if (null != key)
>                       {
>                               Object value = this.m_map.get(key);
>                               return new Pair(key, value);
>                       }
>                       return null;
>               }
>
>               /* (non-Javadoc)
>                * @see com.ulcjava.base.application.IListModel#getSize()
>                */
>               public int getSize()
>               {
>                       return this.m_map.size();
>               }
>
>               public void addListDataListener(IListDataListener
>listener) {
>                       // TODO Auto-generated method stub
>               }
>
>               public void
>removeListDataListener(IListDataListener listener) {
>                       // TODO Auto-generated method stub
>               }
>       }
>
>       /**
>        * @param items
>        */
>       public MyComboBox(final Map items)
>       {
>               super();
>               this.setModel(new MyComboBoxModel(items));
>
>               this.addActionListener(new IActionListener(){
>                       public void actionPerformed(ActionEvent event) {
>                        ULCComboBox source = (ULCComboBox)
>event.getSource();
>                        System.out.println("new item chosen: "
>                                + source.getSelectedItem());
>                       }
>               });
>       }
>}
>
>
>Regards
>Armin Kurz
>

-----------------------------------------


import java.awt.Component;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.swing.JList;

import com.ulcjava.base.application.AbstractApplication;
import com.ulcjava.base.application.DefaultComboBoxCellRenderer;
import com.ulcjava.base.application.IComboBoxModel;
import com.ulcjava.base.application.ULCBoxPane;
import com.ulcjava.base.application.ULCComboBox;
import com.ulcjava.base.application.ULCFrame;
import com.ulcjava.base.application.event.ActionEvent;
import com.ulcjava.base.application.event.IActionListener;
import com.ulcjava.base.application.event.IListDataListener;
import com.ulcjava.base.client.UILabel;
import
com.ulcjava.base.client.streamcoder.DefaultClientCoderRegistryProvider;
import com.ulcjava.base.development.DevelopmentRunner;
import
com.ulcjava.base.server.streamcoder.DefaultServerCoderRegistryProvider;
import com.ulcjava.base.shared.CoderRegistry;
import com.ulcjava.base.shared.IObjectInputStream;
import com.ulcjava.base.shared.IObjectOutputStream;
import com.ulcjava.base.shared.IStreamCoder;

public class ComboBoxKeyValuePairSetSelectionSnippet extends
AbstractApplication {
    public void start() {

        ULCComboBox combo = new ULCComboBox();

        HashMap items = new HashMap();
        items.put("11", "AAAA");
        items.put("22", "BBBB");
        items.put("33", "CCCC");
        items.put("44", "DDDD");
        items.put("55", "EEEE");
        items.put("66", "FFFF");
        items.put("77", "GGGG");

        combo.setModel(new MyComboBoxModel(items));
        combo.setEditable(false);
        combo.setRenderer(new MyULCComboRenderer());

        combo.addActionListener(new IActionListener() {
            public void actionPerformed(ActionEvent event) {
                ULCComboBox source = (ULCComboBox)event.getSource();
                System.out.println("new item chosen: " +
source.getSelectedItem());
            }
        });

        ULCBoxPane content = new ULCBoxPane(true);
        content.add(ULCBoxPane.BOX_EXPAND_CENTER, combo);
        content.setVisible(true);
        ULCFrame frame = new ULCFrame("Snippet");
        frame.setDefaultCloseOperation(ULCFrame.TERMINATE_ON_CLOSE);
        frame.add(content);
        frame.setSize(300, 300);
        frame.setVisible(true);

    }

    public static void main(String[] args) {
        DevelopmentRunner.setClientRegistryProviderClassName(ClientRegistryP
rovider.class.getName());
        DevelopmentRunner.setServerRegistryProviderClassName(ServerRegistryP
rovider.class.getName());
        DevelopmentRunner.setApplicationClass(ComboBoxKeyValuePairSetSelecti
onSnippet.class);
        DevelopmentRunner.run();
    }

    public static class MyULCComboRenderer extends
DefaultComboBoxCellRenderer {
        protected String typeString() {
            return MyUIComboRenderer.class.getName();
        }
    }

    public static class MyUIComboRenderer extends UILabel {
        public Component getListCellRendererComponent(JList list, Object
value, int index, boolean isSelected,
                boolean hasFocus) {
            if (!list.isShowing() && value != null) {
                value = ((Pair)value).getKey();
            }
            return super.getListCellRendererComponent(list, value, index,
isSelected, hasFocus);
        }
    }

    public static class MyComboBoxModel implements IComboBoxModel {
        private final Map m_map;
        private Object m_selected = null;


        public MyComboBoxModel(final Map map) {
            this.m_map = new HashMap(map);
        }

        public Object getSelectedItem() {
            return m_selected;
        }

        public void setSelectedItem(Object item) {
            Object selected = item;

            if (item instanceof Pair) {
                selected = ((Pair)item).getKey();
            } else {
                selected = item;
            }
            m_selected = selected;

        }

        public Object getElementAt(int index) {
            Object key = this.m_map.keySet().toArray()[index];
            if (null != key) {
                Object value = this.m_map.get(key);
                return new Pair(key, value);
            }
            return null;
        }

        public int getSize() {
            return this.m_map.size();
        }

        public void addListDataListener(IListDataListener listener) {
        }

        public void removeListDataListener(IListDataListener listener) {
        }
    }

    public static class Pair {

        private String key;
        private String value;

        public Pair(Object key, Object value) {
            super();
            this.key = (String)key;
            this.value = (String)value;
        }

        public String getKey() {
            return key;
        }

        public void setKey(String key) {
            this.key = key;
        }

        public String getValue() {
            return value;
        }

        public void setValue(String value) {
            this.value = value;
        }

        public String toString() {
            return key + " " + value;
        }
    }

    public static class ClientRegistryProvider extends
DefaultClientCoderRegistryProvider {

        protected void initializeRegistry(CoderRegistry registry) {
            super.initializeRegistry(registry);
            registry.registerCoder(Pair.class, new PairCoder());
        }
    }


    public static class ServerRegistryProvider extends
DefaultServerCoderRegistryProvider {

        protected void initializeRegistry(CoderRegistry registry) {
            super.initializeRegistry(registry);
            registry.registerCoder(Pair.class, new PairCoder());
        }
    }


    public static class PairCoder implements IStreamCoder {
        public String getPeerClassName() {
            return Pair.class.getName();
        }

        public void writeObject(IObjectOutputStream out, Object data) {
            try {
                out.writeObject(((Pair)data).getKey());
                out.writeObject(((Pair)data).getValue());
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

        public Object readObject(IObjectInputStream in) throws IOException {
            String key = (String)in.readObject();
            String value = (String)in.readObject();
            Pair p = new Pair(key, value);
            return p;
        }

    }
}

_______________________________________________
ULC-developer mailing list
[email protected]
http://lists.canoo.com/mailman/listinfo/ulc-developer

Reply via email to