当列表单元的内部组件之一获得焦点时,如何自动选择它?

时间:2019-07-04 00:40:27

标签: java javafx

我有一个javafx listview,其中每个列表单元格包含带有多个节点的图形。这些节点之一是文本字段。当文本字段被赋予焦点(用户单击它)时,我希望它所在的列表单元也被选中。我还没有找到在Javafx中实现此目标的一般方法。

我能够通过某种方式进行硬编码来获得想要的信息,从而获得选择我想要的商品所需的所有信息,但是如果有更好的方法,我想避免这种情况。以下是我在更复杂的应用程序中需要做的事情...

父级皮肤= this.getParent()。getParent()。getParent()。getParent();

int index = skin.getParent()。getChildrenUnmodifiable()。indexOf(skin);

ListView listview =(ListView) skin.getParent()。getParent()。getParent()。getParent();

listview.getSelectionModel()。select(index);

这是一个希望可以说明问题的简单应用。任何帮助将不胜感激。

    package uitesting;

    import javafx.application.Application;
    import javafx.collections.FXCollections;
    import javafx.collections.ObservableList;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.control.ListCell;
    import javafx.scene.control.ListView;
    import javafx.scene.control.TextField;
    import javafx.scene.layout.AnchorPane;
    import javafx.scene.layout.VBox;
    import javafx.stage.Stage;

    public class testSelection extends Application {
        public static void main(String[] args){
            launch(args);
        }

        @Override
        public void start(Stage primaryStage) throws Exception {
            AnchorPane pane = new AnchorPane();
            ListView<String> viewer = new ListView<>();
            AnchorPane.setTopAnchor(viewer,5D);AnchorPane.setLeftAnchor(viewer,5D);
            AnchorPane.setBottomAnchor(viewer,5D);AnchorPane.setRightAnchor(viewer,5D);
            pane.getChildren().add(viewer); // Add listview to root pane
            ObservableList<String> viewItems = FXCollections.observableArrayList(
                    "Lorem Ipsum",
                    "I would like the cell this is located in to get selected when this text is clicked while the cell is not selected.",
                    "A third listcell");
            viewer.setItems(viewItems);
            // Modify the cell factory
            viewer.setCellFactory((e)->new myListCell());
            // create and start the stage
            primaryStage.setTitle("Test GUI");
            primaryStage.setScene(new Scene(pane, 600, 300));
            primaryStage.show();
        }
        static VBox createGraphic(String text){
            // Put together an example pane with a button and a textfield that will go into each listcell
            Button buttonA = new Button("A Button");
            TextField textField = new TextField(text);
            textField.setEditable(false);
            VBox root = new VBox();
            root.getChildren().addAll(buttonA,textField);
            return root;
        }
    }
    class myListCell extends ListCell<String>{
        @Override
        public void updateItem(String item, boolean empty) {
            super.updateItem(item, empty);
            if (item == null || empty) {
                setText(null);
                setGraphic(null);
            } else {
                setText(null);
                setGraphic(testSelection.createGraphic(item));
            }
        }
    }

0 个答案:

没有答案