Focus problems with JDK7 and built-in components

We have a swing application that embeds the ocx IE component through JNIWrapper.

After moving from jdk6 to jdk7, we begin to notice focus problems. When embedded IE shows a web page with text fields (like a Google search page), than the problem starts:

The browser "captures" the focus, so you can start typing in the search field. Each key entered is sent to IE ocx. But swing seems to ignore this change of focus. Even if I focus on the swing text field (and the swing shows a blinking input cursor), all the keys entered go to IE ocx

The only way to “fix” the focus is to deactivate and activate the main frame. after that, the focus seems consistent. But if I click again in the Google search text box, the focus will break again.

It seems that in jdk7 there is a big shift in focus. From the link :

On the Windows platform, the concept of "synthetic focus" is implemented. This means that the focus owner component only emulates its custom state, while the real focus is set to the focus proxy component. This component receives the main and initial input method messages and sends them to the focus owner. Prior to JDK7, the focus proxy component was a dedicated hidden child component within the frame / dialog. In JDK7, the frame / dialog itself serves as the proxy focus. Now it proxies focuses not only on components in its own window, but also on all child components. A simple window never gets its own focus and relies on the focus proxy of its owner. This mechanism is transparent to the user, but it should be considered when debugging.

Does anyone have an idea to "fix" behavior?

EDIT: here is the code to reproduce the problem with JxBrowser

public static void main(String[] args) { Browser browser = BrowserFactory.createBrowser(BrowserType.IE); JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(browser.getComponent(), BorderLayout.CENTER); JPanel panel = new JPanel(); frame.getContentPane().add(panel, BorderLayout.NORTH); textField = new JTextField(); panel.add(textField); textField.setColumns(10); frame.setSize(700, 500); frame.setLocationRelativeTo(null); frame.setVisible(true); browser.navigate("http://www.google.com"); } 
+6
source share
4 answers

All - we just experienced this problem with another browser component (DjProject Native Swing). Everything works fine under Java 1.6, but in Java-7 we started to see strange problems where you can enter in the input field, but if you select back to fix a typo that you could not enter after clicking the mouse. To restore, you had to select from the input field, and then return to continue editing.

See http://docs.oracle.com/javase/7/docs/webnotes/tsg/TSG-Desktop/html/awt.html#gdcqp In particular, we are talking about syntax focus and focus proxy.

In any case, in order to shorten the long history - in our case, I managed to find a job by connecting the mouse listener to JWebBrowser.getNativeComponent (). Then, on mousePressed, execute the .requestFocus () browser, followed by the .getNativeComponent () browser. RequestFocusInWindow ();

Hope this helps someone else who comes across this.

+3
source

Try to find FocusListener and WindowFocusListener to implement listeners for the Swing or JFrame components themselves. Whenever a focusLost(..) call is made, you can use requestFocus() in a window or component to force the focus to return.

0
source

I finally found a working solution. While figuring out what caused this, it was noticed that clicking on another component (i.e. JLabel) and clicking on the text box worked fine. So I reproduced this behavior using the AWT Robot class. See Replicating mouse behavior in code for more information .

0
source

Try using the latest jdk Java 1.7. It worked for me. Previously, I could not enter any data into the text fields. After the update, I do not encounter this problem.

http://bugs.sun.com/view_bug.do?bug_id=8018672

0
source

Source: https://habr.com/ru/post/913632/


All Articles