JOptionPane.showInputDialog() in GWT

2019-07-02 00:59发布

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条回答
成全新的幸福
2楼-- · 2019-07-02 01:10

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);
}
查看更多
登录 后发表回答