- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以我正在编写一个 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”设置为不可编辑,我将无法编辑,也无法选择。
如您所见(下图),我正在编辑单元格,但我希望能够选择文本(而不是项目),但我不希望能够删除字符(可选择文本但不可编辑)。
最佳答案
因此,在经过大量研究和阅读 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/
我需要默认提供多项选择,这样用户就不必按住 ctrl。希望得到一些帮助,谢谢。 最佳答案 $("#selectable").selectable(); $("#selectable").on(
有没有一种简单的方法可以在 Cocoa 中创建一个可选择的 NSRect?需要一个可以选择并在单击鼠标后保持选中状态的矩形。 谢谢。 最佳答案 NSRect 只是一个具有位置和大小的结构。它不是一个实
我不知道我使用 jQuery UI 的 .selectable() API 是否完全错误。该脚本的预期结果是在选择黑框 (div) 期间简单地获得一些警报: http://jsfiddle.net/j
我试图创建两个可选择的 div。 div 应该表现得像按钮。我希望能够使用左右箭头选择其中一个 div。到目前为止,我能够使用这段代码实现这一点 document.addEventListener("
你好 StackOverFlow 社区, 我有一个标准的 UIBezierPath 如下: let π:CGFloat = CGFloat(M_PI) override func drawRect(r
我有一个 WPF TextBox白色背景。我给了它一些 Padding这样文本周围就有一个“边距”(类似于 MS Word 中的边距)。 然而,与 Word 不同的是,文本周围的这个空白区域不是“事
我的问题:我动态生成列表,并且我想让它们可供选择。 例如: function gen() { //list generation $('#blank').append(listHtml
我已经为我的 DatePicker View 设置了一个 minDate() 。现在的问题是我仍然可以从指定的最短日期之前的日期中进行选择。 我的java: long thirtyDaysInMill
我正在使用 SWT 库在 Eclipse 中创建一个向导页面。到目前为止,我已成功创建页面,但只有一个问题:- 未选择 SWT 复合 Material 。 我的向导页面包含一个 scrolledCom
https://www.youtube.com/watch?v=SR28jj4nTEkthis是链接][1] 我想了解如何使 TextView 可选,选择并复制此链接,但此链接已失效,无法通过此链接访
我一直在 Android Studio 中开发一个简单的计算器应用程序。结果窗口(放置数字用于计算的位置)我最初设置为 TextView。但是,我还没有找到在使用 TextView 时实现光标的方法。
我正在尝试获得类似于 Facebook 使用的设置(如果他们使用 UITextView)。我希望自动检测链接,但我不希望 UITextView 中的任何其他文本可选。因此,用户可以单击链接但无法选择任
我想要一个不可选择的 UITableViewCell,而其附属 View (在我的例子中是 UISwitch)是可编辑的。 问题是我还有另外两个单元,其中一个需要保持事件状态;这与 iOS 日历应用程
我想知道 jQuery UI Selectable 上是否有一个选项可以让我禁用 Ctrl+Click,但仍保持可拖动以进行多项选择。在我的项目中,我希望人们能够选择多个,但只能通过拖动,而不是通过
这个问题在这里已经有了答案: Jquery selectable for range selection (slider behaviour) (3 个答案) 关闭 2 年前。 我想使用类似于 jQ
所以我几个小时以来一直在寻找解决方案,但我就是找不到我的错误。我使用循环创建了 JCheckBoxes 并将其添加到滚动面板中。但如果我检查它们,isSelected() 总是返回 false。 im
我想让 EditText 或 Textview 在我的 android 项目中可选。项目适用于 android 4.0+。我添加了这个:txtView.setTextIsSelectable(true
所以我有一张 Accordion 风格的 table 。单击一行时,会显示五个单选按钮。然后,用户应该能够单击这些按钮。它成功显示了按钮,但是当单击按钮时,它不是突出显示所选按钮,而是缩小了行本身。我
我有一大堆可选择的对象。 您可以单击它们,然后它们的 ID 会添加到表单中,取消选择它们时,它会再次从表单中删除。 我有一个工作版本,直到我注意到有时如果你点击太快,id 会被添加多次。 为了禁止这种
当我们已经在 html 页面中有 onselectstart="return false;" 时,有谁知道如何使用 javascript 使特定的 div 可选(复制/粘贴)
我是一名优秀的程序员,十分优秀!