我正在使用 GWT/GXT 并尝试创建一个“普通”组合框 - 您无法输入,但您可以输入单个字符,它会自动转到列表中以该字母开头的第一项。所以,我不想要它 READONLY,我想要它,这样你就不能用你自己的文本替换其中的文本(不能在其中输入字符)。
我不知道如何让 ComboBox 或 SimpleComboBox 做到这一点。我已经尝试了所有设置组合,但均无济于事。我确实看到有一个 GXT ListBox,但我需要一个从 Field 扩展的组件。
真的没有办法做到这一点还是我错过了什么?
旧帖子,但这非常令人沮丧,我同意。以下可能是您正在寻找的内容。
SimpleComboBox<String> names = new SimpleComboBox<String>();
names.add( "Brian" );
names.add( "Kevin" );
names.add( "Katie" );
names.setTriggerAction( TriggerAction.ALL );
通过使用setEditable(false)
和setForceSelection(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;
}
}
}
});
}
}
和测试:
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);
}
}
给组合框焦点并按“T”应该在列表中选择“两个”。
照原样,该类始终选择列表中以字符开头的第一项;但是,修改它以使其选择列表中的下一个项目并不难(就像“真实”组合框的典型情况一样)。
ListBox 做了我一直在寻找的事情——它是不可编辑的,你可以在它集中时按下一个键,它将进入下一场比赛。
使用ListBox
很好,但它是 gwt,而不是 gxt(至少对于 gxt 2.2.5 没有ListBox
)。对于这种情况,您必须使用 gxt api cosider 以下代码(它有效,我已经测试过):
SimpleComboBox<MyEmumType> simpleComboBox = new SimpleComboBox<MyEmumType>();
List<MyEmumType> values = Arrays.asList(MyEmumType.values());
//here you set all values
simpleComboBox.add(values);
//here set first to display. it does not add new, just display one from collection
simpleComboBox.setSimpleValue(values.iterator().next());
//prevent combobox for setting/searching values out of collection
simpleComboBox.setEditable(false);
//disable autocomplete, with first value it will display all others
simpleComboBox.setTriggerAction(ComboBox.TriggerAction.ALL);
PS我在这里使用了一个枚举,但认为该代码适用于其他类型。