通過使用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」 應該在列表中選擇 「二」。
現在,類總是選擇列表中以字符開頭的第一項;然而,修改它以使其選擇列表中的下一個項目並不困難(正如「真實」組合框的典型例子)。
我想我不清楚。我知道我可以自己做這一切,我只是認爲必須有一個內置的方式來做到這一點,有適當的選擇。也許沒有? – 2010-03-29 13:12:31
我沒有看到有意義的東西。 「你會想」的選項會引起這種行爲,不要。也許這是一個錯誤,或者一個很好的功能請求。 – 2010-03-29 15:12:12
是的 - 這就是我的想法,但我希望我錯過了一些東西。謝謝。 – 2010-04-05 14:03:50