【发布时间】:2020-12-18 10:12:29
【问题描述】:
我希望我的表格根据是否选中列或单击按钮来更改行的颜色。 我寻找解决方案,但它从来没有按照我想要的方式工作。我很高兴为您提供的每个关键字或帮助。也许我只是在寻找错误的东西?
我的代码如下所示:
public class Cellexample_presenter implements Initializable{
@FXML
TableColumn<ExampleData, Boolean> checkcol;
@FXML
TableView<ExampleData> testtable;
@Override
public void initialize(URL arg0, ResourceBundle arg1) {
checkcol.setText("Check");
ObservableList<ExampleData> exList = FXCollections.observableArrayList();
exList.add(new ExampleData(true));
exList.add(new ExampleData(false));
checkcol.setCellValueFactory(
new Callback<CellDataFeatures<ExampleData, Boolean>, ObservableValue<Boolean>>() {
@Override
public ObservableValue<Boolean> call(CellDataFeatures<ExampleData, Boolean> param) {
return param.getValue().checkProperty();
}
});
testtable.setItems(exList);
testtable.setEditable(true);
checkcol.setCellFactory(CheckBoxTableCell.forTableColumn(checkcol));
}
@FXML
private void clicked() {
ObservableList<ExampleData> tabledata = testtable.getItems();
for (ExampleData e : tabledata) {
System.out.println(e.isCheck());
}
}
}
我找到了另一个 setCellFactory 的解决方案,但是当我已经有一个 setCellFactory 并且我无法合并时,我似乎无法工作。此外,仍然缺少与
更改Checkbox。
我遇到的另一个解决方案是PseudoClass,但这也需要另一个setCellFactory
method private void clicked() 我用来检查我的对象中的数据是否真的改变了。
我还希望作为另一个功能单击按钮并更改具有错误数据的行的颜色。
我希望有一种方法可以遍历表格,检查数据,如果某些东西不等于其他东西。它改变颜色。
像这样的伪代码
for (tablerow row: table)
if (row.ischecked()) {
row.changecolor();
}
这是用于将所有内容组合在一起的 .FXML:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.Pane?>
<AnchorPane prefHeight="254.0" prefWidth="700.0" xmlns="http://javafx.com/javafx/11.0.1"
xmlns:fx="http://javafx.com/fxml/1"
fx:controller="pathToFXML/Cellexample_presenter">
<children>
<Pane layoutX="-269.0" prefHeight="315.0" prefWidth="700.0" AnchorPane.bottomAnchor="0.0"
AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<children>
<TableView fx:id="testtable" layoutX="142.0" layoutY="27.0" prefHeight="200.0"
prefWidth="340.0">
<columns>
<TableColumn fx:id="checkcol" prefWidth="75.0" text="C1" />
</columns>
</TableView>
<Button fx:id="checkbtn" layoutX="70.0" layoutY="36.0" mnemonicParsing="false"
onAction="#clicked" text="isChecked" />
</children>
</Pane>
</children>
</AnchorPane>
【问题讨论】:
-
没有看到你做任何事情来改变颜色..按照你(说)找到的例子;)回来minimal reproducible example 展示它们如何不按预期工作。不:循环遍历行是完全错误的方法(行被虚拟化,这意味着它们被重复使用!)