Reputation: 23
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;
}
});
Upvotes: 1
Views: 2982
Reputation: 23
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);
}
}
};
}
});
Upvotes: 1
Reputation: 10989
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.
Upvotes: 0