2010-03-25 2 views
4

GWT/GXT를 사용하고 있으며 "일반"ComboBox를 만들려고합니다. 하나는 입력 할 수 없지만 하나의 문자를 입력하면 자동으로 해당 문자로 시작하는 목록의 첫 번째 항목으로 이동합니다 . 그래서, 나는 그것을 READONLY하고 싶지 않다, 당신이 자신의 텍스트로 텍스트를 대체 할 수 없기를 바란다 (그것에 문자를 입력 할 수 없다).편집 할 수없는 GXT ComboBox는 어떻게 만듭니 까?

ComboBox 또는 SimpleComboBox를 가져 오는 방법을 알 수 없습니다. 나는 아무 소용이 그것에 설정의 모든 조합을 시도했습니다. GXT ListBox가 있다는 것을 알았지 만, Field에서 확장하는 구성 요소가 필요합니다.

이 작업을 수행 할 수있는 방법이 없습니까, 아니면 누락 된 것이 있습니까?

답변

2

옵니다 내가 찾던 않습니다 - 그것은 불가능한 그리고 그것은 초점이 맞춰 중에도 키를 누를 수는 다음 경기로 이동합니다.

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); 
    } 
} 

콤보 상자 포커스를 부여하고 목록에서 "2"를 선택해야한다 "T"를 누르면.

마찬가지로 클래스는 항상 문자로 시작하는 목록의 첫 번째 항목을 선택합니다. 그러나 목록에서 다음 항목을 선택하도록 수정하는 것은 어렵지 않습니다 ("실제"콤보 상자와 마찬가지로).

+0

나는 분명치 않다. 나는 모든 것을 혼자 할 수 있다는 것을 알고 있으며, 적절한 옵션으로 이것을 수행 할 수있는 내장 된 방법이 있어야한다고 생각했습니다. 어쩌면 거기 있지 않을까요? –

+0

나는 이해가 안가는 것을 본다. "당신이 생각할"수있는 옵션은이 동작을 호출하지 않습니다. 버그 일 수도 있고 좋은 기능 요청 일 수도 있습니다. –

+0

그래, 그게 내가 생각한거야.하지만 내가 방금 뭔가를 놓쳤 으면 좋겠다. 감사. –

5

이전 게시물이지만 매우 실망 스럽지만 동의합니다. 아래는 아마도 당신이 찾고있는 것입니다.

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

ListBox을 사용하는 것이 좋지만 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); 

P.S.을 여기에 enum을 사용했지만 코드가 다른 유형과 작동한다고 생각합니다.

+0

"저는 GWT/GXT를 사용하고"일반 "ComboBox"를 만들려고 했으므로 GWT 솔루션을 사용할 수 있습니다 (질문에서 GXT 솔루션을 찾고 있음을 나타내지 만 실제로는 그렇지 않습니다). 요구 사항). 귀하의 솔루션은 좋아 보인다. –

+1

감사합니다. 나는 GXT (non-GWT) 솔루션을 찾고 있었고'setEditable (false)'와'setTriggerAction (TriggerAction.ALL)'은 훌륭하게 작동한다. +1 –

관련 문제