Swingnode Error in Java Swingnode

When you try to take a picture of the BorderLayout that contains the SwingNode and JavaFX element, the Swingnode appears in the bottom right corner in the resulting image. I get these results only when BorderLayout has not been noticed. I reproduced the problem in the test program below. When you click the export button, both visible and invisible tabs should be saved in two different files with the same image, but the image "invisible_file.png" should not be in the lower right corner.

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="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> 

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.embed.swing.SwingFXUtils; import javafx.embed.swing.SwingNode; import javafx.fxml.FXML; import javafx.scene.SnapshotParameters; import javafx.scene.control.Button; import javafx.scene.control.Tab; import javafx.scene.control.TabPane; import javafx.scene.layout.BorderPane; public class TabController { @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(); } } } 

TabExportTester.java

 import javafx.application.Application; import javafx.fxml.FXMLLoader; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.stage.Stage; public class TabExportTester extends Application { @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); } } 
+5
source share

Source: https://habr.com/ru/post/1258041/


All Articles