0

combobox show only 10 elements

asked 2010-02-02 09:19:52 +0800

andij62 gravatar image andij62
301 1 7

combobox.setModel(new SimpleListModel(Array)).
If Array > 10 then the combobox just show 10 data.
Can everyone help me?

best regards

delete flag offensive retag edit

15 Replies

Sort by ยป oldest newest

answered 2010-02-02 20:20:05 +0800

PeterKuo gravatar image PeterKuo
481 2

updated 2010-02-02 20:21:12 +0800

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/

link publish delete flag offensive edit

answered 2010-02-05 07:58:39 +0800

andij62 gravatar image andij62
301 1 7

Hi Peter,

i don't understand what i must do. My zk-version is 3.6.3. Please can you give me an sample!

best regards

link publish delete flag offensive edit

answered 2010-02-05 09:35:33 +0800

n4rk0 gravatar image n4rk0
120 3

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
		
	}


Enter the max number of rows you need in your combobox in getSubModel method (you can modify the class to recibe the max number as a parameter) and your code will be :


combobox.setModel(new myListModel(Array))

link publish delete flag offensive edit

answered 2010-02-06 16:15:58 +0800

andij62 gravatar image andij62
301 1 7

updated 2010-02-06 16:38:54 +0800

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

link publish delete flag offensive edit

answered 2010-02-08 14:09:39 +0800

andij62 gravatar image andij62
301 1 7

Have anyboby an idea? I can't handle this!

link publish delete flag offensive edit

answered 2010-02-08 18:21:11 +0800

n4rk0 gravatar image n4rk0
120 3

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



Regards,

link publish delete flag offensive edit

answered 2010-02-09 04:05:22 +0800

andij62 gravatar image andij62
301 1 7

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

and i debug the field "myLM" then it has the following value


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

link publish delete flag offensive edit

answered 2010-02-09 09:13:15 +0800

n4rk0 gravatar image n4rk0
120 3

I'm using the same ZK version (3.6.3) as you, and it works for me. Can you show more code ? ... maybe you are doing something more ...

link publish delete flag offensive edit

answered 2010-02-09 12:58:46 +0800

andij62 gravatar image andij62
301 1 7
Hi n4rk0, here are more code
<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));	

	}
and your code
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
		
	}
}

link publish delete flag offensive edit

answered 2010-02-10 10:55:34 +0800

andij62 gravatar image andij62
301 1 7

Hallo n4rk0,

do you have any ideas?

regards

link publish delete flag offensive edit
Your reply
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow

RSS

Stats

Asked: 2010-02-02 09:19:52 +0800

Seen: 1,178 times

Last updated: Feb 11 '10

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More