JOptionPane.showInputDialog() in GWT

2019-07-02 00:34发布

问题:

Is there any simple way to create instance of modal DialogBox with single text input control, which will return String entered into the text control on pressing "OK"?

I'm looking for something similar to JOptionPane.showInputDialog() one-liner from Swing.

回答1:

You can create your own class,which will contain all you need. Small example:

class MyDialogBox extends DialogBox {
        private TextBox textBox = new TextBox();
        private Button okButton = new Button("Ok");

        public MyDialogBox(Label label) {
            super();
            setText("My Dialog Box");
            final Label l = label;
            okButton.addClickHandler(new ClickHandler() {
                @Override
                public void onClick(ClickEvent event) {
                    hide();
                    l.setText(textBox.getText());
                }
            });
            VerticalPanel vPanel = new VerticalPanel();
            vPanel.add(textBox);
            vPanel.add(okButton);
            setWidget(vPanel);
        }
    }

and example of using

public void onModuleLoad() {
    Label label = new Label("Text");
    final MyDialogBox mDBox = new MyDialogBox(label);
    Button btn = new Button("Click me!");

    btn.addClickHandler(new ClickHandler() {
        @Override
        public void onClick(ClickEvent event) {
            mDBox.center();
            mDBox.show();
        }
    });
    RootPanel.get().add(label);
    RootPanel.get().add(btn);
}