我想要一个保存 String
对象列表的 Box。不是 ChoiceBox
、ComboBox
等,因为它需要显示而无需单击打开框。用户可以通过在下面的 TextField
中输入新条目并按 Enter
来添加新条目。控件项也不能是 TextField
,因为您无法单击 TextField
的各个行。在此应用程序中,我希望能够双击任何项目将其删除。如果这真的很容易,那么也许双击可以让我编辑该条目?
这里有人可以提出建议吗?在我所知道的所有控件类型中,我想不出任何一件事。
最佳答案
您可以使用 ListView
与 TextField
。使这些单元格可编辑相当容易,因为已经有一种方法可以使用 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
关于JavaFX - 最适合我的需求的控制项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34821204/