滚动窗格内容在窗格中移动。锁定内容

时间:2014-09-30 09:11:03

标签: javafx javafx-8

这是我的示例代码,在我的项目中我使用了滚动窗格,但是我在节点外单击并使用箭头键将节点移动到Center,left,right,bottom.how将节点锁定在同一位置,

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;
    }

}

我参考了这个示例代码:JavaFX: scrolling vs. focus traversal with arrow keys

1 个答案:

答案 0 :(得分:0)

@James_D告诉“滚动窗格的默认行为是,如果它具有键盘焦点,则光标(箭头)键将使其滚动”。为此解决方案使用该事件Ref JavaFX: scrolling vs. focus traversal with arrow keys < / p>

/*
 * 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();
                    }
                }
            });
        }
    }

}