Thread.exit () line: unavailable error in javafx swing integration using jfxpanel

I am trying to use fxml inside a swing application. The problem I am facing is the jframe, but the javafx components are not visible. The error is not listed, but after debugging it gives the error "Thread.exist () line: not available". I have complete code here: -

package nonResponsiveButtons;

import java.awt.Color;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.Pane;
import javafx.application.Platform;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class NonResponsiveButtons extends JFrame {

    public static void main(String[] args) {
        new NonResponsiveButtons();
    }

    public NonResponsiveButtons(){

        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                try{
                new JFXPanel();
                BottomPanel bottomPanel = new BottomPanel();
                add(bottomPanel);
                }
                catch(Exception e){
                    System.out.println("Error in swing utilities thread :" + e.getMessage());
                }

            }
        });

        this.setSize(600, 600);
        getContentPane().setLayout(null);
        add(jPanel());      


        setVisible(true);
    }

    private JPanel jPanel(){
        JPanel panel = new JPanel();
        panel.setSize(600,500);
        panel.setBackground(Color.blue);
        return panel;
    }

    private class BottomPanel extends JPanel{
        private JFXPanel jfxPanel;
        private Pane scenePane;
        private Button btn1;
        private Button btn2;
        private Button btn3;

        private BottomPanel(){
            setSize(600, 100);
            setLocation(0, 500);
            setLayout(null);



            Platform.runLater(new Runnable(){
                @Override
                public void run(){
                    getScenePane().getChildren().addAll(getBtn1(),getBtn2(),getBtn3());
                    getjfxPanel().setScene(new Scene(getScenePane()));

                }

            });
        }

        private JFXPanel getjfxPanel(){
            if(jfxPanel == null){
                jfxPanel = new JFXPanel();
                jfxPanel.setSize(600,200);
            }
            return jfxPanel;
        }

        private Pane getScenePane(){
            if(scenePane == null){
                scenePane = new Pane();
                scenePane.setStyle("-fx-background-color:#666666");
            }
            return scenePane;
        }

        /*
         * using getters will avoid :-
         * 1. null pointer exceptions
         * 2. standard coding format
         * 3. makex programming felxible
         */
        private Button getBtn1(){
            if(btn1 == null){
                btn1 = new Button();
                btn1.setPrefSize(100, 50);
                btn1.setLayoutX(80);
            }
            return btn1;
        }
        private Button getBtn2(){
            if(btn2 == null){
                btn2 = new Button();
                btn2.setPrefSize(100, 50);
                btn2.setLayoutX(80);
            }
            return btn2;
        }
        private Button getBtn3(){
            if(btn3 == null){
                btn3 = new Button();
                btn3.setPrefSize(100, 50);
                btn3.setLayoutX(80);
            }
            return btn3;
        }
    }


}

      

+3


source to share


1 answer


You cannot see JFXPanel

because you have not added JFXPanel

to BottomPanel

. Inside the constructor, BottomPanel

just addadd(getjfxPanel());

Your constructor will look like this:



private BottomPanel(){
    setSize(600, 100);
    setLocation(0, 500);
    setLayout(null);

    Platform.runLater(new Runnable(){
       @Override
       public void run(){
           getScenePane().getChildren().addAll(getBtn1(),getBtn2(),getBtn3());
           getjfxPanel().setScene(new Scene(getScenePane()));
       }
    });
    add(getjfxPanel());
}

      

Note. ... After adding this parameter, you will only be able to see one Button

. No code problem, you add buttons to Pane

, so they are on top of each other. Just replace Pane

with HBox

and you should be able to see all the buttons because it HBox

aligns the controls horizontally, just what you want (maybe;))

0


source







All Articles