1

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;
            }
        });
Peppalvino
  • 23
  • 1
  • 6

2 Answers2

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


        }
    });
Peppalvino
  • 23
  • 1
  • 6
  • I changed my code to be as close to yours as possible and it worked. I fixed it up a bit and it still worked. My guess is `result.match` is your problem. Use `item` instead. – brian May 27 '14 at 15:11
0

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.

enter image description here

brian
  • 10,619
  • 4
  • 21
  • 79
  • Same issue... The color of all the Strings is the same of the last String. (E.G. I have a "OK" in the last String, all the Strings are green) and now whem i try to stop and restart the thread that modify the grapic i have a lot of Exceptions... – Peppalvino May 27 '14 at 10:14
  • I updated the code. It's hard to say what's going wrong without the full code. I don't see any threads either. – brian May 27 '14 at 12:15