Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find composite location on screen

I am implementing a on screen keyboard in Java for SWT and AWT. One important thing is to move the keyboard to a position where the selected text field can show and is not lying behind the on screen keyboard.

For AWT i can detect the position of the current selected component with

Component owner = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
if (owner == null) {
    return;
}
Point ownerLocation = owner.getLocationOnScreen();
Dimension ownerSize = owner.getSize();

How can i implement the same logic in SWT? I get the current selected widget by adding a focuslistener to the SWT event queue. But when i call

Point location = new Point(mTextWidget.getLocation().x, mTextWidget.getLocation().y);
Dimension dimension = new Dimension(mTextWidget.getSize().x, mTextWidget.getSize().y);

I will get the position relativ to the parent composite.

How can i get the location of a special widget relativ to the complete screen?

like image 296
Markus Lausberg Avatar asked Dec 04 '08 07:12

Markus Lausberg


Video Answer


1 Answers

I believe the method Control.toDisplay() should be able to translate your coordinates into ones relative to the screen.

This snippet may illustrate what you are after:

package org.eclipse.jface.snippets;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class Bla {
    public static void main(String[] args) {
        Display display = new Display();
        Shell shell = new Shell(display);

        final Text t = new Text(shell,SWT.BORDER);
        t.setBounds(new Rectangle(10,10,200,30));
        System.err.println(t.toDisplay(1, 1));

        Button b = new Button(shell,SWT.PUSH);
        b.setText("Show size");
        b.setBounds(new Rectangle(220,10,100,20));
        b.addSelectionListener(new SelectionAdapter() {

            public void widgetSelected(SelectionEvent e) {
                System.err.println(t.toDisplay(1, 1)); 
            }

        });

        shell.open();

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

        display.dispose();
    }
}
like image 95
VonC Avatar answered Oct 18 '22 16:10

VonC