如何创建不可编辑的 GXT 组合框?

发布于 2024-08-26 23:22:29 字数 267 浏览 11 评论 0原文

我正在使用 GWT/GXT 并尝试创建一个“普通”组合框 - 您无法输入该组合框,但您可以输入单个字符,它会自动转到列表中以该字母开头的第一项。所以,我不想要它只读,我想要它,这样你就不能用你自己的文本替换其中的文本(不能在其中输入字符)。

我不知道如何让 ComboBox 或 SimpleComboBox 来执行此操作。我已经尝试了所有设置组合,但均无济于事。我确实看到有一个 GXT ListBox,但我需要一个从 Field 扩展的组件。

真的没有办法做到这一点还是我错过了什么?

I'm using GWT/GXT and trying to create a "normal" ComboBox - one that you cannot type in, but you can type a single character and it will automatically go to the first item in the list that starts with that letter. So, I don't want it READONLY, I want it so that you cannot replace the text in it with your own text (can't type characters into it).

I cannot figure out how to get ComboBox or SimpleComboBox to do this. I've tried every combination of settings on it to no avail. I did see there is a GXT ListBox, but i need a component that extends from Field.

Is there really no way to do this or am I missing something?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

不即不离 2024-09-02 23:22:29

通过使用 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”应该在列表中选择“二”。

按照原样,类始终选择列表中以该字符开头的第一项;但是,修改它以使其选择列表中的下一个项目并不困难(就像“真实”组合框的典型情况)。

By using setEditable(false) and setForceSelection(true) and extending the class, you can accomplish this yourself (by watching for key presses on the widget).

First, the subclass:

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

}

And the test:

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

Giving the combo box focus and pressing "T" should select "Two" in the list.

As is, the class always selects the first item in the list that starts with the character; however, it would not be difficult to modify it to make it select the next item in the list (as is typical with "real" combo boxes).

倾`听者〃 2024-09-02 23:22:29

老帖子,但这非常令人沮丧,我同意。下面的内容可能就是您正在寻找的内容。

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

Old post but this is very frustrating, I agree. The below is probably what you're looking for.

SimpleComboBox<String> names = new SimpleComboBox<String>();
names.add( "Brian" );
names.add( "Kevin" );
names.add( "Katie" );
names.setTriggerAction( TriggerAction.ALL );
梦里泪两行 2024-09-02 23:22:29

ListBox 做了我一直在寻找的东西 - 它是不可编辑的,你可以在它聚焦时按下一个键,它将转到下一场比赛。

ListBox does what I was looking for - it's non-editable and you can press a key while it's focused and it will go to the next match.

把回忆走一遍 2024-09-02 23:22:29

使用 ListBox 很好,但它是 gwt,而不是 gxt(至少对于 gxt 2.2.5 没有 ListBox)。对于这种情况,当你必须使用 gxt api 时,请考虑以下代码(它有效,我已经测试过):

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 我在这里使用了枚举,但认为该代码适用于其他类型。

Using ListBox is good, but it is gwt, not gxt (At least for gxt 2.2.5 there is not ListBox). For such situations when you have to use gxt api cosider following code (it works, i have tested):

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. I have use a enum here, but think that code works with other types.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文