LolenFromPolen
LolenFromPolen

Reputation: 31

KeyEvent to change photos dosen't work, how to activate it?

I want to make switching photo program, using imageView and keyEvent, but when I try to use keyEvent it dosen't work, I was trying add scene.getRoot().requestFocus(); but didn't help, so my question is, why it dosen't work any how could I activate it?

MenuScreenController.java

    @FXML
public void getNewImageHandler(KeyEvent event) {
    System.out.println(singleFile.getName());
    imgFieldView.setOnKeyPressed(e -> {
        if (e.getCode() == KeyCode.N) {
            photoSwipCounter++;
            System.out.println("P clicked");
        } else if (e.getCode() == KeyCode.P) {
            photoSwipCounter--;
        }
    });

//        if (event.getCode().equals(KeyCode.N)) {
//            photoSwipCounter++;
//        }
//        if (event.getCode().equals(KeyCode.P)) {
//            photoSwipCounter--;
//        }

    if (photoSwipCounter < 0) {
        singleFile = selectedImgsList.get(selectedImgsList.size() - photoSwipCounter);
    } else {
        singleFile = selectedImgsList.get(photoSwipCounter);
    }

    image = new Image(singleFile.toURI().toString(),
            900, 400,
            true, true, true);
    imgFieldView.setImage(image);
}

As you can see I was trying many methods, by stream, or classic, but it dosen't make change.

MenuScreen.fxml

<Pane fx:id="menuPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="700.0" prefWidth="1000.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="screensaverfxml.Controllers.MenuScreenController">
   <children>
      <ImageView fx:id="imgFieldView" fitHeight="400.0" fitWidth="900.0" layoutX="66.0" layoutY="62.0" onKeyPressed="#getNewImageHandler" onMouseClicked="#imgDoubleClick" pickOnBounds="true" preserveRatio="true" />
   </children>
</Pane>

ScreenSaverFXML.java

    @Override
public void start(Stage primaryStage) throws Exception {
    FXMLLoader loader = new FXMLLoader(this.getClass().getResource("/screensaverfxml/fxmlConfig/MainScreen.fxml"));
    Pane mainPane = loader.load();
    Scene scene = new Scene(mainPane, 1000, 700);
    primaryStage.setScene(scene);
    primaryStage.setTitle("Screen Saver");

    primaryStage.show();

   scene.getRoot().requestFocus();
}

And ofc I have method to load first photo on imageView and it's work by load it from the folder to the list, but switching to next nope

Upvotes: 0

Views: 48

Answers (1)

Slaw
Slaw

Reputation: 45776

Only nodes that have the focus receive key events. Since you have scene.getRoot().requestFocus() it's the root that receives the events. However, you're adding your EventHandler to imgFieldView and since that node is a descendant of the root it never sees any key events. You need to either have your imgFieldView obtain the focus or add your handler to the root node instead.

Note: You can only request focus once the target Node is part of a Scene.

The other problem is your call to imgFieldView.setOnKeyPressed(...) in the getNewImageHandler method. When you add an event handler via FXML, for instance by using onKeyPressed="#getNewImageHandler", it sets the corresponding onXXX property of the node. In your case, it's setting the onKeyPressed property of imgFieldView. However, in that handler you also set that property which replaces the handler set by the FXMLLoader. Your new handler simply increments or decrements the photoSwipeCounter field but does nothing with the new value. Your method should look more like:

@FXML
public void getNewImageHandler(KeyEvent event) {
    if (event.getCode().equals(KeyCode.N)) {
        photoSwipeCounter++;
    }
    if (event.getCode().equals(KeyCode.P)) {
        photoSwipeCounter--;
    }

    // wrap around logic
    if (photoSwipeCounter < 0) {
        photoSwipeCounter = selectedImgsList.size() - 1;
    } else if (photoSwipeCounter >= selectedImgsList.size()) {
        photoSwipeCounter = 0;
    }

    singleFile = selectedImgsList.get(photoSwipeCounter);

    image = new Image(singleFile.toURI().toString(),
            900, 400,
            true, true, true);
    imgFieldView.setImage(image);
}

Upvotes: 3

Related Questions