禁用JavaFX图表背景图像的缓存 [英] Disable caching of JavaFX Chart background image

查看:479
本文介绍了禁用JavaFX图表背景图像的缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的LineChart,只需按一下按钮就会在新窗口中打开。此LineChart使用存储在硬盘驱动器上的图像作为其背景。
如果我关闭计算LineChart的窗口,更改图像文件(或删除它)并重新打开窗口,将再次加载旧图像。
我在场景生成器和我的代码中禁用了LineChart的缓存,但它没有帮助。

I have a simple LineChart, that opens in a new window on press of a button. This LineChart uses an image stored on harddrive as its background. If I close the window countaining the LineChart, change the image file (or delete it) and reopen the window, the old image is loaded again. I disabled the cache of my LineChart in Scene Builder and in my code, but it doesn't help.

有人能给我一个暗示我的意思吗?我失踪了?

Can somebody give me a hint what I'm missing?

这是我用于测试的LineChart-Controller的简单代码片段:

Here's a simple code-snippet of the LineChart-Controller that I'm using for testing:

@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加载,即使文件本身发生了变化,也会显示相同的图像。

----- Update ----- Currently I'm using a StackPane with an ImageView and LineChart to display my image. But initial problem remains. Once an external image is loaded by css, the same image is shown, even if the file itself changed.

这是我更新的测试用例,在按下时作为对话框加载按钮。

Here is my updated testcase, loaded as a dialog when pressing a button.

FXML:

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>

控制器:
包chartbackgroundtest;

Controller: package 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。
关闭对话框并按指定按钮重新打开它,原始图像仍然加载。

I have two different images, histData.png and histData2.png. My chart uses histData.png as the background-image of the chart-plot-background. After opening the dialog, the files histData.png and histData2.png are switched. Closing the dialog and re-opening it by pressing the assigned button, the original image is still loaded.

按钮代码,打开对话框:

Code of the button, opening the dialog:

    @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);
        }    
    }


推荐答案

我建议您使用不同的名称保存文件,并使用不同的名称调用setStyle。

I recommend you saving file with the different name and call to setStyle with different name.

这篇关于禁用JavaFX图表背景图像的缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆