Layout bug with SwingNode ?

Adam Granger adam at adamish.com
Fri Feb 23 09:50:08 UTC 2018


Greeting, 

(https://stackoverflow.com/questions/48937412/node-snapshot-causing-layout-issue)

Using JavaFX 9.0.4 I'm embedding some 3rd party Swing content in a
JavaFX app. The 3rd party content already has its own preferred size,
so I'm wrapping it with a StackPane to stretch it out... 

	However, when node.snapshot() is called this causes the SwingNode to
jump back to its preferred size, manually resizing the window causes
it to fill the window again. 

	I've also identified this only occurs if the scene is first shown
without content and the content is added later. 

	Is this a bug? How can I work around it? 

	The following example recreates the issue 

	public

class

NodeSnapshotFunny

extends

Application

{

@Override

public

void
 start
(
Stage
 stage
)

throws

Exception

{

SwingNode
 swingNode 
=

new

SwingNode
();

Button
 button 
=

new

Button
(
"Test"
);

 button
.
setOnAction
(
event 
->

{

WritableImage
 image 
=
 button
.
snapshot
(
null
,

null
);

});

StackPane
 stackPane 
=

new

StackPane
(
swingNode
);

Scene
 scene 
=

new

Scene
(
stackPane
);

 stage
.
setScene
(
scene
);

 stage
.
show
();

 stackPane
.
getChildren
().
add
(
button
);

SwingUtilities
.
invokeLater
(()

->

{

JPanel
 jPanel 
=

new

JPanel
();

 jPanel
.
setPreferredSize
(
new

Dimension
(
300
,

300
));

 jPanel
.
setBackground
(
Color
.
red
);

 swingNode
.
setContent
(
jPanel
);

});

}

public

static

void
 main
(
String
[]
 args
)

{

 launch
(
args
);

}

}




More information about the openjfx-dev mailing list