JavaFX ListView mirroring content at the end - javafx

When I add new item to my ListView, it starts mirroring all the content at the end it's layout (it's not clickable) does somebody know what can cause it? Thanks.

Ok, I found the problem.
I have the list of objects and want it to display the title of object as list node text.
The problem is that I wrongly defined the custom cell factory.
tagListView.setCellFactory(param -> new ListCell<>() {
#Override
protected void updateItem(StoreTagVoImpl item, boolean empty) {
super.updateItem(item, empty);
if (!empty) {
setText(item.getTitle());
}
}
});
But it should be as this:
tagListView.setCellFactory(param -> new ListCell<>() {
#Override
protected void updateItem(StoreTagVoImpl item, boolean empty) {
super.updateItem(item, empty);
if (item == null || empty) {
setText(null);
} else {
setText(item.getTitle());
}
}
});

Related

Does Cell Factory supports two different types of objects?

I'm populating my ListView with two different types of objects, and I would like to rename all the items inside the ListView. I'm using the following code to rename all the cells that containing items from a specific kind of object.
public void listViewSetCellFactory() {
listView.setCellFactory(lv -> new ListCell<Banana>() {
#Override
public void updateItem(Banana item, boolean empty) {
super.updateItem(item, empty);
if (empty) {
setText(null);
} else {
String text = item.getBananaName();
setText(text);
}
}
});
}
But since my ListView contains two different types ob objects (Bananas and Grapefruits). How can I rename the cells that contains grapefruits too using the same event?
It would be best to move the method returning the name to a supertype and using the same method for all names. To include the type name in the property name just results in longer identifiers without any additional benefit (banana.getName() would be just as easy to understand as banana.getBananaName(), maybe even better).
This would allow you to create a ListView<Fruit> and treat the items the same way:
listView.setCellFactory(lv -> new ListCell<Fruit>() {
#Override
public void updateItem(Fruit item, boolean empty) {
super.updateItem(item, empty);
setText(empty ? null : item.getName());
}
});
If you cannot do this you've got to swallow the bitter pill and use instanceof to determine the type of the item and treat the element accordingly:
listView.setCellFactory(lv -> new ListCell<Object>() {
#Override
public void updateItem(Object item, boolean empty) {
super.updateItem(item, empty);
String text;
if (empty) {
text = null;
} else if (item instanceof Banana) {
text = ((Banana) item).getBananaName();
} else {
text = ((Grapefruit) item).getGrapefruitName();
}
setText(text);
}
});

Javafx treeview adding extra icons randomly to the view

I am using javafx treeview and i added icons to my tree view.
I use the following code to add the icon to a treeitem. Using Jfeniox library for material design icon. When i click on the tree item the icons appears randomly to the end of the treeview list like in image.
rootTreeView.setCellFactory(tv -> new TreeCell<LeafItem>() {
#Override
public void updateItem(final LeafItem item, final boolean empty) {
super.updateItem(item, empty);
setText(null);
setTooltip(null);
setContextMenu(null);
if (!empty) {
if (getTreeItem().equals(rootTreeItem)) {
if (item == null) {
setText("sasa");
}
}
if (item instanceof Project) {
final Project project = (Project) item;
setText(project.getName());
setGraphic(createIcon(MaterialDesignIcon.FOLDER));
}
}
}
});
private MaterialDesignIconView createIcon(final MaterialDesignIcon icon) {
final MaterialDesignIconView materialDesignIconView = new MaterialDesignIconView(
icon);
materialDesignIconView.setSize("1.5em");
materialDesignIconView.setStyleClass("icon-color");
return materialDesignIconView;
}
In the updateItem method of the cell you do not set the graphic property to null in case the cell is empty or the item is not a instance of Project. Since items can be reassigned to the cell you need to do this in order to remove the icon from the cell:
#Override
public void updateItem(final LeafItem item, final boolean empty) {
super.updateItem(item, empty);
setText(null);
setTooltip(null);
setContextMenu(null);
setGraphic(null);
...

JavaFX Multiple cellValueFactories for one tableRow?

I'm newish to JavaFX still and don't quite understand how factories work.
What I'm trying to do is both apply styling to a tableCell in a tableColumn depending on what the String value is, and also make the cell editable.
Right now my code looks like this:
notesColumn.setCellFactory(column -> new TableCell<Computer, String>() {
#Override
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (item == null | empty)
{
setText(null);
setStyle("");
}
else
{
setText(item);
if (item.contains("Restoring #")) setTextFill(Color.CRIMSON);
else if (item.contains("Ready")) setTextFill(Color.FORESTGREEN);
else setTextFill(Color.BLACK);
}
}
});
And this piece works as I'd like it to, but if I try to make the cell editable by using the below code, the cellFactories overwrite one another, of course. How do I combine the two?
notesColumn.setCellFactory(TextFieldTableCell.forTableColumn());
The following should work:
notesColumn.setCellFactory(column -> new TextFieldTableCell<Computer, String>(new DefaultStringConverter()) {
#Override
public void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (item == null | empty)
{
setStyle("");
}
else
{
if (item.contains("Restoring #")) setTextFill(Color.CRIMSON);
else if (item.contains("Ready")) setTextFill(Color.FORESTGREEN);
else setTextFill(Color.BLACK);
}
}
});

JavaFX - bug in cell render after hiding columns

First of all sorry for my English:)
I have a strange bug... I'd like to hide some columns in JavaFX TableView after pressing the button. The code is as simple as possible:
column8.visibleProperty().set(false);
column9.visibleProperty().set(false);
(I also tested with remove with the same result).
The problem is that in "neighboring cell" I have "separator" from somewhere after this action.
I use Cellfactories is my code. For one of the hidden Cell it is
column8.setCellFactory(column -> {
return new TableCell<Anfrage, Mandant>() {
#Override
protected void updateItem(Mandant item, boolean empty) {
super.updateItem(item, empty);
if (item == null || empty) {
setText(null);
} else {
setText(item.getNameMandant());
}
}
};
});
And for cell, where I have "separator" after hiding:
//Store
column10.setCellFactory(column -> {
return new TableCell<Anfrage, Terminals>() {
#Override
protected void updateItem(Terminals item, boolean empty) {
super.updateItem(item, empty);
if (item == null || empty) {
setText(null);
} else {
setText(item.getStore());
}
}
};
});
Some pics to explain what is actually wrong.
If i set visible to "true" again everythings works as expected - all columns are in there without any "separators" anywhere.
Thanks a lot for any advice!
So, finally tableview refreshing is resolved in JavaFX 8u60. Now "native" table.refresh() do the trick.

Colouring table row in JavaFX

This question is related to this. Now I want to colour the row where field value equals to some value.
#FXML
private TableView<FaDeal> tv_mm_view;
#FXML
private TableColumn<FaDeal, String> tc_inst;
tc_inst.setCellValueFactory(cellData -> new SimpleStringProperty(""+cellData.getValue().getInstrumentId()));
tc_inst.setCellFactory(column -> new TableCell<FaDeal, String>() {
#Override
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (item == null || empty) {
setText(null);
} else {
setText(item);
// Style row where balance < 0 with a different color.
TableRow currentRow = getTableRow();
if (item.equals("1070")) {
currentRow.setStyle("-fx-background-color: tomato;");
} else currentRow.setStyle("");
}
}
});
The problem is I don't want to show tc_inst in my table. For this reason I set visible checkbox in SceneBuilder to false. In this case colouring part doesn't work at all. How can hide tc_inst so that colouring works?
Use a row factory, instead of a cell factory, if you want to change the color of the whole row:
tv_mm_view.setRowFactory(tv -> new TableRow<FaDeal>() {
#Override
public void updateItem(FaDeal item, boolean empty) {
super.updateItem(item, empty) ;
if (item == null) {
setStyle("");
} else if (item.getInstrumentId().equals("1070")) {
setStyle("-fx-background-color: tomato;");
} else {
setStyle("");
}
}
});
Note that if the value of instrumentId changes while the row is displayed, then the color will not change automatically with the above code, unless you do some additional work. The simplest way to make that happen would be to construct your items list with an extractor which returned the instrumentIdProperty() (assuming you are using the JavaFX property pattern in FaDeal).

Resources