首页 文章

禁用JavaFX图表背景图像的缓存

提问于
浏览
2

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

有人能给我一个暗示我缺少的东西吗?

这是我用于测试的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>

控制器: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 . 关闭对话框并按指定的按钮重新打开,原始图像仍然加载 .

按钮代码,打开对话框:

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

2 回答

  • 1

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

  • 1

    我建议您观看背景图像文件的更改,然后如果文件更改,将背景图像设置为null,然后再次将背景图像设置回文件以强制重新加载 . 见:Can I watch for single file change with WatchService (not the whole directory)? .

    出于性能原因,setCache函数禁止JavaFX系统将节点缓存为内存中的映像;即,当setCache打开时,它会提示JavaFX呈现节点一次,然后将其存储在缓存的图像中 . 我不确定JavaFX是否对通过样式加载的图像有单独的缓存 . 如果确实如此,这导致我之前的建议不起作用,您可以将图表放在StackPane中,图表位于顶部,而ImageView位于图表后面的背景视图,然后在文件观察者检测到该图像时加载新图像图像文件已更改(从而绕过JavaFX CSS系统可能有或没有的任何缓存机制) .

相关问题