Created
January 19, 2018 09:15
-
-
Save pdemanget/27342c948abf70ea8761d4a6bfd86ace to your computer and use it in GitHub Desktop.
Javafx utils
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
}); | |
} |
Author
pdemanget
commented
Jan 19, 2018
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment