【发布时间】:2016-09-08 19:57:11
【问题描述】:
我在 javafx 中使用自定义单元工厂根据警报状态设置单元格样式,该警报状态基于现在时间和检查时间。
一切正常,但我还想添加一个淡入淡出过渡或其他东西,这将使行基本上闪烁,直到通过更改检查时间来确认它。
它比我认为可以添加大量代码的要复杂一些,所以我将发布我的单元工厂类
public class FormattedTableCellFactory<S, T> implements Callback<TableColumn<S, T>, TableCell<S, T>> {
private TableCell<S, T> cell = null;
private FadeTransition ft;
public FormattedTableCellFactory() {
}
@Override
public TableCell<S, T> call(TableColumn<S, T> p) {
cell = new TableCell<S, T>() {
@Override
protected void updateItem(Object item, boolean empty) {
super.updateItem((T) item, empty);
// CSS Styles
String cssStyle;
Person person = null;
if(getTableRow() != null ) {
person = (Person) getTableRow().getItem();
}
ft = new FadeTransition(Duration.millis(500), cell);
ft.setFromValue(1.0);
ft.setToValue(0.1);
ft.setCycleCount(Timeline.INDEFINITE);
ft.setAutoReverse(true);
//Remove all previously assigned CSS styles from the cell.
//Determine how to format the cell based on the status of the container.
if(person != null){
switch(PersonAlert.getAlert(person)){
case WARNING:
ft.stop();
cssStyle = "warning";
break;
case PAST_DUE:
ft.stop();
cssStyle = "pastdue";
break;
case ERC_PAST_DUE:
ft.stop();
cssStyle = "ercpastdue";
break;
case OVERDUE:
ft.playFromStart();
cssStyle = "overdue";
break;
case OVERNIGHT:
ft.stop();
cssStyle = "overnight";
break;
default:
ft.stop();
cssStyle = "normal";
break;
}
}else{
ft.stop();
setText("");
return;
}
//Set the CSS style on the cell and set the cell's text.
getStyleClass().setAll(cssStyle);
if(item != null) {
setText(item.toString());
}else{
setText("");
}
}
};
return cell;
}
}
这段代码可以正常工作,只要人有某种状态,单元格的颜色就会相应地改变。这里唯一的问题是淡入淡出过渡,每当它第一次运行时,淡入淡出过渡运行良好,但是当它更改为不同的样式时,它不会停止并一直闪烁。
我无法停止淡入淡出过渡。我相信这是因为每次调用此 updateItem 时,它都会创建我的淡入淡出转换的新实例,因此当它调用停止时,它是在当前转换而不是前一个转换上调用它。我试图通过在顶部初始化转换来消除这种情况,但它甚至不会闪烁。
所以我的问题是,我如何编辑它以在调用 stop 时停止淡入淡出过渡,或者是否有其他方法可以使用单元工厂使单元格/行“闪烁”?
【问题讨论】: