gpt4 book ai didi

java - 如何使 ListView 可选择但不可编辑

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

所以我正在编写一个 javafx 应用程序,我需要能够从 ListView 中选择单元格(用于复制粘贴目的),但我不想让它可编辑,我的意思是,内容无法更改除非我想(例如,通过按钮允许它)。

所以我有以下代码:

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.StackPane;
import javafx.stage.Stage;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Main extends Application {

@Override
public void start(Stage primaryStage) throws Exception{
List<String> contacts = new ArrayList<>(Arrays.asList("968787522","3424234234","2343234324"));
ListView<String> contactsList = new ListView();
contactsList.setItems(FXCollections.observableArrayList(contacts));
//this gives me the ability to edit the row as text field but I want this text field to not be editable
contactsList.setCellFactory(TextFieldListCell.forListView());
StackPane pane = new StackPane();
pane.getChildren().add(contactsList);
primaryStage.setScene(new Scene(pane, 300, 275));
primaryStage.show(); }


public static void main(String[] args) {
launch(args);
}
}

如果我将“contactsList”设置为不可编辑,我将无法编辑,也无法选择。

如您所见(下图),我正在编辑单元格,但我希望能够选择文本(而不是项目),但我不希望能够删除字符(可选择文本但不可编辑)。 enter image description here

最佳答案

因此,在经过大量研究和阅读 API 后,我想出了一个解决方案。这正是我想做的。如果有人需要,这里是演示 ;)所以这个想法是,每次我们想要选择一行的内容时,我们需要选择该行,获取 textField 并将编辑设置为 true 或 false,(每次)。因此,在我制作的演示中,我放置了一个按钮,以便您可以将编辑切换为 true 或 false,以确保它正在运行,以及运行情况如何。

干杯。

我评论了一些代码以便更好地理解,如果您对此有任何疑问,请告诉我。

package sample;

import com.sun.javafx.scene.control.skin.VirtualFlow;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.cell.TextFieldListCell;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


public class Main extends Application {
private boolean editable = false;

public static IndexedCell getCell(final Control control, final int index) {
return getVirtualFlow(control).getCell(index);
}

public static VirtualFlow<?> getVirtualFlow(Control control) {
Group group = new Group();
Scene scene = new Scene(group);
Stage stage = new Stage();

if(control.getScene() == null) {
group.getChildren().setAll(control);
stage.setScene(scene);
stage.show();
}

VirtualFlow<?>flow = (VirtualFlow<?>) control.lookup("#virtual-flow");
return flow;
}

public void setEditable(ListView contactsList){
//this needs to be done since we need to run our code after the text field was rendered
//so we need to invoke our code after this happens, if not it will throw a null pointer...
Platform.runLater(() -> {
//this is one of the most important guys because javafx api says that
//TextFieldListCell.forListView() allows editing of the cell content when the cell is double-clicked,
// or when {@link ListView#edit(int)} is called.
int rowIndex = contactsList.getSelectionModel().getSelectedIndex();
contactsList.edit(rowIndex);
ListCell rootCell = (ListCell) getCell(contactsList, rowIndex);
TextField textField = (TextField) rootCell.getGraphic();
textField.setEditable(editable);
});
}

@Override
public void start(Stage primaryStage) throws Exception{
List<String> contacts = new ArrayList<>(Arrays.asList("968787522","3424234234","2343234324"));
ListView<String> contactsList = new ListView();
contactsList.setItems(FXCollections.observableArrayList(contacts));
contactsList.setEditable(true);

//this gives me the ability to edit the row as text field but I want this text field to not be editable
contactsList.setCellFactory(TextFieldListCell.forListView());
contactsList.setOnEditStart(e -> {
setEditable(contactsList);
});

StackPane pane = new StackPane();
Button editBtn = new Button("Toggle edit");
editBtn.setOnAction(event -> {
editable = !editable;
editBtn.setText("Editing = " + editable);
//to cancel any editing that might be occuring
contactsList.getSelectionModel().clearSelection();
});
pane.getChildren().addAll(contactsList,editBtn);
primaryStage.setScene(new Scene(pane, 300, 275));
primaryStage.show();
}

public static void main(String[] args) {
launch(args);
}
}

关于java - 如何使 ListView 可选择但不可编辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42989477/

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