JDK-8087914 : [JFXPanel] Clicking on Menu in JFXPanel ignored if another swing component has focus
  • Type: Bug
  • Component: javafx
  • Sub-Component: swing
  • Affected Version: 8u25
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • Submitted: 2014-12-08
  • Updated: 2019-09-18
  • Resolved: 2017-08-22
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 10
10Fixed
Related Reports
Relates :  
Relates :  
Relates :  
Description
The following test program (a slightly modified version of the one from RT-39621 to avoid using an AWT component since it doesn't affect the test) will illustrate this.

Steps:

1) Run the program
2) Click in the JTextField at the bottom
3) BUG: Click on the "Menu" and notice that the pull down menu doesn't popup
4) Click on it again and it is fine

If you click anywhere in the top JFXPanel before clicking on the menu  (between steps 2 and 3), then it is also fine.


-----------------------------------------------------------------------------------------------------------------

import java.awt.GridLayout;
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.paint.Color;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;

public class FxSwing {

    private static void initAndShowGUI() {
        // This method is invoked on the EDT thread
        JFrame frame = new JFrame("Swing and JavaFX");
        JPanel contentPane = new JPanel(new GridLayout(2, 1));
        final JFXPanel fxPanel = new JFXPanel();
        contentPane.add(fxPanel);
        contentPane.add(new JTextArea("Here is some text"));

        frame.add(contentPane);

        frame.setSize(300, 200);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Platform.runLater(() -> initFX(fxPanel));
    }

    private static void initFX(JFXPanel fxPanel) {
        // This method is invoked on the JavaFX thread
        Scene scene = createScene();
        fxPanel.setScene(scene);
    }

    private static Scene createScene() {
        Group root = new Group();
        Scene scene = new Scene(root, Color.ALICEBLUE);

        MenuBar menuBar = new MenuBar();

        Menu menuFile = new Menu("Menu");

        menuFile.getItems().addAll(new MenuItem("item 1"),
                new MenuItem("item 2"),
                new MenuItem("item 3"),
                new MenuItem("item 4"));

        menuBar.getMenus().addAll(menuFile);
        root.getChildren().addAll(menuBar);

        return (scene);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> initAndShowGUI());
    }
}

Comments
Changeset: 0a7078250c16 Author: psadhukhan Date: 2017-08-22 11:14 +0530 URL: http://hg.openjdk.java.net/openjfx/10-dev/rt/rev/0a7078250c16
22-08-2017

+1
17-08-2017

OK. Modified webrev http://cr.openjdk.java.net/~psadhukhan/fx/8087914/webrev.02/
17-08-2017

Shouldn't we add null check for context? If it is null we can simply do not post event.
16-08-2017

Modified webrev after inputs from Alex http://cr.openjdk.java.net/~psadhukhan/fx/8087914/webrev.01/
16-08-2017

In JFXPanel#processMouseEvent protected void processMouseEvent(MouseEvent e) { if ((e.getID() == MouseEvent.MOUSE_PRESSED) && (e.getButton() == MouseEvent.BUTTON1)) { if (!hasFocus()) { requestFocus(); ... .} it request for a focus when fx component does not have focus. Now, this focus request event goes to eventqueue and will be asynchronously handled and fx will gain focus when Event Dispatch Thread calls JFXPanel.processFocusEvent(). So, MOUSE_PRESSED event will not be honoured by FX immediately due to lack of focus in fx component. Proposed fix is to fire the same MOUSE_PRESSED event after requestFocus() so that 2nd mouse press will be honoured since now fx have focus. http://cr.openjdk.java.net/~psadhukhan/fx/8087914/webrev.00/ Request to review this fix.
11-08-2017

Please target this to "10" if you propose to fix it now.
11-08-2017