4

私は GWT/GXT を使用しており、「通常の」ComboBox を作成しようとしています。これは入力できませんが、単一の文字を入力すると、その文字で始まるリストの最初の項目に自動的に移動します。したがって、READONLY ではなく、その中のテキストを自分のテキストに置き換えることができないようにしたい (文字を入力できない)。

ComboBox または SimpleComboBox を取得してこれを行う方法がわかりません。設定のあらゆる組み合わせを試しましたが、役に立ちませんでした。GXT ListBox があることは確認しましたが、Field から拡張するコンポーネントが必要です。

これを行う方法は本当にありませんか、それとも何か不足していますか?

4

4 に答える 4

5

古い投稿ですが、これは非常にイライラします。同意します。以下はおそらくあなたが探しているものです。

SimpleComboBox<String> names = new SimpleComboBox<String>();
names.add( "Brian" );
names.add( "Kevin" );
names.add( "Katie" );
names.setTriggerAction( TriggerAction.ALL );
于 2012-08-17T23:46:43.750 に答える
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" を押すと、リストで "Two" が選択されます。

そのままで、クラスは常に文字で始まるリスト内の最初の項目を選択します。ただし、リスト内の次の項目を選択するように変更することは難しくありません (「実際の」コンボ ボックスでよくあることです)。

于 2010-03-28T23:38:56.990 に答える
2

ListBox は私が探していたものを実行します。これは編集不可であり、フォーカスされている間にキーを押すと、次の一致に移動します。

于 2012-10-03T14:38:50.907 に答える