Skip to content

Instantly share code, notes, and snippets.

@pdemanget
Created January 19, 2018 09:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pdemanget/27342c948abf70ea8761d4a6bfd86ace to your computer and use it in GitHub Desktop.
Save pdemanget/27342c948abf70ea8761d4a6bfd86ace to your computer and use it in GitHub Desktop.
Javafx utils
public static <ROW> Callback<TableColumn<ROW, Boolean>, TableCell<ROW, Boolean>> getCheckboxCell(){
return column -> {
CheckBoxTableCell<ROW, Boolean> cell = new CheckBoxTableCell<>();
cell.setAlignment(Pos.CENTER);
return cell;
};
}
public static <ROW, T extends Temporal> Callback<TableColumn<ROW, T>, TableCell<ROW, T>> getDateCell(
DateTimeFormatter format) {
return column -> {
return new TableCell<ROW, T>() {
@Override
protected void updateItem(T item, boolean empty) {
super.updateItem(item, empty);
if (item == null || empty) {
setText(null);
} else {
setText(format.format(item));
}
}
};
};
}
public static <ROW, T extends Number> Callback<TableColumn<ROW, T>, TableCell<ROW, T>> getPercentCell() {
return column -> {
return new TableCell<ROW, T>() {
@Override
protected void updateItem(T item, boolean empty) {
super.updateItem(item, empty);
if (item == null || empty) {
setText(null);
} else {
double value = ((Double) item) * 100d;
DecimalFormat df = new DecimalFormat("0");
setText(df.format(value) + " %");
}
}
};
};
}
public static <ROW, T extends Temporal> Callback<TreeTableColumn<ROW, T>, TreeTableCell<ROW, T>> getDateTreeCell(
DateTimeFormatter format) {
return column -> {
return new TreeTableCell<ROW, T>() {
@Override
protected void updateItem(T item, boolean empty) {
super.updateItem(item, empty);
if (item == null || empty) {
setText(null);
} else {
setText(format.format(item));
}
}
};
};
}
/**
* Créee une cellule de tableau boolean pour afficher une croix rouge en cas de
* défaut.
*
* @param isCumul
* @return
*/
public static <T> Callback<TableColumn<T, Boolean>, TableCell<T, Boolean>> getBooleanCell(Image imageTrue,
Image imageFalse) {
Callback<TableColumn<T, Boolean>, TableCell<T, Boolean>> booleanCell = param -> {
ImageView imageview;
imageview = new ImageView();
imageview.setFitHeight(16);
imageview.setFitWidth(16);
imageview.setOnMouseClicked(evt -> {
Object source = evt.getSource();
System.out.println(source);
});
TableCell<T, Boolean> cell = new TableCell<T, Boolean>() {
@Override
public void updateItem(Boolean item, boolean isEmpty) {
if (Boolean.TRUE.equals(item)) {
imageview.setImage(imageTrue);
} else if (item == null) {
imageview.setImage(null);
} else {
imageview.setImage(imageFalse);
}
}
};
cell.setGraphic(imageview);
// imageview.setImage(imageTrue);
cell.setAlignment(Pos.CENTER);
return cell;
};
return booleanCell;
}
public static <T> Callback<TableColumn<T, Boolean>, TableCell<T, Boolean>> getClickableImage(Image imageTrue, Consumer<T> onAction) {
Callback<TableColumn<T, Boolean>, TableCell<T, Boolean>> booleanCell = param -> {
ImageView imageview;
imageview = new ImageView();
imageview.getStyleClass().add("action");
imageview.setFitHeight(16);
imageview.setFitWidth(16);
imageview.setOnMouseClicked(evt -> {
Object source = evt.getSource();
System.out.println(source);
});
TableCell<T, Boolean> cell = new TableCell<T, Boolean>() {
@Override
public void updateItem(Boolean item, boolean isEmpty) {
if (Boolean.FALSE.equals(item)) {
imageview.setImage(imageTrue);
} else {
imageview.setImage(null);
}
}
};
cell.addEventFilter(MouseEvent.MOUSE_CLICKED,evt->{
onAction.accept(cell.getTableRow().getItem());
});
/*
cell.setOnMouseClicked(evt->{
onAction.accept(cell.getTableRow().getItem());
});*/
cell.setGraphic(imageview);
// imageview.setImage(imageTrue);
cell.setAlignment(Pos.CENTER);
return cell;
};
return booleanCell;
}
public static <T> Callback<TableColumn<T, String>, TableCell<T, String>> getClickableButton(String text, Consumer<T> onAction) {
Callback<TableColumn<T, String>, TableCell<T, String>> callbackCell = param -> {
Button button = new Button(text);
TableCell<T, String> cell = new TableCell<>() {
@Override
public void updateItem(String item, boolean isEmpty) {
if(getTableRow() == null || getTableRow().getItem() == null) {
setGraphic(null);
} else {
setGraphic(button);
}
}
};
button.setOnAction(evt -> {
//Object source = evt.getSource();
onAction.accept(cell.getTableRow().getItem());
});
cell.setGraphic(button);
cell.setAlignment(Pos.CENTER);
return cell;
};
return callbackCell;
}
public static <T> void doubleClick(TableView<T> table, Consumer<T> action) {
table.setRowFactory(tv -> {
TableRow<T> row = new TableRow<>();
row.setOnMouseClicked(event -> {
if (event.getClickCount() == 2 && (!row.isEmpty())) {
T rowData = row.getItem();
action.accept(rowData);
// System.out.println(rowData);
}
});
return row;
});
}
@SuppressWarnings({ "rawtypes", "unchecked" })
public static void moveableFalse(TableView tableView) {
List<TableColumn<?, ?>> columns = new ArrayList(tableView.getColumns());
tableView.getColumns().addListener(new ListChangeListener() {
public boolean suspended;
@Override
public void onChanged(Change change) {
change.next();
if (change.wasReplaced() && !suspended) {
this.suspended = true;
tableView.getColumns().setAll(columns);
this.suspended = false;
}
}
});
}
@pdemanget
Copy link
Author

  public static <B, T> void bindBeanBidirectional (Property<B> objProp, Function<B, Property<T>> getter, Property<T> property) {
     objProp.addListener( (obs, old, newValue) -> {
      if (old != null ) {
        property.unbindBidirectional(getter.apply(old));
      }
      if (newValue != null) {
        property.bindBidirectional(getter.apply(newValue));
      }
    });
  }
  
  
  /**
   * Bind les getter setter d'un bean classique wrappé dans une property à une property.
   *
   * @param objProp
   * @param getter
   * @param setter
   * @param property
   */
  @SuppressWarnings ("unchecked")
  public static <B, T> void bindBeanBidirectional (ObjectProperty<B> objProp, Function<B, T> getter, BiConsumer<B, T> setter, Property<? super T> property) {
    objProp.addListener( (obs, old, value) -> {
      if (value == null) {
        property.setValue(null);
      }
      else {
        property.setValue(getter.apply(objProp.get()));
      }
    });

    property.addListener( (obs, old, value) -> {
      if (objProp.get() == null) {
        return;
      }
      setter.accept(objProp.get(), (T)value);
    });
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment