質問
SWTおよびAWT用のJavaで画面キーボードを実装しています。 重要なことの1つは、選択したテキストフィールドが表示され、オンスクリーンキーボードの背後にない位置にキーボードを移動することです。
AWTの場合、現在選択されているコンポーネントの位置を検出できます
Component owner = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
if (owner == null) {
return;
}
Point ownerLocation = owner.getLocationOnScreen();
Dimension ownerSize = owner.getSize();
SWTで同じロジックを実装するにはどうすればよいですか? SWTイベントキューにフォーカスリスナーを追加することで、現在選択されているウィジェットを取得します。しかし、私が電話するとき
Point location = new Point(mTextWidget.getLocation().x, mTextWidget.getLocation().y);
Dimension dimension = new Dimension(mTextWidget.getSize().x, mTextWidget.getSize().y);
親コンポジットの相対位置を取得します。
どのようにして特別なウィジェットの位置を完全な画面に相対的に取得できますか?
解決
メソッド Control.toDisplay()は、座標を画面に相対的な座標に変換できるはずです。
このスニペットは、あなたが何をしているのかを示しているかもしれません:
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();
}
}
所属していません StackOverflow