2010-03-25 20 views
4

我正在使用GWT/GXT並嘗試創建一個「普通」組合框 - 一個無法輸入的組合框,但您可以鍵入單個字符並自動轉到列表中以該字母開頭的第一個項目。所以,我不需要它READONLY,我想要它,以便您不能用自己的文本替換它中的文本(不能在其中鍵入字符)。如何創建不可編輯的GXT ComboBox?

我想不出如何讓ComboBox或SimpleComboBox來做到這一點。我已經嘗試過所有的設置組合都無濟於事。我確實看到有一個GXT列表框,但我需要一個從Field擴展的組件。

是否真的沒有辦法做到這一點,或者我錯過了什麼?

回答

2

ListBox中做什麼,我一直在尋找 - 這是不可編輯的,雖然它的重點,你可以按一個鍵,它會進入下一場比賽。

4

通過使用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」 應該在列表中選擇 「二」。

現在,類總是選擇列表中以字符開頭的第一項;然而,修改它以使其選擇列表中的下一個項目並不困難(正如「真實」組合框的典型例子)。

+0

我想我不清楚。我知道我可以自己做這一切,我只是認爲必須有一個內置的方式來做到這一點,有適當的選擇。也許沒有? – 2010-03-29 13:12:31

+0

我沒有看到有意義的東西。 「你會想」的選項會引起這種行爲,不要。也許這是一個錯誤,或者一個很好的功能請求。 – 2010-03-29 15:12:12

+0

是的 - 這就是我的想法,但我希望我錯過了一些東西。謝謝。 – 2010-04-05 14:03:50

5

舊帖子,但這是非常令人沮喪,我同意。以下可能是你要找的。

SimpleComboBox<String> names = new SimpleComboBox<String>(); 
names.add("Brian"); 
names.add("Kevin"); 
names.add("Katie"); 
names.setTriggerAction(TriggerAction.ALL); 
2

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

附:我在這裏使用枚舉,但認爲代碼與其他類型的作品。

+0

我聲明「我使用GWT/GXT並試圖創建一個」普通「ComboBox」,所以GWT解決方案是可以接受的(儘管問題確實表明我正在尋找一個GXT解決方案,但那真的不是需求)。你的解決方案看起來不錯。 – 2015-12-08 16:42:57

+1

謝謝!我正在尋找一個GXT(非GWT)解決方案,'setEditable(false)'和'setTriggerAction(TriggerAction.ALL)'很好。 +1 – 2016-02-17 12:39:05