[英]How do I create a non-editable GXT ComboBox?
我正在使用GWT / GXT并尝试创建一个“普通”ComboBox - 一个你无法输入的组合框,但你可以键入一个字符,它会自动转到列表中以该字母开头的第一个项目。 因此,我不希望它为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我在这里使用枚举,但认为代码适用于其他类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.