简体   繁体   中英

SWT Dialog has no title bar at runtime

I am very used to using the Swing designer in NetBeans, but I figured I'd try something new. I'm very new to using the SWT designer in Eclipse, so I'm still learning.

I'm trying to create a Dialog that is supposed to be modal to an ApplicationWindow . The GUI designer shows the title bar (with the window title, close/minimize buttons, etc.), but when the dialog opens at runtime, the title bar is completely gone.

Am I missing something? I'm trying to make an equivalent to a modal JDialog on top of a JFrame . I haven't found anything substantial.

EDIT: I discovered the style SWT.DEFAULT which makes the modal dialog "slide down" in front of the parent window (I'm on a Mac), which is certainly better than a non-movable separate window with no title bar, but if anyone can provide more info that would be extremely helpful.

I can't speak to using SWT Designer as I've never used it, but I can provide a code snippet.

I'd recommend taking a look at creating a class which extends the JFace Dialog abstract class.

You can use the setShellStyle(int) method to change the style, specifically using a modal style bit such as SWT.APPLICATION_MODAL or SWT.PRIMARY_MODAL . If you want the Dialog to be modal to all windows in the application, use SWT.APPLICATION_MODAL , or if you'd like it to only be modal to the parent, use SWT.PRIMARY_MODAL .

For example:

public class ModalDialog extends Dialog {

    public ModalDialog(final Shell parentShell) {
        super(parentShell);
        // SWT.APPLICATION_MODAL or SWT.PRIMARY_MODAL depending on needs
        setShellStyle(SWT.APPLICATION_MODAL | SWT.TITLE | SWT.BORDER | SWT.CLOSE);
    }

}

You can add/remove style bits (and the buttons, title, content, etc.) depending on what you're looking for, but this will give you a minimal Dialog which is modal to the application:

在此处输入图片说明

Full example:

public class ModalDialogTest {

    public static class ModalDialog extends Dialog {

        /**
         * @param parentShell
         */
        public ModalDialog(final Shell parentShell) {
            super(parentShell);
            setShellStyle(SWT.APPLICATION_MODAL | SWT.TITLE | SWT.BORDER | SWT.CLOSE);
        }

    }

    private final Display display;
    private final Shell shell;

    public ModalDialogTest() {
        display = new Display();
        shell = new Shell(display);
        shell.setLayout(new FillLayout());

        final ApplicationWindow applicationWindow = new ApplicationWindow(shell);
        applicationWindow.open();

        final Button button = new Button(shell, SWT.PUSH);
        button.setText("Open modal dialog");
        button.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(final SelectionEvent e) {
                // Create the modal Dialog on the ApplicationWindow
                new ModalDialog(applicationWindow.getShell()).open();
            }
        });
    }

    public void run() {
        shell.setSize(200, 200);
        shell.open();

        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
        display.dispose();
    }

    public static void main(final String... args) {
        new ModalDialogTest().run();
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM