furby91
furby91

Reputation: 21

How to programmatically open a gwt listbox?

I have a user form with a lot of gwt listbox. The form is like an excel form with named list. It's ugly and the arrows take place.

I would like the cells were like in excel. The arrow appears only when you click in the cell.

I start to program my own widget with a textbox and a listbox embedded into a DeckPanel, switching when you click on the textbox or when the value change. But with this solution, it is necessary to click again to open the listbox.

Now, it will be great, if when you click on the textbox, the listbox will be displayed already open.

In the code below, I try to do this into the method onClick wih this line:

DomEvent.fireNativeEvent(event.getNativeEvent(), listBox);

But it has no effects.

public class CustomListBox extends Composite implements ClickHandler,
    ChangeHandler, HasChangeHandlers {

private final StringListBox listBox;
private final TextBox textBox;
private final DeckPanel panel;

public CustomListBox() {
    textBox = new TextBox();
    textBox.addClickHandler(this);
    textBox.setReadOnly(true);

    listBox = new StringListBox();
    listBox.addChangeHandler(this);
    panel = new DeckPanel();

    panel.add(textBox);
    panel.add(listBox);
    panel.showWidget(0);

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

@Override
public void onClick(ClickEvent event) {
    Object sender = event.getSource();

    if (sender == textBox) {
        panel.showWidget(1);
        DomEvent.fireNativeEvent(event.getNativeEvent(), listBox);
    }
}

public void addItem(String item) {
    listBox.addItem(item);
}

public int getSelectedIndex() {
    return listBox.getSelectedIndex();
}

public String getItemText(int selectedIndex) {
    return listBox.getItemText(selectedIndex);
}

@Override
public HandlerRegistration addChangeHandler(ChangeHandler handler) {
    return listBox.addChangeHandler(handler);
}

@Override
public void onChange(ChangeEvent event) {
    Object sender = event.getSource();
    if (sender == listBox) {
        textBox.setText(getItemText(getSelectedIndex()));
        panel.showWidget(0);
    }
}
}

Upvotes: 1

Views: 1382

Answers (1)

Deanna
Deanna

Reputation: 696

Since you are already programming your own widget, why don't you go all the way. Don't swap out the text box for a list box widget. Instead of a textbox use a label. Add an arrow to your label background when you mouse over, then use a popupPanel for the list itself. In the popupPanel you can make the list items whatever you like, just make sure when you click on it, it sets the text in your original label.

Upvotes: 0

Related Questions