Search code examples
javaswt

Setting size of inner region of Java SWT shell window


In a Java SWT shell window, how do I set its inner size than its whole window frame size?

For instance, if I use shell.setSize(300, 250) this would make the whole window appearing as exactly 300x250. This 300x250 includes the size of the window frame.

How can I set the inner size, that is the content display region of the shell window to 300x250 instead? That's this 300x250 excludes the width of the window frame.

I tried to minus some offset values but the thing is different Operating Systems have different window frame sizes. So having a constant offset would not be accurate.

Thanks.


Solution

  • From your question what I understood is that you want to set the dimension of the Client Area. And in SWT lingo it is defined as a rectangle which describes the area of the receiver which is capable of displaying data (that is, not covered by the "trimmings").

    You cannot directly set the dimension of Client Area because there is no API for it. Although you can achieve this by a little hack. In the below sample code I want my client area to be 300 by 250. To achieve this I have used the shell.addShellListener() event listener. When the shell is completely active (see the public void shellActivated(ShellEvent e)) then I calculate the different margins and again set the size of my shell. The calculation and resetting of the shell size gives me the desired shell size.

    >>Code:

    import org.eclipse.swt.SWT;
    import org.eclipse.swt.events.ShellEvent;
    import org.eclipse.swt.events.ShellListener;
    import org.eclipse.swt.layout.GridData;
    import org.eclipse.swt.layout.GridLayout;
    import org.eclipse.swt.widgets.Display;
    import org.eclipse.swt.widgets.Menu;
    import org.eclipse.swt.widgets.Shell;
    
    public class MenuTest {
    
        public static void main (String [] args) 
        {
            Display display = new Display ();
            final Shell shell = new Shell (display);
    
            GridLayout layout = new GridLayout();
            layout.marginHeight = 0;
            layout.marginWidth = 0;
            layout.horizontalSpacing = 0;
            layout.verticalSpacing = 0;
            layout.numColumns = 1;
            shell.setLayout(layout);
            shell.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,true));
            final Menu bar = new Menu (shell, SWT.BAR);
            shell.setMenuBar (bar);
    
            shell.addShellListener(new ShellListener() {
    
                public void shellIconified(ShellEvent e) {
                }
                public void shellDeiconified(ShellEvent e) {
                }
                public void shellDeactivated(ShellEvent e) {
                }
                public void shellClosed(ShellEvent e) {
                    System.out.println("Client Area: " + shell.getClientArea());
                }
                public void shellActivated(ShellEvent e) {
                    int frameX = shell.getSize().x - shell.getClientArea().width;
                    int frameY = shell.getSize().y - shell.getClientArea().height;
                    shell.setSize(300 + frameX, 250 + frameY);
                }
            });     
    
            shell.open ();
            while (!shell.isDisposed()) {
                if (!display.readAndDispatch ()) display.sleep ();
            }
            display.dispose ();
        }
    }