pcorless / icepdf

PDF Rendering and Viewing API in Java
Apache License 2.0
84 stars 21 forks source link

JavaFX Cannot Get Focus In TextFields #167

Open realumhelp opened 3 years ago

realumhelp commented 3 years ago

I am running PdfFXViewer.java with no modifications and it works fine. However, when I add something else to the BorderPane I can no longer get focus inside of the icepdf Panels. I tried both OpenJDK11 /w OpenFX11 and JDK1.8.

For example add in PdfFXViewer.java:

    @Override
    public void start(Stage primaryStage) throws Exception {
        BorderPane borderPane = new BorderPane();
        Scene scene = new Scene(borderPane);

        //////////////////////////////////////////////////////////////////////////////
        //Add something else to the BorderPane
        HBox hbox = new HBox();
        TextField tf = new TextField(); //(javafx textfield)
        hbox.getChildren().add(tf);
        borderPane.setTop(hbox);
        //////////////////////////////////////////////////////////////////////////////

        // add viewer content pane
        createViewer(borderPane);

It is like JavaFX is no longer giving up Focus over to Swing or something is throwing an Exception that is getting eaten in the background. I tried turning on logging without much success. If anyone has any ideas I'm all ears!

Thanks!

realumhelp commented 3 years ago

I threw in a bit of a hack that requests focus when the mouse enters the Pane but seems to work. This could potentially be a JavaFX bug maybe:

  swingNode.addEventHandler(MouseEvent.MOUSE_ENTERED,
    new EventHandler<MouseEvent>() {
          @Override
          public void handle(MouseEvent e) {
            if(!swingNode.isFocused()) {
              swingNode.requestFocus();
            }
          }
        });

Curious if there is a cleaner solution.

Thanks!

pcorless commented 3 years ago

It's been a while since I worked on getting swing and javaFX to play nice. There might be some strange side effects with that code snipped but as you said it appears to work. I would also lean towards your thought that is some sort of bug/issue with the two UI libraries. If you don't need a very big toolbar, it might be easier to build the buttons in javaFX and call the swing controller as needed.

Kayeeec commented 3 years ago

I am also having trouble with the free text annotation in my JavaFX/TornadoFX application. At first, the free text could not gain focus at all, and I used the request focus on mouse entered workaround from @realumhelp's comment.

But I use icepdf to view and annotate multiple PDF files simultaneously. The PDFs are open as tabs. Opening a single PDF twice is handled so that it cannot happen.

The problem is, that the free text annotation works only in the first opened tab. In the others, the text box appears but I cannot write in it. Closing other tabs often fixes it. But sometimes the annotation does not work in any opened tab. Also the annotation selection using the select tool does not work properly as it does not draw the select rectangle around the annotation.

Each tab creates a completely separate icepdf swing component mostly according to the JavaFX guide from the wiki (the controller, the swing node with the viewer…) and each swing node has its own onMouseEntered listener, which seems to fire correctly.

SwingNode with the viewerPanel is embedded in a centerof a BorderPane. The annotation toolbar buttons are generated individually using the SwingViewBuilder factory and are added in a FlowPane embedded at the top of the BorderPane.

I tried creating custom JavaFX button and call

...
try {
   swingControler.setDocumentToolMode(17)
} finally {
   swingControler.setDisplayTool(17)
}

from SwingUtilities.invokeAndWait, as per @pcorless 's comment, but it did not work. Though I am not sure I called toggling the free text annotation correctly.

So how could this be fixed, is there any progress on this issue?

The full code for my View component that is embedded in a tab can be viewed here . The project can be downloaded and run. All code related to the viewer is in the diagram package.