我有一个带有String项目的ListView。我为Button创建了一个重写方法setOnMouseClicked。当我们推送它时,ListView创建新项目,我们可以编辑它的名称。但是,我需要这个物品的名字。所以我不能这样做...也许我应该使用theads?希望能为您解答。
主要问题-编辑后如何获取项目名称?
代码:
@Override
public void start(Stage stage) throws Exception {
File file = new File("D:\\");
String[] filesName = file.list();
ObservableList<String> Langs = FXCollections.observableArrayList(filesName);
ListView<String> langsListView = new ListView<String>(Langs);
Button BtnBack = new Button();
Button CreateDir = new Button();
CreateDir.setText("New Directory");
BtnBack.setText("Back");
ArrayList<String> z = new ArrayList<String>();
z.add(file.getPath());
CreateDir.setOnMouseClicked(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
langsListView.setEditable(true);
langsListView.getItems().add(0,"");
langsListView.setCellFactory(TextFieldListCell.forListView());
langsListView.layout();
langsListView.edit(0);
langsListView.setEditable(false);
//I need that next code is working but it isn't... don't know why.
String newDir = new String();
for (String s : z)
newDir += s;
File newfile = new File(newDir+langsListView.getItems().get(0));
System.out.println(newDir + langsListView.getItems().get(0));
}
});
FlowPane BtnPane = new FlowPane(10,10,BtnBack, CreateDir);
FlowPane root = new FlowPane(BtnPane,langsListView);
Scene scene = new Scene(root, 600, 500);
stage.setScene(scene);
stage.setTitle("ListView in JavaFX");
stage.show();
}