how to change tableview's cells background dynamically - javafx

I want to change tableview's cells background color when their values is empty. Could you help me? thanks.
Below, an overview of my source code, but it doesn't work.
public class Cell extends TextFieldTableCell<Itemtest, String>{
public Cell(StringConverter<String> str){
super(str);
this.itemProperty().addListener((obs, oldValue, newValue)->{
if(newValue.isEmpty())
this.setBackground(new Background(new BackgroundFill(Color.RED, CornerRadii.EMPTY, Insets.EMPTY)));
});
}
#Override
public void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
setText(empty ? null : getString());
setGraphic(null);
}
private String getString(){
return getItem() == null ? "" : getItem().toString();
}
}

With your code you set the background to a color, if the item becomes "". You never change it back in case the item should change. Also the item being "" implies that the cell is not empty.
Furthermore you have already overwritten the updateItem method which is called when the item changed or the cell becomes empty and it should be used instead to update the background.
public class Cell extends TextFieldTableCell<Itemtest, String>{
public Cell(StringConverter<String> str){
super(str);
}
#Override
public void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
setText(empty ? null : getString());
setGraphic(null);
// is this really the check you want?
if (item != null && item.isEmpty()) {
this.setBackground(new Background(new BackgroundFill(Color.RED, CornerRadii.EMPTY, Insets.EMPTY)));
} else {
// change back to empty background
this.setBackground(Background.EMPTY);
}
}
private String getString(){
return getItem() == null ? "" : getItem().toString();
}
}

Related

JavaFx How to make checkbox visible in tableColumnCell using cellFactory

I want to add checkBox to tableCell but i dont know what to do to make checkBox visible when i use .setCellFactory
I want to achive this with cellFactory function ---> checkBoxVisible
when i want to make use of cellFactory checkBox are not visible
----> wrong effect
window1.setCellFactory(new BooleanColorCellFactory());
window2.setCellFactory(new BooleanColorCellFactory());
This is BooleanColorCellFactory Class
#Override
public TableCell<Server, CheckBox> call(TableColumn<Server, CheckBox> param) {
return new TableCell<Server, CheckBox>(){
#Override
protected void updateItem(CheckBox item, boolean empty) {
super.updateItem(item, empty);
// if(!empty) {
// setVisible(true);
// setEditable(true);
// getChildren().add(item);
// setText(item.toString());
// if(item.isSelected())
// setStyle(" -fx-background-color: Green");
// else
// setStyle(" -fx-background-color: RED");
}
// }
};
}
}
I have tried some things but nothing was working.
What i need to add in BooleanColorCellFactory to make this work ?
UPDATE:
So i was playing around and i manage to get step closer to solution by adding this into BooleanColorCellFactory Class
if(!getChildren().contains(item))
getChildren().add(item);
but it is buggy and dont looks well and its added after i start scrolling(which is weird behavior for me)--> click
You shouldn't put a Node inside the item class unless you really need to. Furthermore never access the children of a Control directly unless you're writing a Skin for this Control.
You should instead add a BooleanProperty to the Server class:
private final BooleanProperty window1 = new SimpleBooleanProperty();
public boolean isWindow1() {
return window1.get();
}
public void setWindow1(boolean value) {
window1.set(value);
}
public BooleanProperty window1Property() {
return window1;
}
TableColumn<Server, Boolean> window1;
Callback<TableColumn<Server, Boolean>, TableCell<Server, Boolean>> factory = new BooleanColorCellFactory();
// cellValueFactory returns property
window1.setCellValueFactory(new PropertyValueFactory<>("window1"));
window1.setCellFactory(factory);
...
window2.setCellFactory(factory);
#Override
public TableCell<Server, Boolean> call(TableColumn<Server, Boolean> param) {
return new TableCell<Server, Boolean>(){
private final CheckBox checkBox = new CheckBox();
{
checkBox.selectedProperty().addListener((o, oldValue, newValue) -> {
// save new value in row item
WritableValue<Boolean> property = (WritableValue<Boolean>) getTableColumn().getCellObservableValue​(getIndex());
property.setValue(newValue);
});
}
#Override
protected void updateItem(Boolean item, boolean empty) {
super.updateItem(item, empty);
if (empty || item == null) {
setGraphic(null);
setStyle(null);
} else {
setGraphic(checkBox);
checkBox.setSelected(item);
setStyle(item ? "-fx-background-color: Green" : "-fx-background-color: RED");
}
}
};
}
Procede accordingly for window2

JavaFX: add contextmenu to TableView cell

I'm trying to add a context menu for every individual cell in a column, that gets activated by rightclicking on the cell. The menuitems would also depend on the cell clicked.
I would think using a cell factory would be the best way, but I cant figure out how to do it.
Edit: This is my implementation for one of my columns. setText in updateItem() adds the text to my column, but I want to add it as a label, how should I do that?
moColumn = new TableColumn<>("Maandag");
moColumn.setSortable(false);
moColumn.setCellValueFactory(new PropertyValueFactory<>("Mo"));
getColumns().add(moColumn);
moColumn.setCellFactory(new Callback<TableColumn<RowData, Label>, TableCell<RowData, Label>>() {
#Override
public TableCell<RowData, Label> call(TableColumn<RowData, Label> col) {
final TableCell<RowData, Label> cell = new TableCell<RowData, Label>() {
#Override
protected void updateItem(Label item, boolean empty) {
super.updateItem(item, empty);
if(empty){
setText(null);
} else {
setText(item.getText());
}
}
};
//cell.textProperty().bind(cell.getItem().textProperty());
cell.setOnMouseClicked(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent event) {
if(event.getButton() == MouseButton.SECONDARY){
ContextMenu contextMenu = new ContextMenu();
MenuItem menuItem = new MenuItem(cell.getText());
contextMenu.getItems().add(menuItem);
cell.setContextMenu(contextMenu);
}
}
});
return cell;
}
});
Edit: How can I right click on a cell in tableview in Javafx (fxml)?
I've found this, but the link to the blog isn't working anymore...
Changed:
#Override
protected void updateItem(Label item, boolean empty) {
super.updateItem(item, empty);
if(empty){
setText(null);
} else {
setText(item.getText());
}
}
To:
#Override
protected void updateItem(Label item, boolean empty) {
super.updateItem(item, empty);
if(empty){
setText(null);
} else {
setGraphic(item);
}
}

Javafx: Table Column commit on Button click

I have method that make a TableColumn editable that I made according to this tutorial, with the two changes recommended in this question witch are changing focusedProperty with setOnAction and add binding. The method is like this:
public static <T> void setAutoCompleteTableColumn(TableColumn<T,String> column, BiConsumer<T, String> field, String type, BiConsumer handleUpdates, List autoComplete){
column.setCellFactory(param -> {
return new TableCell<T, String>(){
private TextField textField;
#Override
public void startEdit() {
super.startEdit();
createTextField();
setText(null);
setGraphic(textField);
textField.selectAll();
}
#Override
public void cancelEdit() {
super.cancelEdit();
setText((String) getItem());
setGraphic(null);
}
#Override
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (empty) {
setText(null);
setGraphic(null);
} else {
if (isEditing()) {
if (textField != null) {
textField.setText(getString());
}
setText(null);
setGraphic(textField);
} else {
setText(getString());
setGraphic(null);
}
}
}
private void createTextField(){
textField = new TextField(getString());
textField.setMinWidth(this.getWidth() - this.getGraphicTextGap()* 2);
textField.setOnAction(event ->{
if(textField.getText() != ""){
commitEdit(textField.getText());
}
});
AutoCompletionBinding<T> binding = TextFields.bindAutoCompletion(textField,autoComplete);
}
private String getString() {
return getItem() == null ? "" : getItem().toString();
}
};
});
column.setOnEditCommit(event -> {
T model = event.getRowValue();
if (!event.getNewValue().isEmpty()) {
Cloner cloner = new Cloner();
final T oldModel = cloner.deepClone(model);
//Update the old value with the new value
field.accept(model, event.getNewValue());
//Retrieve changes
handleUpdates.accept(oldModel, model);
}
});
}
I use this method in the controller like this:
TableColumn<Purchase, String> nameColumn = new TableColumn<>();
setAutoCompleteTableColumn(nameColumn, (p,s) -> validatePurchaseEdit(p,s), "text", (o, n) -> updateTracker(o, n), appState.items);
I have a button click event that switch the TableView editable to false.
For some reason when this event is executed the current cell that I am editing does commit edit but an other cell(a random one) switches to edit mode. Can some one explain what is happening, and how can I make all cells commit edit?

Apply cell factory only at child level in treetableview

I have a treetableview with 4 level hierarchy. I want to have a column with cell factory as combobox. but this combobox should be visible only at the last child level and not the parent level. Is that possible to achieve?
Edit: I tried the following as per suggestions received.
ObservableList ADM = WSData.getADMObjectMapList();
tbAdmMap.setCellFactory(new Callback<TreeTableColumn<ProjectPlan, String>, TreeTableCell<ProjectPlan, String>>() {
#Override
public TreeTableCell<ProjectPlan, String> call(TreeTableColumn<ProjectPlan, String> param) {
TreeTableCell<ProjectPlan, String> cell = new TreeTableCell<ProjectPlan, String>() {
#Override
public void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
System.out.println(getItem());
if (getString().equals("")) {
setGraphic(null);
} else {
ComboBox<String> comboBox = new ComboBox(ADM);
comboBox.setValue(item);
setGraphic(comboBox);
}
}
private String getString() {
return getItem() == null ? "" : getItem();
}
};
return cell;
}
});
It is giving me the proper look as I want but not taking the cell into edit mode. As in the onCommitEdit method is not getting called.

How to change the row color based on data that can change in a tableview?

I am writing a podcast player and have a list of files that I display.
The user can double click an entry and that files play. If they select another one. I don’t know what has been selected in the list. The idea is to highlight the file that is playing in a different color. With still keeping the ability to select another file (don’t want to lose that functionality). Most audio players work on the same principle.
I had a look at this JavaFX tableview colors and this changes the color of the row based on data. But if the data changes the row color do not refresh as the data changes.
I have a table with cell factory and based on the variable "isSelectedFile" I set the cell style.
If the isSelectedFile=True at load time all is ok but once I change the data while the rows are loaded into the tableview it doesn't refresh.
My cell factory is as follows:
entryDisplayNameColumn.setCellFactory(new Callback<TableColumn<PlaylistEntry, String>, TableCell<PlaylistEntry, String>>() {
#Override
public TableCell call(TableColumn p) {
TableCell cell = new TableCell<Task, Object>() {
#Override
public void updateItem(Object item, boolean empty) {
super.updateItem(item, empty);
setText(empty ? null : getString());
setGraphic(null);
TableRow currentRow = getTableRow();
PlaylistEntry ple = currentRow == null ? null : (PlaylistEntry) currentRow.getItem();
if (ple != null) {
clearPriorityStyle();
setPriorityStyle(ple.isSelectedFile());
}
}
#Override
public void updateSelected(boolean upd) {
super.updateSelected(upd);
System.out.println("is update");
}
private void clearPriorityStyle() {
ObservableList<String> styleClasses = getStyleClass();
styleClasses.remove("priorityLow");
styleClasses.remove("priorityMedium");
styleClasses.remove("priorityHigh");
}
private void setPriorityStyle(boolean activeValue) {
if (activeValue) {
getStyleClass().add("priorityLow");
} else {
getStyleClass().add("priorityMedium");
}
}
private String getString() {
return getItem() == null ? "" : getItem().toString();
}
};
return cell;
}
});
My CSS:
.priorityLow {
-fx-control-inner-background: snow;
-fx-accent: derive(-fx-control-inner-background, -40%);
-fx-cell-hover-color: derive(-fx-control-inner-background, -20%);
}
.priorityMedium {
-fx-control-inner-background: #1d1d1d;
-fx-accent: derive(-fx-control-inner-background, -40%);
-fx-cell-hover-color: derive(-fx-control-inner-background, -20%);
}
try this its perfectly worked...
tablecol.setCellFactory(new Callback<TableColumn<CheckDo, String>, TableCell<CheckDo, String>>() {
#Override
public TableCell<CheckDo, String> call(TableColumn<CheckDo, String> p) {
return new TableCell<CheckDo, String>() {
#Override
public void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (!isEmpty())
this.setStyle("-fx-background-color:red");
this.getTableRow().setStyle("-fx-background-color:red");
setText(item);
}
}
};

Resources