JOptionPane.showInputDialog() in GWT

余生颓废 提交于 2019-12-13 13:46:12

问题


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


来源:https://stackoverflow.com/questions/9799879/joptionpane-showinputdialog-in-gwt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!