Search code examples
javaswtmetricstitlebar

Java SWT Height of shell Caption/Title bar


How do I determine the height of an application window's title bar in a Java SWT application application?

Since Java is platform agnostic, I need to find the Java way.

As a side question, is the title/caption bar height information in the same location as other system/window metrics?

I tried searching for "caption bar" and "title bar", just did not see anything.


Solution

  • You basically want to calculate the height difference between the "bounds" of your Shell and the "client area" of the Shell:

    public static void main(String args[])
    {
        Display display = new Display();
        final Shell shell = new Shell(display);
        shell.setText("StackOverflow");
        shell.setLayout(new FillLayout());
    
        Button button = new Button(shell, SWT.PUSH);
        button.setText("Calculate");
        button.addListener(SWT.Selection, new Listener()
        {
            @Override
            public void handleEvent(Event arg0)
            {
                Rectangle outer = Display.getCurrent().getActiveShell().getBounds();
                Rectangle inner = Display.getCurrent().getActiveShell().getClientArea();
    
                System.out.println(outer.height - inner.height);
            }
        });
    
        shell.pack();
        shell.setSize(400, 200);
        shell.open();
        while (!shell.isDisposed())
        {
            if (!shell.getDisplay().readAndDispatch())
                shell.getDisplay().sleep();
        }
    }
    

    Will print 31 (px) on my laptop running Linux Mint.