Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8167991

JavaFX SwingNode non-visible Image save bug

XMLWordPrintable

      FULL PRODUCT VERSION :
      java version "1.8.0_101"
      Java(TM) SE Runtime Environment (build 1.8.0_101-b13)
      Java HotSpot(TM) 64-Bit Server VM (build 25.101-b13, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows [Version 10.0.10586]

      A DESCRIPTION OF THE PROBLEM :
      This bug has also been posted by me on Stack Overflow:
      http://stackoverflow.com/questions/39982799/java-swingnode-non-visible-image-save-bug

      On trying to take a snapshot of a BorderLayout which contains a SwingNode and JavaFX element, in the resulting image the Swingnode appears to be positioned in the lower right corner. I only get these results when the BorderLayout has not been seen. I have reproduced the problem in the test program below. When the export button is clicked, both the visible and non visible tabs should save to two different files with the same picture, but "invisible_file.png" picture should not be in the lower right corner.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1. Create a javafx tab which has not and will not be displayed to the screen
      2. Use a javafx borderpane for a container of the elements
      3. Embed a picture inside of a SwingNode
      4. Place that swingnode inside the borderpane as the center element
      5. Place a javafx element at the bottom of the boarderpane
      6. Take a snapshot of the still non visible boarderpane

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The image should be saved in the center of the borderpane
      ACTUAL -
      The image is saved in the bottom right corner of the borderpane with the javafx element overlapping it. The image is only 1/4 visible.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      TabController.java

      import java.awt.BorderLayout;
      import java.awt.Image;
      import java.io.File;
      import java.io.IOException;
      import java.net.URL;

      import javax.imageio.ImageIO;
      import javax.swing.ImageIcon;
      import javax.swing.JLabel;
      import javax.swing.JPanel;

      import javafx.application.Application;
      import javafx.embed.swing.SwingFXUtils;
      import javafx.embed.swing.SwingNode;
      import javafx.fxml.FXML;
      import javafx.fxml.FXMLLoader;
      import javafx.scene.Parent;
      import javafx.scene.Scene;
      import javafx.scene.SnapshotParameters;
      import javafx.scene.control.Button;
      import javafx.scene.control.Tab;
      import javafx.scene.control.TabPane;
      import javafx.scene.layout.BorderPane;
      import javafx.stage.Stage;

      public class TabController extends Application{

      @FXML
      private TabPane tabPane;

      @FXML
      private Tab visibleTab, invisibleTab;

      @FXML
      private SwingNode visibleNode, invisibleNode;

      @FXML
      private Button export;

      @FXML
      private BorderPane invisibleBP, visibleBP;

      private JPanel visiblePanel, invisiblePanel;

      public void initialize()
      {
      this.visiblePanel = new JPanel(new BorderLayout());
      this.invisiblePanel = new JPanel(new BorderLayout());

      URL url = null;
      Image image = null;
      try{
      url = new URL("http://devstickers.com/assets/img/pro/d1i3.png");
      image = ImageIO.read(url);
      } catch (IOException e){ e.printStackTrace(); }

      ImageIcon img1 = new ImageIcon(image);
      this.visiblePanel.add(new JLabel(img1), BorderLayout.CENTER);
      this.visibleNode.setContent(this.visiblePanel);

      ImageIcon img2 = new ImageIcon(image);
      this.invisiblePanel.add(new JLabel(img2), BorderLayout.CENTER);
      this.invisibleNode.setContent(this.invisiblePanel);

          }

      @FXML
      private void handleExport()
      {
      File visibleFile = new File("visible_file.png");
      File invisibleFile = new File("invisible_file.png");
      System.out.println("Export these pictures");
      try {
      ImageIO.write(SwingFXUtils.fromFXImage(this.invisibleBP.snapshot(new SnapshotParameters(), null), null), "png", invisibleFile);
      ImageIO.write(SwingFXUtils.fromFXImage(this.visibleBP.snapshot(new SnapshotParameters(), null), null), "png", visibleFile);
      } catch (IOException e) {
      e.printStackTrace();
      }
      }

      @Override
      public void start(Stage primaryStage) {
      try {
      Parent root = FXMLLoader.load(TabController.class.getResource("tab_hide_document.fxml"));
      Scene scene = new Scene(root);
      primaryStage.setHeight(1000);
      primaryStage.setWidth(1500);
      primaryStage.setScene(scene);
      primaryStage.show();
      } catch(Exception e) {
      e.printStackTrace();
      }
      }

      public static void main(String[] args) {
      launch(args);
      }
      }


      tab_hide_document.fxml

      <?xml version="1.0" encoding="UTF-8"?>

      <?import java.lang.*?>
      <?import java.util.*?>
      <?import javafx.scene.*?>
      <?import javafx.scene.control.*?>
      <?import javafx.scene.layout.*?>
      <?import javafx.geometry.Insets?>
      <?import javafx.embed.swing.SwingNode?>


       <VBox xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="exporter.TabController" prefHeight="500">
        <Button text="Export" fx:id="export" onAction="#handleExport"/>
        <TabPane fx:id="tabPane" minWidth="500">
        <tabs>
        <Tab text="visible tab" fx:id="visibleTab">
        <BorderPane fx:id="visibleBP" minHeight="300" minWidth="300">
        <center>
      <SwingNode fx:id="visibleNode"></SwingNode>
      </center>
      <bottom>
      <Slider></Slider>
      </bottom>
        </BorderPane>
        </Tab>
        <Tab text="invisible tab" fx:id="invisibleTab">
        <BorderPane fx:id="invisibleBP" minHeight="300" minWidth="300">
        <center>
      <SwingNode fx:id="invisibleNode"></SwingNode>
      </center>
      <bottom>
      <Slider></Slider>
      </bottom>
        </BorderPane>
        </Tab>
        </tabs>
        </TabPane>
       </VBox>



      ---------- END SOURCE ----------

            Unassigned Unassigned
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: