-
FEATURED COMPONENTS
First time here? Check out the FAQ!
combobox.setModel(new SimpleListModel(Array)).
If Array > 10 then the combobox just show 10 data.
Can everyone help me?
best regards
Hi, andij62, welcome to ZK.
You can refer to the thread, I just replied there:
Combobox question? please help me?
http://www.zkoss.org/forum/listComment/11114/
Hi andij62,
You can use this class
/** * A simple implementation of {@link ListModel}. * Note: It assumes the content is immutable. If not, use {@link ListModelList} * or {@link ListModelArray} nstead. * * @author tomyeh * @see ListModelArray * @see ListModelSet * @see ListModelList * @see ListModelMap * @see ListSubModel (since 3.0.2) */ public class myListModel implements ListModel, ListModelExt, ListSubModel, java.io.Serializable { private static final long serialVersionUID = 20060707L; private final Object[] _data; /** Constructor. * * @param data the array to represent * @param live whether to have a 'live' {@link ListModel} on top of * the specified list. * If false, the content of the specified list is copied. * If true, this object is a 'facade' of the specified list, * i.e., when you add or remove items from this ListModelList, * the inner "live" list would be changed accordingly. * * However, it is not a good idea to modify <code>data</code> * once it is passed to this method with live is true, * since {@link Listbox} is not smart enough to hanle it. * @since 2.4.1 */ public myListModel(Object[] data, boolean live) { if (data == null) throw new NullPointerException(); _data = live ? data: (Object[])ArraysX.clone(data); } /** Constructor. * It made a copy of the specified array (<code>data</code>). */ public myListModel(Object[] data) { this(data, false); } /** Constructor. * @since 2.4.1 */ public myListModel(List data) { _data = data.toArray(new Object[data.size()]); } //-- ListModel --// public int getSize() { return _data.length; } public Object getElementAt(int j) { return _data; } /** * Returns the subset of the list model data that matches * the specified value. * It is ususally used for implmentation of auto-complete. * * <p>The implementation uses {@link #objectToString} to convert * the returned object of {@link #getElementAt} to the string. * And then, an element is considered as 'matched', if the string * starts with the specified value. * * <p>Note: If the nRows is a negative number, 150 is assumed. * * @param value the value to retrieve the subset of the list model. * It is converted to a string first by use of {@link #objectToString}. * Then, it is used to check if an element starts with (aka., prefix with) * this string. * @since 3.0.2 */ public ListModel getSubModel(Object value, int nRows) { final String idx = value == null ? "" : objectToString(value); if (nRows < 0) nRows = MAX NUMBER OF ROWS; final LinkedList data = new LinkedList(); for (int i = 0; i < _data.length; i++) { if (idx.equals("") || _data<i >.toString().startsWith(idx)) { data.add(_data<i >); if (--nRows <= 0) break; //done } } return new myListModel(data); } /** * Returns the string from the value object. It is used to convert * the object type to the string type for {@link #getSubModel(Object, int)}. * * <p>The implementation uses {@link Object#toString} to convert * the value to a string (and to an empty string if null). * If you need better control, you can override this method. * * @param value the value object. * @since 3.0.2 */ protected String objectToString(Object value) { return value != null ? value.toString(): ""; } public void sort(Comparator arg0, boolean arg1) { // TODO Auto-generated method stub } public void addListDataListener(ListDataListener arg0) { // TODO Auto-generated method stub } public void removeListDataListener(ListDataListener arg0) { // TODO Auto-generated method stub }
combobox.setModel(new myListModel(Array))
hallo n4rk0,
when i use this class, i have a problem...
List myList = new ArrayList(); myList.add("A"); myList.add("B"); myList.add("C"); combobox.setModel(new myListModel(myList.toArray()));
then the elements in the combobox are
[A, B, C]
[A, B, C]
[A, B, C]
instead of
A
B
C
whats wrong?
best regards
Try setting values in a String array
String myStringArray[] = new myStringArray[3]; myStringArray[0] = "A"; myStringArray[1] = "B"; myStringArray[2] = "C"; combobox.setModel(new myListModel(myStringArray));
Sorry n4rk0, but it doesn't work!
when i use this code
String myStringArray[] = new String[3]; myStringArray[0] = "A"; myStringArray[1] = "B"; myStringArray[2] = "C"; ListModel myLM = new myListModel(myStringArray); combobox.setModel(myLM);
myLM myListModel (id=92)
_data String[3] (id=114)
[0] "1" (id=116)
[1] "2" (id=118)
[2] "3" (id=119)
but the elements in the combobox are
[A, B, C]
[A, B, C]
[A, B, C]
instead of
A
B
C
do you have any ideas???
regards
<window title="JDBC" border="normal" apply="test.Test"> <grid> <rows> <row> Name : <textbox id="name" /> </row> <row> Email: <textbox id="email" /> </row> <row> Listbox: <listbox id="lblist" mold="select" rows="1" width="150px" /> </row> <row> Combobox: <combobox id="cblist" width="150px" /> </row> </rows> </grid> <grid id="mygrid" height="100px" width="100%" /> <button id="cbNewValue" label="cbNewValue" /> </window>
public void onClick$cbNewValue() { String myStringArray[] = new String[3]; myStringArray[0] = "A"; myStringArray[1] = "B"; myStringArray[2] = "C"; cblist.setModel(new myListModel(myStringArray)); }
package test.misc; import java.util.ArrayList; import java.util.Comparator; import java.util.LinkedList; import java.util.List; import org.zkoss.util.ArraysX; import org.zkoss.zul.*; import org.zkoss.zul.event.ListDataListener; /** * A simple implementation of {@link ListModel}. * Note: It assumes the content is immutable. If not, use {@link ListModelList} * or {@link ListModelArray} nstead. * * @author tomyeh * @see ListModelArray * @see ListModelSet * @see ListModelList * @see ListModelMap * @see ListSubModel (since 3.0.2) */ public class myListModel implements ListModel, ListModelExt, ListSubModel { private final Object[] _data; /** Constructor. * * @param data the array to represent * @param live whether to have a 'live' {@link ListModel} on top of * the specified list. * If false, the content of the specified list is copied. * If true, this object is a 'facade' of the specified list, * i.e., when you add or remove items from this ListModelList, * the inner "live" list would be changed accordingly. * * However, it is not a good idea to modify <code>data</code> * once it is passed to this method with live is true, * since {@link Listbox} is not smart enough to hanle it. * @since 2.4.1 */ public myListModel(Object[] data, boolean live) { if (data == null) throw new NullPointerException(); _data = live ? data: (Object[])ArraysX.clone(data); } /** Constructor. * It made a copy of the specified array (<code>data</code>). */ public myListModel(Object[] data) { this(data, false); } /** Constructor. * @since 2.4.1 */ public myListModel(List data) { _data = data.toArray(new Object[data.size()]); } //-- ListModel --// public int getSize() { return _data.length; } public Object getElementAt(int j) { return _data; } /** * Returns the subset of the list model data that matches * the specified value. * It is ususally used for implmentation of auto-complete. * * <p>The implementation uses {@link #objectToString} to convert * the returned object of {@link #getElementAt} to the string. * And then, an element is considered as 'matched', if the string * starts with the specified value. * * <p>Note: If the nRows is a negative number, 150 is assumed. * * @param value the value to retrieve the subset of the list model. * It is converted to a string first by use of {@link #objectToString}. * Then, it is used to check if an element starts with (aka., prefix with) * this string. * @since 3.0.2 */ public ListModel getSubModel(Object value, int nRows) { final String idx = value == null ? "" : objectToString(value); if (nRows < 0) nRows = 25; final LinkedList data = new LinkedList(); for (int i = 0; i < _data.length; i++) { if (idx.equals("") || _data<i >.toString().startsWith(idx)) { data.add(_data<i >); if (--nRows <= 0) break; //done } } return new myListModel(data); } /** * Returns the string from the value object. It is used to convert * the object type to the string type for {@link #getSubModel(Object, int)}. * * <p>The implementation uses {@link Object#toString} to convert * the value to a string (and to an empty string if null). * If you need better control, you can override this method. * * @param value the value object. * @since 3.0.2 */ protected String objectToString(Object value) { return value != null ? value.toString(): ""; } public void sort(Comparator arg0, boolean arg1) { // TODO Auto-generated method stub } public void addListDataListener(ListDataListener arg0) { // TODO Auto-generated method stub } public void removeListDataListener(ListDataListener arg0) { // TODO Auto-generated method stub } }
Asked: 2010-02-02 09:19:52 +0800
Seen: 1,178 times
Last updated: Feb 11 '10