Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Keyboard on focused Control hold by AWT under GTK(Once focused, the JTextField keeps the focus for keyboard events)
Keyboard on focused Control hold by AWT under GTK [message #1805317] Thu, 11 April 2019 14:22 Go to next message
Patrick Muscat is currently offline Patrick MuscatFriend
Messages: 14
Registered: May 2018
Junior Member
Hello,

Under GTK 2.24.32 or 3.22.30:
This little piece of code shows how the Swing / AWT text field keeps the focus for keyboard events once it obtains it. It should release keyboard focus when the Text (SWT) or the TextField (JavaFX) are focused.
The mouse focus is ok (including the selection paste using middle button) event if the keyboard focus is held by the JTextField.
The keyboard focus is ok when completely leaving the program window.

Under win32, the keyboard focus follows correctly the mouse focus.

SWT fragment version is org.eclipse.swt.gtk.linux.x86_64_3.106.0.v20170608-0516.jar

I suppose the problem is in SWT_AWT but is is also linked with GTK, any help or clue will be appreciated.

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.embed.swt.FXCanvas;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import java.awt.Frame;
import java.awt.KeyboardFocusManager;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.JPanel;
import javax.swing.JTextField;

import org.eclipse.swt.SWT;
import org.eclipse.swt.awt.SWT_AWT;
import org.eclipse.swt.internal.gtk.OS;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class JavaFXAndAWTInsideSWT {

	public static void main(String[] args) {
		// Print GTK version.
		System.out.println(
				"GTK Version: " + OS.gtk_major_version() + "." + OS.gtk_minor_version() + "." + OS.gtk_micro_version());

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

		// SWT
		Composite composite = new Composite(shell, SWT.FILL);
		composite.setLayout(new GridLayout());
		Text text = new Text(composite, SWT.NONE);
		text.addFocusListener(new org.eclipse.swt.events.FocusListener() {
			@Override
			public void focusGained(org.eclipse.swt.events.FocusEvent arg0) {
				System.out.println("focusGained (SWT)");
			}

			@Override
			public void focusLost(org.eclipse.swt.events.FocusEvent arg0) {
				System.out.println("focusLost (SWT)");
			}
		});
		text.setText("Text");

		// JavaFX
		FXCanvas fxCanvas = new FXCanvas(shell, SWT.NONE);
		TextField textField = new TextField("Text");
		textField.focusedProperty().addListener(new ChangeListener<Boolean>() {
			@Override
			public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
				System.out.println(newValue ? "focusGained (JavaFX)" : "focusLost (JavaFX)");
			}
		});
		Group group = new Group(textField);
		Scene scene = new Scene(group);
		fxCanvas.setScene(scene);

		// AWT
		JPanel jPanel = new JPanel();
		JTextField jTextField = new JTextField("Text", 20);
		jTextField.addFocusListener(new java.awt.event.FocusListener() {
			@Override
			public void focusGained(java.awt.event.FocusEvent e) {
				System.out.println("focusGained (AWT)");
			}

			@Override
			public void focusLost(java.awt.event.FocusEvent e) {
				System.out.println("focusLost (AWT)");
			}
		});
		jPanel.add(jTextField);
		Composite embedded = new Composite(shell, SWT.EMBEDDED);
		Frame frame = SWT_AWT.new_Frame(embedded);
		frame.add(jPanel);

		KeyboardFocusManager focusManager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
		focusManager.addPropertyChangeListener(new PropertyChangeListener() {
			public void propertyChange(PropertyChangeEvent e) {
				System.out.println("focus property changes: " + e.getPropertyName());
			}
		});

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


Re: Keyboard on focused Control hold by AWT under GTK [message #1807870 is a reply to message #1805317] Tue, 11 June 2019 08:25 Go to previous messageGo to next message
Patrick Muscat is currently offline Patrick MuscatFriend
Messages: 14
Registered: May 2018
Junior Member
Hello,

Have all of these bugs the same root cause ?

https://bugs.eclipse.org/bugs/show_bug.cgi?id=389786
https://bugs.eclipse.org/bugs/show_bug.cgi?id=211450
https://bugs.eclipse.org/bugs/show_bug.cgi?id=295113
https://bugs.eclipse.org/bugs/show_bug.cgi?id=295493
https://bugs.eclipse.org/bugs/show_bug.cgi?id=341991
https://bugs.eclipse.org/bugs/show_bug.cgi?id=353683
https://bugs.eclipse.org/bugs/show_bug.cgi?id=372595

There is a brutish workaround in the discussion of 353683... I'll try that :)
Re: Keyboard on focused Control hold by AWT under GTK [message #1807877 is a reply to message #1807870] Tue, 11 June 2019 12:04 Go to previous message
Patrick Muscat is currently offline Patrick MuscatFriend
Messages: 14
Registered: May 2018
Junior Member
Another linked focus issue ? https://bugs.eclipse.org/bugs/show_bug.cgi?id=14711

The use of clearFocusOwner/clearGlobalFocusOwner methods of the AWT KeyboardFocusManager for focusGained event stops the Swing JTextField being notified of typed key but other TextFields are not more notified.

AWT KeyboardFocusManager release the focus but does not give it back to SWT.
Previous Topic:SWT Composite to java.awt.Graphics2D object for creating vector graphics
Next Topic:browser creation silently closes program
Goto Forum:
  


Current Time: Sat May 30 09:53:20 GMT 2020

Powered by FUDForum. Page generated in 0.01955 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top