i have 3 test code ,It's all Incorrect (the components):
sometimes not display,
sometimes have black area,
sometimes Disappear
I have a *.doc containt code and printScreen ,if need see ,please send email to me.
Jdk 1.8.0_25 windows8.1
this is code:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package test;
/**
*
* @author zbp
*/
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.embed.swing.SwingNode;
import javafx.geometry.Orientation;
import javafx.scene.Scene;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.util.Callback;
import javax.swing.JButton;
import javax.swing.SwingUtilities;
public class Bug extends Application {
ListView<String> list = new ListView<String>();
ObservableList<String> data = FXCollections.observableArrayList("chocolate", "salmon", "gold", "coral", "darkorchid", "darkgoldenrod", "lightsalmon", "black", "rosybrown", "blue", "blueviolet", "brown");
@Override
public void start(Stage stage) {
VBox box = new VBox();
list.setOrientation(Orientation.HORIZONTAL);
Scene scene = new Scene(box, 200, 200);
stage.setScene(scene);
stage.setTitle("ListViewSample");
box.getChildren().addAll(list);
VBox.setVgrow(list, Priority.ALWAYS);
list.setItems(data);
list.setCellFactory(new Callback<ListView<String>, ListCell<String>>() {
@Override
public ListCell<String> call(ListView<String> list) {
return new ColorRectCell();
}
});
stage.show();
}
static class ColorRectCell extends ListCell<String> {
SwingNode swingNode = new SwingNode();
@Override
public void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
Text t = new Text(item);
VBox vb = new VBox();
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
swingNode.setContent( new JButton("jbutton"));
}
});
vb.getChildren().addAll(t, swingNode);
if (item != null) {
setGraphic(vb);
}
}
}
public static void main(String[] args) {
launch(args);
}
}
Run
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package test;
import java.awt.event.MouseListener;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.embed.swing.SwingNode;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javax.swing.JButton;
import javax.swing.SwingUtilities;
public class SwingFX extends Application {
Button botton=new Button("java fx button");
JButton jb ;
@Override
public void start(Stage stage) {
final SwingNode swingNode = new SwingNode();
createAndSetSwingContent(swingNode);
VBox vb=new VBox();
botton.setOnMouseClicked(new EventHandler(){
@Override
public void handle(Event event) {
SwingUtilities.invokeLater(new Runnable(){
public void run(){
jb.setEnabled(!jb.isEnabled());
}
});
}
});
StackPane pane = new StackPane();
pane.getChildren().add(swingNode);
vb.getChildren().addAll(pane,botton);
stage.setScene(new Scene(vb, 100, 50));
stage.show();
}
private void createAndSetSwingContent(final SwingNode swingNode ) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
jb =new JButton("swing button");
jb.addMouseListener(new MouseListener(){
@Override
public void mouseClicked(java.awt.event.MouseEvent e) {
Platform.runLater(new Runnable(){
public void run(){
botton.setDisable(!botton.isDisable());
}
});
}
@Override
public void mousePressed(java.awt.event.MouseEvent e) {
//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void mouseReleased(java.awt.event.MouseEvent e) {
//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void mouseEntered(java.awt.event.MouseEvent e) {
//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void mouseExited(java.awt.event.MouseEvent e) {
//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
});
swingNode.setContent(jb);
}
});
}
public static void main(String[] args) {
launch(args);
}
}
Run
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package test;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import java.awt.Dimension;
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.embed.swing.SwingNode;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javax.swing.JButton;
import javax.swing.SwingUtilities;
/**
*
* @author zbp
*/
public class bug2 extends Application {
Stage dialogStage = null;
final ScrollPane sp = new ScrollPane();
Dimension dialogSize = new Dimension(700, 200);
@Override
public void start(Stage primaryStage) {
dialogStage = primaryStage;
ini();
dialogStage.setResizable(false);
dialogStage.show();
}
private void ini() {
HBox box = new HBox();
Group rootGroup = new Group();
HBox hbb = new HBox();
hbb.setSpacing(5);
for (int i = 1; i < 6; i++) {
VBox vb = new VBox();
Text mapNameLabel = new Text("title "+i+" ");
final SwingNode sn = new SwingNode();
createSwingComponent(sn);
StackPane pane = new StackPane();
pane.getChildren().add(sn);
vb.getChildren().addAll(mapNameLabel, pane);
hbb.getChildren().add(vb);
}
sp.setContent(hbb);
sp.setPrefSize(dialogSize.width, dialogSize.height);
BorderPane bp = new BorderPane();
Button left = new Button("<");
Button right = new Button(">");
left.setPrefHeight(dialogSize.height);
right.setPrefHeight(dialogSize.height);
bp.setLeft(left);
bp.setRight(right);
bp.setCenter(sp);
box.getChildren().addAll(bp);
rootGroup.getChildren().add(box);
Scene scene = new Scene(rootGroup, dialogSize.width + 50, dialogSize.height);
dialogStage.setScene(scene);
}
private void createSwingComponent(final SwingNode swingNode) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JButton jb = new JButton("+");
jb.setPreferredSize(new Dimension(dialogSize.height, dialogSize.height - 45));
jb.setMinimumSize(new Dimension(dialogSize.height, dialogSize.height - 45));
swingNode.setContent(jb);
}
});
}
public static void main(String[] s) {
launch(s);
}
}
Run 1
Run 2
Run 3
sometimes not display,
sometimes have black area,
sometimes Disappear
I have a *.doc containt code and printScreen ,if need see ,please send email to me.
Jdk 1.8.0_25 windows8.1
this is code:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package test;
/**
*
* @author zbp
*/
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.embed.swing.SwingNode;
import javafx.geometry.Orientation;
import javafx.scene.Scene;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.util.Callback;
import javax.swing.JButton;
import javax.swing.SwingUtilities;
public class Bug extends Application {
ListView<String> list = new ListView<String>();
ObservableList<String> data = FXCollections.observableArrayList("chocolate", "salmon", "gold", "coral", "darkorchid", "darkgoldenrod", "lightsalmon", "black", "rosybrown", "blue", "blueviolet", "brown");
@Override
public void start(Stage stage) {
VBox box = new VBox();
list.setOrientation(Orientation.HORIZONTAL);
Scene scene = new Scene(box, 200, 200);
stage.setScene(scene);
stage.setTitle("ListViewSample");
box.getChildren().addAll(list);
VBox.setVgrow(list, Priority.ALWAYS);
list.setItems(data);
list.setCellFactory(new Callback<ListView<String>, ListCell<String>>() {
@Override
public ListCell<String> call(ListView<String> list) {
return new ColorRectCell();
}
});
stage.show();
}
static class ColorRectCell extends ListCell<String> {
SwingNode swingNode = new SwingNode();
@Override
public void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
Text t = new Text(item);
VBox vb = new VBox();
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
swingNode.setContent( new JButton("jbutton"));
}
});
vb.getChildren().addAll(t, swingNode);
if (item != null) {
setGraphic(vb);
}
}
}
public static void main(String[] args) {
launch(args);
}
}
Run
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package test;
import java.awt.event.MouseListener;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.embed.swing.SwingNode;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javax.swing.JButton;
import javax.swing.SwingUtilities;
public class SwingFX extends Application {
Button botton=new Button("java fx button");
JButton jb ;
@Override
public void start(Stage stage) {
final SwingNode swingNode = new SwingNode();
createAndSetSwingContent(swingNode);
VBox vb=new VBox();
botton.setOnMouseClicked(new EventHandler(){
@Override
public void handle(Event event) {
SwingUtilities.invokeLater(new Runnable(){
public void run(){
jb.setEnabled(!jb.isEnabled());
}
});
}
});
StackPane pane = new StackPane();
pane.getChildren().add(swingNode);
vb.getChildren().addAll(pane,botton);
stage.setScene(new Scene(vb, 100, 50));
stage.show();
}
private void createAndSetSwingContent(final SwingNode swingNode ) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
jb =new JButton("swing button");
jb.addMouseListener(new MouseListener(){
@Override
public void mouseClicked(java.awt.event.MouseEvent e) {
Platform.runLater(new Runnable(){
public void run(){
botton.setDisable(!botton.isDisable());
}
});
}
@Override
public void mousePressed(java.awt.event.MouseEvent e) {
//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void mouseReleased(java.awt.event.MouseEvent e) {
//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void mouseEntered(java.awt.event.MouseEvent e) {
//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void mouseExited(java.awt.event.MouseEvent e) {
//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
});
swingNode.setContent(jb);
}
});
}
public static void main(String[] args) {
launch(args);
}
}
Run
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package test;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import java.awt.Dimension;
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.embed.swing.SwingNode;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javax.swing.JButton;
import javax.swing.SwingUtilities;
/**
*
* @author zbp
*/
public class bug2 extends Application {
Stage dialogStage = null;
final ScrollPane sp = new ScrollPane();
Dimension dialogSize = new Dimension(700, 200);
@Override
public void start(Stage primaryStage) {
dialogStage = primaryStage;
ini();
dialogStage.setResizable(false);
dialogStage.show();
}
private void ini() {
HBox box = new HBox();
Group rootGroup = new Group();
HBox hbb = new HBox();
hbb.setSpacing(5);
for (int i = 1; i < 6; i++) {
VBox vb = new VBox();
Text mapNameLabel = new Text("title "+i+" ");
final SwingNode sn = new SwingNode();
createSwingComponent(sn);
StackPane pane = new StackPane();
pane.getChildren().add(sn);
vb.getChildren().addAll(mapNameLabel, pane);
hbb.getChildren().add(vb);
}
sp.setContent(hbb);
sp.setPrefSize(dialogSize.width, dialogSize.height);
BorderPane bp = new BorderPane();
Button left = new Button("<");
Button right = new Button(">");
left.setPrefHeight(dialogSize.height);
right.setPrefHeight(dialogSize.height);
bp.setLeft(left);
bp.setRight(right);
bp.setCenter(sp);
box.getChildren().addAll(bp);
rootGroup.getChildren().add(box);
Scene scene = new Scene(rootGroup, dialogSize.width + 50, dialogSize.height);
dialogStage.setScene(scene);
}
private void createSwingComponent(final SwingNode swingNode) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JButton jb = new JButton("+");
jb.setPreferredSize(new Dimension(dialogSize.height, dialogSize.height - 45));
jb.setMinimumSize(new Dimension(dialogSize.height, dialogSize.height - 45));
swingNode.setContent(jb);
}
});
}
public static void main(String[] s) {
launch(s);
}
}
Run 1
Run 2
Run 3
- duplicates
-
JDK-8097328 [SwingNode]: Swing Nodes have serious rendering artifacts when first displayed on the primary Stage
- Resolved