1

Could you please advise in situation: I have ScrollPane and GridPane in it. In GridPane i have many buttons. When i focused one button by mouse and try to move focus by arrow keys, focus still stay on the same button and i only see that ScrollPane scrolling instead.

How can i disable scrolling by arrow keys for ScrollPane and send send that events to my child buttons. I try do some things like:

ScrollPane sp = new ScrollPane() {
        @Override
        public void requestFocus(){}
    };
    sp.addEventFilter(KeyEvent.ANY, (event) -> {
        if(event.getCode() == KeyCode.DOWN) {
            event.consume();
            javafx.event.Event.fireEvent(mainGrid, event);
        }
    });

But unfortunately that have not worked.

What else could i do? Many thanks, Roman

Romeo
  • 31
  • 2

2 Answers2

0

I have changed the way you consumed the event this will not block the button from getting the key event and also the way you traverse nodes is usually with the tab button but I have no clue what you have implemented. this will print out every key you press when the focus is on the button without scrolling

public class Main extends Application {

    @Override
    public void start(Stage stage) {
        VBox scrollPaneVbox = new VBox();

        Button button = new Button("Button");
        button.setOnKeyPressed(event -> System.out.println(event.getCode().toString()));

        ScrollPane scrollPane = new ScrollPane(scrollPaneVbox);
        scrollPane.setPannable(true);
        scrollPane.setPrefSize(150,150);
        scrollPane.setOnKeyPressed(event -> {
            if(event.getCode() == KeyCode.DOWN || event.getCode() == KeyCode.UP)
                event.consume();
        });

        int i = 0;
        while (i++!=21)
            scrollPaneVbox.getChildren().add(new Label("Label i:"+i));

        scrollPaneVbox.getChildren().add(button);

        Scene scene = new Scene(scrollPane, 200,150);

        stage = new Stage();
        stage.setScene(scene);
        stage.showAndWait();
    }



    public static void main(String[] args) { launch(args); }
}

Why I think this was not working is because the event first goes to the scrollpane and from there it is passed to the button and when you filter the event it doesn't pass the event on. But I have no facts to back this up if anyone else wants to elaborate or tell me im wrong I will remove this explanation

Matt
  • 3,052
  • 1
  • 17
  • 30
0

Thanks. I do some research and come to conclusion that event reach the button but something is blocking 'focus functionality' and focus don't move to next button (( But if i press 'tab' then focus moving! How enable moving by pressing arrow keys? See my code

package testjavafxscrollpane;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ScrollPane;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class TestJavaFXScrollPane extends Application {

@Override
public void start(Stage primaryStage) {
    Button btn0 = new Button("Btn0");
    btn0.setOnKeyPressed((event) -> {
        System.out.println("btn.KeyPressed; Code: " + event.getCode());
    });
    Button btn1 = new Button("Btn1");
    Button btn2 = new Button("Btn2");
    Button btn3 = new Button("Btn3");

    VBox root = new VBox();
    root.getChildren().addAll(btn0, btn1, btn2, btn3);

    ScrollPane scrollPane = new ScrollPane(root);
    scrollPane.setOnKeyPressed(event -> {
        //if(event.getCode() == KeyCode.DOWN || event.getCode() == KeyCode.UP)
            //event.consume();
    });

    Scene scene = new Scene(scrollPane, 100, 80);

    primaryStage.setTitle("Hello World!");
    primaryStage.setScene(scene);
    primaryStage.show();
}

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    launch(args);
}

}

Are there any thoughts how to fix 'focus problem'?

Romeo
  • 31
  • 2