Not able to get javafx Listview variable height of rows - java

view.setCellFactory(lst ->
new ListCell<AnchorPane>() {
int k=1;
#Override
protected void updateItem(AnchorPane item,boolean empty) {
super.updateItem(item, empty);
if (empty) {
setPrefHeight(0.0);
setGraphic(null);
} else {
if(k == 1){
setPrefHeight(100.00);
k++;
}
else{
setPrefHeight(10.00);
}
setGraphic(item);
}
}
});
Setting different height of row by overriding the update Item and passing anchor pane but not getting different height for the rows.

Related

Is this a Bug in my code or a problem with cellfactory

todoitemsListview.setCellFactory(new Callback<ListView<ToDoItems>, ListCell<ToDoItems>>() {
#Override
public ListCell<ToDoItems> call(ListView<ToDoItems> toDoItemsListView) {
ListCell<ToDoItems> cell= new ListCell<ToDoItems>(){
#Override
protected void updateItem(ToDoItems items, boolean empty) {
super.updateItem(items, empty);
if(empty){
setText(null);
}else {
setText(items.getItemName());
if (items.getDeadline().equals(LocalDate.now())){
System.out.println(items.getDeadline().toString());
System.out.println(items.getItemName());
setTextFill(Color.RED);
}else if (items.getDeadline().equals(LocalDate.now().plusDays(1))){
System.out.println(items.getDeadline().toString());
System.out.println(items.getItemName());
setTextFill(Color.BLUE);
}else if (items.getDeadline().equals(LocalDate.now().plusDays(2))){
System.out.println(items.getDeadline().toString());
System.out.println(items.getItemName());
setTextFill(Color.GREEN);
}else if(items.getDeadline().isBefore(LocalDate.now())){
System.out.println(items.getDeadline().toString());
System.out.println(items.getItemName());
setTextFill(Color.GREY);
}
}
}
};
return cell;
}
});
I used a cellfactory in listview to set text color to different values based on their due date. But when i add new items to the list, items which doesn't satisfy the if - else conditions is also highlighted.
This is part of a basic ToDo application based on a ObservabelList , Where ToDoItems class has three attributes, itemName,ItemDesription,Deadline.
Cells in a ListView are reused when needed. So in your case, you have set a specific cell with a red fill, but when that same cell is reused for a different ToDoItems instance (because you scrolled down or added a new item in the list), and that new ToDoItems instance doesn't fullfil any of your conditions that change the fill color, the cell is gonna keep its previous fill color incorrectly.
What you need to do is make sure you fall back to a default color if none of the conditions in your example are met:
todoitemsListview.setCellFactory(new Callback<ListView<ToDoItems>, ListCell<ToDoItems>>() {
#Override
public ListCell<ToDoItems> call(ListView<ToDoItems> toDoItemsListView) {
ListCell<ToDoItems> cell = new ListCell<ToDoItems>() {
#Override
protected void updateItem(ToDoItems items, boolean empty) {
super.updateItem(items, empty);
if (empty) {
setText(null);
setTextFill(Color.BLACK);
} else {
setText(items.getItemName());
if (items.getDeadline().equals(LocalDate.now())) {
System.out.println(items.getDeadline().toString());
System.out.println(items.getItemName());
setTextFill(Color.RED);
} else if (items.getDeadline().equals(LocalDate.now().plusDays(1))) {
System.out.println(items.getDeadline().toString());
System.out.println(items.getItemName());
setTextFill(Color.BLUE);
} else if (items.getDeadline().equals(LocalDate.now().plusDays(2))) {
System.out.println(items.getDeadline().toString());
System.out.println(items.getItemName());
setTextFill(Color.GREEN);
} else if (items.getDeadline().isBefore(LocalDate.now())) {
System.out.println(items.getDeadline().toString());
System.out.println(items.getItemName());
setTextFill(Color.GREY);
} else {
setTextFill(Color.BLACK);
}
}
}
};
return cell;
}
});

Click event on ImageView inside TableCell in JavaFX

I have a JavaFX TableView populated with many TableColumns, one of which is matImageColumn. The code for it looks like:
matImageColumn.setCellFactory(new Callback<TableColumn<CustomObject, ImageView>, TableCell<CustomObject, ImageView>>() {
#Override
public TableCell call(final TableColumn<CustomObject, ImageView> param) {
final TableCell<CustomObject, ImageView> cell = new TableCell<CustomObject, ImageView>() {
ImageView img = new ImageView();
#Override
public void updateItem(ImageView item, boolean empty) {
super.updateItem(item, empty);
if (empty) {
setGraphic(null);
setText(null);
} else {
CustomObject co = getTableView().getItems().get(getIndex());
img = co.getImageViewOfMat();
setGraphic(img);
setText(null);
}
}
};
return cell;
}
});
Now as it is, I am looking to include a click event on my imageview but I am clueless from where to start.Can you show me possible solutions to this issue? Thanks in advance.

How to change text alignment of a combo box items in javafx?

I have a Combo Box in my app . I added some items as string to it . Items have left alignment . I want the right alignment . I searched a lot but i cant find anything.
Thanks.
I figured out the following solution, although I'm not sure if it's a good one:
For button cell:
box.setButtonCell(new ListCell<String>() {
#Override
public void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (item != null) {
setText(item);
setAlignment(Pos.CENTER_RIGHT);
Insets old = getPadding();
setPadding(new Insets(old.getTop(), 0, old.getBottom(), 0));
}
}
});
For popup list view:
box.setCellFactory(new Callback<ListView<String>, ListCell<String>>() {
#Override
public ListCell<String> call(ListView<String> list) {
return new ListCell<String>() {
#Override
public void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (item != null) {
setText(item);
setAlignment(Pos.CENTER_RIGHT);
setPadding(new Insets(3, 3, 3, 0));
}
}
};
}
});

JavaFX Binding String to TreeCell Factory Properly

I'm getting an issue, where when I expand and unexpand a tree cell. The text I binded to doesn't ever go away.. What could be the cause of this?
This is what I get
Here is my code
treeView.setCellFactory(t -> {
return new TreeCell<Entry>() {
#Override
protected void updateItem(Entry entry, boolean empty) {
super.updateItem(entry, empty);
if (entry != null) {
if (empty) {
setText(null);
setGraphic(null);
} else {
if (entry.getImage() != null) {
Image image = FXUtils.toFXImage(entry.getImage());
setGraphic(new ImageView(image));
} else {
setGraphic(null);
}
textProperty().bind(entry.nameProperty());
}
}
}
};
});
If a cell is empty, the item is also null. However all your logic is only executed, if (entry != null), i.e. never, if the cell is empty or the item is null. Furthermore this should fail, even if you fix this error, since you never unbind the textProperty, but it could be set for empty cells. Change your updateItem method to something like this:
#Override
protected void updateItem(Entry entry, boolean empty) {
super.updateItem(entry, empty);
textProperty().unbind();
if (empty || entry == null) {
setText(null);
setGraphic(null);
} else {
if (entry.getImage() != null) {
Image image = FXUtils.toFXImage(entry.getImage());
setGraphic(new ImageView(image));
} else {
setGraphic(null);
}
textProperty().bind(entry.nameProperty());
}
}

Custom row style

I have a tableview tbvAlumnos which two tablecolumns: tbcNombre, tbcApellidos.
I mapped a list from pojo:
public class Alumno
{
private boolean activo;
private String nombre;
private String apellidos;
}
Where I mapped alumnos's list to tableview I want to text fill row with red colour where alumno activo is false and text fill row with green when is true.
I tried but I don't know how to get each alumno to check if is active or not because follow code fails because I pass Alumno item rather than String.
tbcNombre.setCellFactory(column -> {
return new TableCell<Alumno, Alumno>() {
#Override
protected void updateItem(Alumno item, boolean empty) {
super.updateItem(item, empty);
if (item == null || empty) {
setText(null);
setStyle("");
} else {
// Format date.
setText(item.toString());
setStyle("-fx-text-fill: red");
}
}
};
});
So How can I add style to all row fields based on condition of each item of each row.

Categories