我有一个简单的折线图,按下按钮即可在新窗口中打开。该折线图使用存储在硬盘上的图像作为背景。
如果我关闭计算折线图的窗口,更改图像文件(或删除它)并重新打开窗口,则会再次加载旧图像。
我在场景生成器和代码中禁用了折线图的缓存,但这没有帮助。
有人能给我提示我缺少什么吗?
这是我用于测试的 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;");
}
Thanks!
- - - 更新 - - -
目前我正在使用带有 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>
控制器:
包图表背景测试;
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);
}
}