gpt4 book ai didi

JavaFX:在 onEditCommit 函数中使用 ListView.edit(int itemIndex)

转载 作者:行者123 更新时间:2023-11-30 06:31:18 24 4
gpt4 key购买 nike

我正在尝试在 javaFX 中创建一个易于用户使用的可编辑 ListView 。为此,我使用了编辑提交功能。它通常非常擅长做我想做的事情,但我遇到了一个问题。每次用户编辑最后一个项目时,我希望它创建另一个项目并开始编辑它。这看起来好像是微不足道的,但是我在 setOnEditCommit 函数中运行 dit 函数时遇到了问题。这是我的代码:

 personList.setOnEditCommit(new EventHandler<ListView.EditEvent<String>>() {
@Override
public void handle(ListView.EditEvent<String> t) {
personList.getItems().set(t.getIndex(), t.getNewValue());

if(personList.getSelectionModel().getSelectedIndices().contains(personList.getItems().size()-1)) {
int personNumber = personList.getItems().size() + 1;
personList.getItems().add("person " + personNumber);
personList.edit(personList.getItems().size()-1); //the line that is not working.
}
}

});

注意:personlist 是一个 ListView

无论如何,是否可以在 setOnEditCommit 上的集合中运行编辑功能。谢谢。

最佳答案

基本问题是 commitHandler 的重新编辑被取消。有许多(且复杂的)路径可以触发取消,其中之一似乎是“修复”JDK-8094887这相当粗鲁地取消了对数据更改的任何编辑。

但这不可能是全部故事,因为在单元配置期间触发重新编辑后收到的取消,大致:

indexedCell.updateIndex(index)
-> listCell.indexChanged(oldIndex, newIndex)
-> listCell.updateFocus()
-> node.setFocused(xx) //for some reason xx is false
-> cell.focusedListener
-> cancelEdit()

运行下面的示例,编辑单个项目,按 ENTER 会产生输出:

edit start: 0   // start edit on last row
edit commit: 0 // commit the edit by items.set(...)
edit cancel: -1 // cancel triggered by modification
edit start: 1 // re-edit the added item
edit cancel: -1 // cancel triggered by cell config

我能想到的唯一出路是非常脆弱的:像您一样开始重新编辑,然后启动一个计时器,“等待”直到所有内部配置完成并再次编辑。显然,我们不想在生产代码中做任何事情;)

import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.scene.Scene;
import javafx.scene.control.ListView;
import javafx.scene.control.cell.TextFieldListCell;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import javafx.util.Duration;

/**
* Trying to add item/start edit on new item in commitHandler:
* https://stackoverflow.com/q/46047134/203657
*
* one last try: use timer to start editing some time later ..
*/
public class ListViewAutoEditInHandler extends Application {

private ListView<String> simpleList;
private int expectedEditIndex = -1;
private Timeline editTimer;

/**
* Callback for editTimer. Implemented to scroll to and force
* edit of cell at expectedEditIndex.
*/
private void checkEdit() {
if (expectedEditIndex < 0) return;
if (expectedEditIndex == simpleList.getEditingIndex()) {
expectedEditIndex = -1;
return;
}
int index = expectedEditIndex;
expectedEditIndex = -1;
simpleList.scrollTo(index);
simpleList.edit(index);
}

@Override
public void start(Stage primaryStage) {
editTimer = new Timeline(new KeyFrame(Duration.millis(100), ae -> checkEdit() ));
simpleList = new ListView<>(FXCollections.observableArrayList("Item1"));
simpleList.setEditable(true);

simpleList.setCellFactory(TextFieldListCell.forListView());

simpleList.setOnEditStart(t -> p("edit start: " + t.getIndex()));
simpleList.setOnEditCancel(t -> p("edit cancel: " + t.getIndex()));
simpleList.setOnEditCommit(t -> {
p("edit commit: " + t.getIndex());
// any modification of the items will trigger a cancel
simpleList.getItems().set(t.getIndex(), t.getNewValue());
// p("editing? " + simpleList.getEditingIndex());
if (t.getIndex() == simpleList.getItems().size() - 1) {
expectedEditIndex = t.getIndex() + 1;
simpleList.getItems().add("newItem");
simpleList.getSelectionModel().select(expectedEditIndex);
simpleList.edit(expectedEditIndex);
// ... so we start a timer to force
// uncomment for a brittle solution ;)
// editTimer.playFromStart();
} else {
// reset .. a bit paranoid here ;)
expectedEditIndex = -1;
editTimer.stop();
}

});


BorderPane root = new BorderPane(simpleList);
Scene scene = new Scene(root, 300, 250);

primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}

private static void p(String t) {
System.out.println(t);
}
public static void main(String[] args) {
launch(args);
}
}

关于JavaFX:在 onEditCommit 函数中使用 ListView.edit(int itemIndex),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46047134/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com