JavaFX - 控制项最适合我的需要?

JavaFX - Control Item best suited for my needs?

我想要一个 Box 来保存 String 个对象的列表。不是 ChoiceBoxComboBox 等,因为无需单击打开框即可显示它。用户可以通过在下面的 TextField 中输入新条目并按 Enter 来添加新条目。控制项也不能是 TextField,因为您无法单击 TextField 的各个行。在此应用程序中,我希望能够双击任何项目以将其删除。如果这真的很容易,那么也许双击可以让我编辑条目?

这里有人可以提出建议吗?在我所知道的所有控件类型中,我想不出一个。

您可以使用 ListView with a TextField. It's rather easy to make those cells editable, since there is already a way to create a cell factory easily using TextFieldListCell.forListView

ListView<String> lv = new ListView<>();

// Make cells editable
lv.setEditable(true);
lv.setCellFactory(TextFieldListCell.forListView());

// print selected item to the console
lv.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
    System.out.println("Selected Item: "+ newValue);
});

TextField tf = new TextField();
// add new text from the textfield as item to the listview
tf.setOnAction((event) -> {
    lv.getItems().add(tf.getText());
    tf.clear();
});

VBox root = new VBox(lv, tf);
// TODO: add root to scene