JavaFX 8摆动兼容性

时间:2014-04-02 10:14:17

标签: java javafx javafx-8

我目前正在评估使用SwingNode将当前的Swing应用程序转换为JavaFX 8是否可行,然后慢慢将所有内容更改为JavaFX组件。我偶然发现了两个问题,这可能是JavaFX 8中的问题,但我无法确定。

  1. 将内容添加到SwingNode后,不会绘制Javafx菜单 正确(大部分时间),导致黑条
  2. 使JScrollBars不透明似乎不起作用

    import java.awt.Color;
    import java.awt.GridLayout;
    
    import javafx.application.Application;
    import javafx.embed.swing.SwingNode;
    import javafx.scene.Scene;
    import javafx.scene.control.Menu;
    import javafx.scene.control.MenuBar;
    import javafx.scene.layout.StackPane;
    import javafx.scene.layout.VBox;
    import javafx.stage.Stage;
    
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    
    
    public class Test extends Application {    
    
        @Override
        public void start(Stage stage) throws Exception {
            stage.setMaximized(true);
    
            VBox vbox = new VBox();
            MenuBar menuBar = new MenuBar();
            menuBar.getMenus().add(new Menu("bla"));
            vbox.getChildren().add(menuBar);
    
            StackPane mainPane = new StackPane();        
            mainPane.setPrefSize(9999, 9999); //Better way to ensure all space used?
            vbox.getChildren().add(mainPane);
            vbox.setVisible(true);
    
            Scene scene = new Scene(vbox, 9999, 9999); //Better way to ensure all space used?
            stage.setScene(scene);
    
            SwingNode swingNode = new SwingNode();
            mainPane.getChildren().add(swingNode);
    
            JPanel jPanel = new JPanel();
            swingNode.setContent(jPanel); //Seems to interfere with painting of menu
    
            jPanel.setBackground(Color.WHITE);
            jPanel.setLayout(new GridLayout(2, 1));
            jPanel.add(new JPanel());
    
            JPanel nonOpaquePanel = new JPanel();
            nonOpaquePanel.setOpaque(false);
            JScrollPane scrollPane = new JScrollPane(nonOpaquePanel);
            scrollPane.setOpaque(false);
            scrollPane.getViewport().setOpaque(false); //Black background -> Bug?
            jPanel.add(scrollPane);
    
            stage.show();
        }
    
        /**
         * Main method launching the application.
         */
        public static void main(String[] args) {
            launch(args);
        }
    }
    

    Executable jar

  3. 编辑: 我编辑了我的示例,使用EDT创建Swing组件。这并没有改变任何事情。或者我做错了什么?

    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.GridLayout;
    
    import javafx.application.Application;
    import javafx.application.Platform;
    import javafx.embed.swing.SwingNode;
    import javafx.scene.Scene;
    import javafx.scene.control.Menu;
    import javafx.scene.control.MenuBar;
    import javafx.scene.layout.StackPane;
    import javafx.scene.layout.VBox;
    import javafx.stage.Stage;
    
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.SwingUtilities;
    
    
    public class Test extends Application {
        private JPanel jPanel = null;
    
        @Override
        public void start(Stage stage) throws Exception {
            if (Platform.isFxApplicationThread()) { // all of this happens on the fx application thread
                stage.setMaximized(true);
    
                VBox vbox = new VBox();
                MenuBar menuBar = new MenuBar();
                menuBar.getMenus().add(new Menu("bla"));
                vbox.getChildren().add(menuBar);
    
                StackPane mainPane = new StackPane();        
                mainPane.setPrefSize(9999, 9999); //Better way to ensure all space used?
                vbox.getChildren().add(mainPane);
                vbox.setVisible(true);
    
                Scene scene = new Scene(vbox, 9999, 9999); //Better way to ensure all space used?
                stage.setScene(scene);
    
                SwingNode swingNode = new SwingNode();
                mainPane.getChildren().add(swingNode);                    
    
                SwingUtilities.invokeLater(new Runnable() { //Use EDT to build swing components              
                    @Override
                    public void run() {
                        jPanel = new JPanel();
    
                        jPanel.setBackground(Color.WHITE);
                        jPanel.setLayout(new GridLayout(2, 1));
                        jPanel.add(new JPanel());
    
                        JLabel nonOpaquePanel = new JLabel("Bla");
                        nonOpaquePanel.setPreferredSize(new Dimension(5000, 5000));
                        nonOpaquePanel.setOpaque(false);
                        JScrollPane scrollPane = new JScrollPane(nonOpaquePanel);
                        scrollPane.setOpaque(false);
                        scrollPane.getViewport().setOpaque(false); //Black background -> Bug?
                        jPanel.add(scrollPane);
                    }
                });
    
                Thread.sleep(1000); //terrible way to wait for the EDT, I know
                swingNode.setContent(jPanel); //Seems to interfere with painting of menu            
                stage.show();
    
                SwingUtilities.invokeLater(new Runnable() { //I am pretty sure this isn't necessary, but whatever                       
                    @Override
                    public void run() {
                        jPanel.repaint();    
                    }
                });
            }
        }
    
        /**
         * Main method launching the application.
         */
        public static void main(String[] args) {
            launch(args);
        }
    }
    

2 个答案:

答案 0 :(得分:4)

在进行更新调用时,请尝试将更新代码包装在此处。这段代码在其他项目之前帮助了我。

Platform.runLater(new Runnable() {
            @Override
            public void run() {
                // Update the text node with calculated results
            }
       });

Source

答案 1 :(得分:1)

一定是JavaFX中的一个错误,这个例子现在工作正常。