自定义GWT ListBox小部件

时间:2015-07-30 18:31:53

标签: java gwt listbox widget

我正在尝试创建自定义GWT ListBox小部件。我列表中的项目有详细的文本和简洁的文本。当列表框打开(展开)时,显示详细文本,例如“帽子里的猫”。一旦选择了一个项目,就会显示文本的简洁形式,例如“cat”。

我正在使用两个GWT ListBoxes(一个带有详细字符串,另一个带有简洁字符串)并通过onClick处理程序隐藏其中一个。我当前的问题是,当我单击简洁列表框并按住单击时,它显示所有简洁字符串,当我希望它显示另一个列表框时。

有什么建议吗?

我的代码:

这是我的代码:

public class TerseVerboseListBox extends Composite implements ClickHandler {
//implements ClickHandler, ChangeHandler, HasChangeHandlers {

//onset OnsetListBox extends ListBox and accepts TerseVerboseItem as an argument to AddItem
//TerseVerboseItem simply contains two strings, a terse one and a verbose one
private OnsetListBox verboseListBox;
private OnsetListBox terseListBox; 
private AbsolutePanel absolutePanel;

/**
 * Constructs a TerseVerboseListBox 
 * 
 */
public TerseVerboseListBox() {
    absolutePanel = new AbsolutePanel();
    terseListBox = new OnsetListBox();
    verboseListBox = new OnsetListBox();

    populateListBoxes();

    //replace verbose list box with terse listbox after item is selected
    verboseListBox.addClickHandler(new ClickHandler() {
        @Override
        public void onClick(final ClickEvent event) {
            int selectedIndex = verboseListBox.getSelectedIndex();              
            verboseListBox.setVisible(false);
            terseListBox.setVisible(true);
            terseListBox.setSelectedIndex(selectedIndex);
        }
    });

    terseListBox.addChangeHandler(new ChangeHandler() {
        @Override
        public void onChange(final ChangeEvent event) {
            verboseListBox.setVisible(true);
            terseListBox.setVisible(false);
        }
    });

    terseListBox.setVisible(false);
    absolutePanel.add(verboseListBox);
    absolutePanel.add(terseListBox);

    // All composites must call initWidget() in their constructors.
    initWidget(absolutePanel);
}

private void populateListBoxes() {
    TerseVerboseItem tvItem = new TerseVerboseItem();
    tvItem.setColor(Color.GREEN);
    tvItem.setPosition(1);
    tvItem.setSeparator(false);
    tvItem.setTerseForm("<=");
    tvItem.setVerboseForm("Present by birth");

    TerseVerboseItem tvItem2 = new TerseVerboseItem();
    tvItem2.setColor(Color.BLACK);
    tvItem2.setPosition(2);
    tvItem2.setSeparator(false);
    tvItem2.setTerseForm("@6M");
    tvItem2.setVerboseForm("Onset at 6 months");

    verboseListBox.addItem(tvItem);
    verboseListBox.addItem(tvItem2);
    terseListBox.addTerseItem(tvItem);
    terseListBox.addTerseItem(tvItem2);
}

@Override
public void onClick(ClickEvent event) {
    //maybe I should catch any clicks here
    //and determine which listbox is visible and show the other one
}

}

0 个答案:

没有答案