更改TableView中单个数据单元格的颜色

时间:2014-05-26 14:33:42

标签: css javafx tableview cell fxml

我在表格视图中有一列,我希望: 绿色字体,如果我写" OK",红色字体,如果我写" KO"。 问题是我尝试在方法" setCellFactory"中修改此值。但它不起作用,因为String具有与最后一个String的颜色相同的颜色(红色或绿色)...如果我的最后一个值是" KO"我列中的所有字符串都是红色的。 我能怎么做? 谢谢你的帮助!

reader.getSampleController().xmlMatch.setCellFactory(new Callback<TableColumn, TableCell>() {
            @Override
            public TableCell call(TableColumn param) {
                TableCell cell = new TableCell() {
                    @Override
                    public void updateItem(Object item, boolean empty) {
                        if (item != null) {
                            setText(item.toString());
                        }

                    }
                };

                cell.setAlignment(Pos.CENTER);

                if (result.match().equals("OK")) {
                    cell.setStyle("-fx-text-fill: green; -fx-font-weight:bold;");
                } else if (result.match().equals("N/A")){
                        cell.setStyle("-fx-text-fill: black; -fx-font-weight:bold;");
                }else{
                    cell.setStyle("-fx-text-fill: red; -fx-font-weight:bold;");
                }

                return cell;
            }
        });

2 个答案:

答案 0 :(得分:1)

所以......最终的代码是:

reader.getSampleController().xmlMatch.setCellFactory(new Callback<TableColumn<String, String>, TableCell<String, String>>() {
        @Override
        public TableCell call(TableColumn p) {
            return new TableCell<String, String>() {
                @Override
                public void updateItem(final String item, final boolean empty) {
                    super.updateItem(item, empty);
                    if (item != null) {
                        setText(item);
                        setAlignment(Pos.CENTER);
                        //setStyle("");

                        if (item.equals("OK")) {
                            setStyle("-fx-text-fill: green; -fx-font-weight:bold;");
                        }
                        else if (item.equals("N/A")) {
                            setStyle("-fx-text-fill: black; -fx-font-weight:bold;");
                        }
                        else if (item.equals("KO") ) {
                            setStyle("-fx-text-fill: red; -fx-font-weight:bold;");
                        }
                        else setStyle("");


                    } else {
                        setText(null);
                    }
                }
            };


        }
    });

答案 1 :(得分:0)

如果没有完整的代码,很难知道出了什么问题,但我会发布一个非常简单的程序,可以正常运行。

import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.stage.Stage;
import javafx.util.Callback;

public class TableColors extends Application {

    @Override
    public void start(Stage primaryStage) {
        TableView<String> tv = new TableView<>();
        TableColumn<String, String> tcName = new TableColumn("name");
        ObservableList<String> items = FXCollections.observableArrayList("OK","KO","N/A","Normal");
        tv.setItems(items);
        tv.getColumns().add(tcName);
        tcName.setCellValueFactory((p) -> new SimpleStringProperty(p.getValue()));
        tcName.setCellFactory(new Callback<TableColumn<String, String>, TableCell<String, String>>() {
            @Override
            public TableCell call(TableColumn p) {
                return new TableCell<String, String>() {
                    @Override
                    public void updateItem(final String item, final boolean empty) {
                        super.updateItem(item, empty);//*don't forget!
                        if (item != null) {
                            setText(item);
                            setAlignment(Pos.CENTER);
                            if (item.equals("OK")) {
                                setStyle("-fx-text-fill: green; -fx-font-weight:bold;");
                            }
                            else if (item.equals("N/A")) {
                                setStyle("-fx-text-fill: black; -fx-font-weight:bold;");
                            }
                            else if (item.equals("KO")) {
                                setStyle("-fx-text-fill: red; -fx-font-weight:bold;");
                            } 
                            else setStyle("");
                        } else {
                            setText(null);
                        }
                    }
                };
            }
        });

        tv.setOnMouseClicked((MouseEvent event) -> {
            if (event.getButton()==MouseButton.PRIMARY) items.add("OK");
            else items.add("KO");
        });

        Scene scene = new Scene(tv, 300, 250);
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

也许在你打电话的地方,你犯了一个错误。 Item是单元格中的对象,所以只需检查一下。不要忘记打电话给超级,但你的代码没有它。

enter image description here