JavaFX and SwingNode - partial black Window

后端 未结 3 423
花落未央
花落未央 2021-01-27 20:49

I tried to use Swing Components in JavaFX with the SwingNode:

public class MyTest extends Application {

    @Override
    public void start(Stage stage) {

             


        
相关标签:
3条回答
  • 2021-01-27 21:20

    I've found an "awful" solution, but that works for me, repainting the Swing component in a delayed way, after showing the Stage:

    new Timer().schedule(new TimerTask() {
            public void run() {
                swingNode.getContent().repaint();
            }
        }, 500L);
    

    so, your code will look like this:

    public class MyTest extends Application {
    
        @Override
        public void start(Stage stage) {
    
            final SwingNode swingNode = new SwingNode();
            FlowPane pane = new FlowPane();
    
            Button btn = new Button("1");
            btn.setVisible(false);
            pane.getChildren().add(btn);
    
            createAndSetSwingContent(swingNode);
            pane.getChildren().add(swingNode);
    
    
            stage.setScene(new Scene(pane, 100, 50));
    
            stage.show();
    
            btn.setVisible(true);
    
            // delays Swing component repaint 1/2 sec.
            new Timer().schedule(new TimerTask() {
                    public void run() {
                        swingNode.getContent().repaint();
                    }
                }, 500L);
    
        }
    
        private void createAndSetSwingContent(final SwingNode swingNode) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    swingNode.setContent(new JButton("Click me!"));
                }
            });
        }
    
        public static void main(String[] args) {
            launch(args);
        }
    }
    

    A bit late, but I hope it helps!

    0 讨论(0)
  • 2021-01-27 21:30

    objSwingNode.requestFocus(); objSwingNode.autosize();

    0 讨论(0)
  • 2021-01-27 21:36

    The Problem is an known bug in java 8 update 31.

    (https://javafx-jira.kenai.com/browse/RT-37810)

    It is fixed in java 8 update 40.

    0 讨论(0)
提交回复
热议问题