我意识到我的JavaFX应用程序中的所有tableView和treeView都会在它们聚焦后立即选择第一行.因此,即使单击相应的滚动条,也会选择第一行.既然我当然不想有这样的行为,我想问一下,如果有办法避免这种行为?
编辑:我添加了一个简单的例子,创建了两个简单的列表.只要单击一个空行,就会选择第一行.
编辑2:为代码添加了两个小侦听器,以便在不重新启动应用程序的情况下重复出现问题.
package testlistview;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ListView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
public class TestListView extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
HBox root = new HBox();
root.setSpacing(10);
Scene primaryStageScene = new Scene(root);
primaryStage.setScene(primaryStageScene);
ListView<String> l1 = new ListView();
l1.getItems().addAll("a", "b", "c", "d", "e", "f", "g");
ListView<String> l2 = new ListView();
l2.getItems().addAll("1", "2", "3", "4", "5", "6", "7");
root.getChildren().addAll(l1, l2);
l1.setOnMouseClicked((MouseEvent e) -> …Run Code Online (Sandbox Code Playgroud)