我有一个简单的折线图,按下按钮即可在新窗口中打开。该折线图使用存储在硬盘上的图像作为背景。 如果我关闭计算折线图的窗口,更改图像文件(或删除它)并重新打开窗口,则会再次加载旧图像。 我在场景生成器和代码中禁用了折线图的缓存,但这没有帮助。
有人可以告诉我我错过了什么吗?
这是我用于测试的 LineChart-Controller 的简单代码片段:
@FXML
private LineChart<?, ?> lineChart;
/**
* Initializes the controller class.
*/
@Override
public void initialize(URL url, ResourceBundle rb) {
lineChart.setCache(false);
Node chartNode = lineChart.lookup(".chart-plot-background");
chartNode.setCache(false);
chartNode.setStyle("-fx-background-image: url('file:///C://Temp//histData.png'); -fx-background-position: center center;");
}
谢谢!
-----更新----- 目前我正在使用带有 ImageView 和 LineChart 的 StackPane 来显示我的图像。 但最初的问题仍然存在。一旦通过 css 加载外部图像,即使文件本身发生更改,也会显示相同的图像。
这是我更新的测试用例,按下按钮时作为对话框加载。
FXML:
<?import javafx.scene.image.*?>
<?import javafx.scene.chart.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" cacheShape="false" prefHeight="500.0" prefWidth="812.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="chartbackgroundtest.ChartController">
<children>
<LineChart fx:id="lineChart" cacheShape="false" prefHeight="353.0" prefWidth="611.0" styleClass="mychart">
<xAxis>
<CategoryAxis side="BOTTOM" />
</xAxis>
<yAxis>
<NumberAxis side="LEFT" />
</yAxis>
</LineChart>
</children>
</AnchorPane>
Controller : 封装chartbackgroundtest;
import java.io.File;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.chart.LineChart;
public class ChartController implements Initializable {
@FXML
private LineChart<?, ?> lineChart;
Node chartNode;
/**
* Initializes the controller class.
*/
@Override
public void initialize(URL url, ResourceBundle rb) {
chartNode = lineChart.lookup(".chart-plot-background");
lineChart.getStylesheets().clear();
chartNode.setStyle(null);
chartNode.setStyle("-fx-background-image: url('file:///C://Temp//histData.png'); -fx-background-position: center;");
new File("C:\\Temp\\histData.png").renameTo(new File("C:\\Temp\\histData3.png"));
new File("C:\\Temp\\histData2.png").renameTo(new File("C:\\Temp\\histData.png"));
new File("C:\\Temp\\histData3.png").renameTo(new File("C:\\Temp\\histData2.png"));
chartNode.applyCss();
lineChart.requestLayout();
}
}
我有两个不同的图像,histData.png 和 histData2.png。我的图表使用 histData.png 作为图表绘图背景的背景图像。打开对话框后,文件 histData.png 和 histData2.png 被切换。 关闭对话框并按指定按钮重新打开它,原始图像仍然会加载。
按钮代码,打开对话框:
@FXML
private void handleButtonAction(ActionEvent event) {
FXMLLoader loader = new FXMLLoader(getClass().getResource("Chart.fxml"));
loader.setBuilderFactory(new JavaFXBuilderFactory());
Stage dialog = new Stage();
dialog.initStyle(StageStyle.UTILITY);
dialog.initModality(Modality.APPLICATION_MODAL);
try {
Scene scene = new Scene(loader.load());
dialog.setScene(scene);
dialog.show();
} catch (IOException ex) {
Logger.getLogger(MainViewController.class.getName()).log(Level.SEVERE, null, ex);
}
}
最佳答案
我建议您观察背景图像文件的变化,然后如果文件发生变化,将背景图像设置为空,然后再次将背景图像设置回文件以强制重新加载。请参阅:Can I watch for single file change with WatchService (not the whole directory)? 。
出于性能原因,setCache 函数禁止 JavaFX 系统将节点作为图像缓存在内存中;即,当 setCache 打开时,它会提示 JavaFX 渲染节点一次,然后将其存储在缓存图像中。我不确定 JavaFX 是否为通过样式加载的图像提供单独的缓存。如果确实如此并且导致我之前的建议不起作用,您可以将图表放在 StackPane 中,图表位于顶部,ImageView 作为图表后面的背景 View ,然后在文件观察器检测到图像文件已更改(从而绕过 JavaFX CSS 系统可能具有或不具有的任何缓存机制)。
关于java - 禁用 JavaFX 图表背景图像的缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27962287/