0

This is my sample code, In my project I have used scroll pane, but i am click outside of node and use arrow keys that nodes are move to Center,left,right,bottom.how to lock the node in same position,

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

/**
 *
 * @author reegan
 */
public class ComboBoxEditable extends Application {

    Node sub;

    @Override
    public void start(Stage primaryStage) {

        ComboBox mainCombo = new ComboBox(listofCombo());
        Button  save = new Button("Save");
        sub = new ComboBox(listofCombo());
        HBox root = new HBox(20);
        root.getChildren().addAll(mainCombo, sub,save);
        ScrollPane pane = new ScrollPane(root);
        mainCombo.getSelectionModel().selectedItemProperty().addListener(new ChangeListener() {

            @Override
            public void changed(ObservableValue observable, Object oldValue, Object newValue) {
                if (newValue == "Others") {
                    sub = new TextField();
                } else {
                    sub = new ComboBox(listofCombo());
                }
                root.getChildren().remove(1);
                root.getChildren().add(1, sub);
            }
        });
        save.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                System.out.println(mainCombo.getValue());
                if(sub.getClass() == ComboBox.class) {
                    ComboBox sub1 = (ComboBox)sub;
                    System.out.println(sub1.getValue());
                } else {
                    TextField field = (TextField)sub;
                    System.out.println(field.getText());
                }
            }
        });


        Scene scene = new Scene(pane, 300, 250);

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

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

    public ObservableList listofCombo() {
        ObservableList<String> list = FXCollections.observableArrayList();
        for (int i = 0; i < 10; i++) {
            list.add(String.valueOf("Hello" + i));
        }
        list.add("Others");
        return list;
    }

}

I am ref this example code :JavaFX: scrolling vs. focus traversal with arrow keys

Community
  • 1
  • 1
Reegan Miranda
  • 2,879
  • 6
  • 43
  • 55
  • Neither do I see a `ScrollPane` in your question, nor is it clear to me what you are trying to ask. Please edit your question to be more specific about your problem – ItachiUchiha Sep 30 '14 at 10:57
  • @ItachiUchiha I am edit my question. Scroll Pane content moving in respect using up and down side key using.how to block the movement. – Reegan Miranda Sep 30 '14 at 11:47
  • I still don't get it what do you mean by `lock the node in same position`. Do you want the scrollPane to not move when you press arrow keys ? – ItachiUchiha Sep 30 '14 at 11:58
  • @ItachiUchiha,Please click outside of node.and press up and down arrow keys.the node will move.but I d'not move nodes – Reegan Miranda Sep 30 '14 at 12:14
  • The default behavior for a scroll pane is that, if it has keyboard focus, the cursor (arrow) keys will cause it to scroll. Are you saying you want to disable this? If so, doesn't the answer you linked already show you how? – James_D Sep 30 '14 at 12:29
  • @James_D Ok thanks...for your answer. I will see that solution.and implement that answers, in my project – – Reegan Miranda Sep 30 '14 at 13:02

1 Answers1

0

@James_D told "The default behavior for a scroll pane is that, if it has keyboard focus, the cursor (arrow) keys will cause it to scroll".So consume that event Ref for this solution JavaFX: scrolling vs. focus traversal with arrow keys

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package comboboxeditable;

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

/**
 *
 * @author reegan
 */
public class ComboBoxEditable extends Application {

    Node sub;

    @Override
    public void start(Stage primaryStage) {

        ComboBox mainCombo = new ComboBox(listofCombo());
        Button save = new Button("Save");
        sub = new ComboBox(listofCombo());
        HBox root = new HBox(20);
        root.getChildren().addAll(mainCombo, sub, save);
        ScrollInterceptor pane = new ScrollInterceptor(root);
        mainCombo.getSelectionModel().selectedItemProperty().addListener(new ChangeListener() {

            @Override
            public void changed(ObservableValue observable, Object oldValue, Object newValue) {
                if (newValue == "Others") {
                    sub = new TextField();
                } else {
                    sub = new ComboBox(listofCombo());
                }
                root.getChildren().remove(1);
                root.getChildren().add(1, sub);
            }
        });
        save.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                System.out.println(mainCombo.getValue());
                if (sub.getClass() == ComboBox.class) {
                    ComboBox sub1 = (ComboBox) sub;
                    System.out.println(sub1.getValue());
                } else {
                    TextField field = (TextField) sub;
                    System.out.println(field.getText());
                }
            }
        });

        Scene scene = new Scene(pane, 300, 250);

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

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

    public ObservableList listofCombo() {
        ObservableList<String> list = FXCollections.observableArrayList();
        for (int i = 0; i < 10; i++) {
            list.add(String.valueOf("Hello" + i));
        }
        list.add("Others");
        return list;
    }

    private static class ScrollInterceptor extends ScrollPane {

        public ScrollInterceptor() {
            remapArrowKeys(this);
        }

        public ScrollInterceptor(Node content) {
            ScrollInterceptor.this.setContent(content);
            remapArrowKeys(this);
        }

        private void remapArrowKeys(ScrollPane scrollPane) {
            scrollPane.addEventFilter(KeyEvent.ANY, new EventHandler<KeyEvent>() {
                @Override
                public void handle(KeyEvent event) {
                    switch (event.getCode()) {
                        case UP:
                        case DOWN:
                        case LEFT:
                        case RIGHT:
                            event.consume();
                    }
                }
            });
        }
    }

}
Community
  • 1
  • 1
Reegan Miranda
  • 2,879
  • 6
  • 43
  • 55