Change the color of a single data cell in a TableView

前提是你 提交于 2019-12-23 03:22:02

问题


I have a column in a table view and i want: Green font if i write "OK", red font if i write "KO". The problem is that i try to modify this value in the method "setCellFactory" but it doesn't work because the String have all the same color (red or green) that is the color of the last String... For istance if my last value is "KO" all the String in my column will be red. How can i do? Thank you for the help!

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;
            }
        });

回答1:


So...The final code is:

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);
                    }
                }
            };


        }
    });



回答2:


It's hard to know what's going wrong without full code but I'll post a very simple program that works properly.

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();
    }
}

Maybe where you call match you're making a mistake. Item is the object in the cell so just check that. Don't forget to call super but your code worked without it.



来源:https://stackoverflow.com/questions/23872477/change-the-color-of-a-single-data-cell-in-a-tableview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!