如何创建不可编辑的GXT ComboBox?

Bri*_*ipa 4 java gwt gxt

我正在使用GWT/GXT并尝试创建一个"普通"ComboBox - 一个你无法输入的组合框,但你可以键入一个字符,它会自动转到列表中以该字母开头的第一个项目.因此,我不希望它为READONLY,我想要它,以便您不能用自己的文本替换其中的文本(不能在其中键入字符).

我无法弄清楚如何让ComboBox或SimpleComboBox这样做.我已经尝试了它的每个组合设置无济于事.我确实看到有一个GXT ListBox,但我需要一个从Field扩展的组件.

真的没有办法做到这一点,还是我错过了什么?

Mic*_*ley 5

通过使用setEditable(false)andsetForceSelection(true)和扩展该类,您可以自己完成此操作(通过观察小部件上的按键)。

首先,子类:

package net.binarymuse.gwt.gxt.client;

import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.KeyListener;
import com.extjs.gxt.ui.client.widget.form.SimpleComboBox;

public class MySimpleComboBox<T extends String> extends SimpleComboBox<T> {

    public MySimpleComboBox() {
        super();
        this.addKeyListener(new KeyListener(){
            @Override
            public void componentKeyDown(ComponentEvent event)
            {
                // Get a reference to the combobox in question
                MySimpleComboBox<T> combo = MySimpleComboBox.this;

                // Get the character that has been pressed
                String sChar = String.valueOf((char) event.getKeyCode());
                // TODO - add some checking here to make sure the character is
                //        one we actually want to process

                // Make sure we have items in the store to iterate
                int numItems = combo.getStore().getCount();
                if(numItems == 0)
                    return;

                // Check each item in the store to see if it starts with our character
                for(int i = 0; i < numItems; i++)
                {
                    String value = combo.getStore().getAt(i).getValue();
                    // If it does, select it and return
                    if(value.startsWith(sChar) || value.startsWith(sChar.toUpperCase()))
                    {
                        MySimpleComboBox.this.setSimpleValue((T) value);
                        return;
                    }
                }
            }
        });
    }

}
Run Code Online (Sandbox Code Playgroud)

和测试:

package net.binarymuse.gwt.gxt.client;

import com.extjs.gxt.ui.client.widget.form.SimpleComboBox;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.RootPanel;


public class GxtSandbox implements EntryPoint {

    public void onModuleLoad() {
        SimpleComboBox<String> box = new MySimpleComboBox<String>();
        box.add("One");
        box.add("Two");
        box.add("Three");
        box.setEditable(false);
        box.setForceSelection(true);

        RootPanel.get().add(box);
    }
}
Run Code Online (Sandbox Code Playgroud)

给予组合框焦点并按“T”应选择列表中的“二”。

按照原样,类始终选择列表中以该字符开头的第一项;但是,修改它以使其选择列表中的下一个项目并不困难(就像“真实”组合框的典型情况)。


Bri*_*ian 5

老帖但这非常令人沮丧,我同意.以下可能是您正在寻找的.

SimpleComboBox<String> names = new SimpleComboBox<String>();
names.add( "Brian" );
names.add( "Kevin" );
names.add( "Katie" );
names.setTriggerAction( TriggerAction.ALL );
Run Code Online (Sandbox Code Playgroud)


Bri*_*ipa 2

ListBox 做了我一直在寻找的东西 - 它是不可编辑的,你可以在它聚焦时按下一个键,它将转到下一场比赛。