0

I'm trying to create a utility method to create and show a JavaFx Application with a Pane as the parameter. It doesn't have to be the "right" way to run a javaFx application as it is really just a method for quickly testing how a pane looks.

Here is the java swing equivalent of what I'm trying to do:

The utility class with a method to create and show a JFrame with a passed in component (ie. JPanel):

public class FrameUtility {

public static void createAndShowJFrame(JComponent component) {
        JFrame frame = new JFrame();

        frame.add( component );

        frame.pack();
        frame.setVisible(true);
    }
}

Example usage:

public class CustomPanel extends JPanel {

    //Custom Panel Code....

    //Test method
    public static void main(String[] args) {
        FrameUtility.createAndShowJFrame( new CustomPanel() );
    }
}

Is this possible in JavaFx?

  • Is this utility method meant to be part of a larger application or just a test application? – Slaw Feb 22 '19 at 17:03
  • 1
    One way or another one class needs to be recompiled. Why not simply adjust the right hand side of a line `Parent root = someNode;` in the `start` method? Otherwise https://stackoverflow.com/questions/24611789/how-to-pass-parameters-to-javafx-application – fabian Feb 22 '19 at 17:14
  • @Slaw The utility method would be in a "common" library used in other various modules/projects. I was trying to avoid having to deal with adding padding/layout managers and dealing with cleaning up extra imports – Rikaateabug Feb 24 '19 at 01:43
  • Well, from the Swing code you posted, the equivalent JavaFX code would be to create `Scene` with the `Pane` as the root, create a `Stage`, set the `Scene` on the `Stage`, and show the `Stage` (i.e. `stage.show()`). If you don't specify dimensions when creating the `Scene` it will use the preferred size of the root. Note the root only need be a `Parent`, which `Pane` is a subclass of. – Slaw Feb 24 '19 at 02:01

1 Answers1

0

Create a new stage and pass the pane as a parameter as such. I suppose that you'd have to overload this method for for different types of pane, but it's a quick test method like you describe.

public void tryThisPane(BorderPane pane){
    Stage stage = new Stage();
    Scene scene = new Scene(pane);
    stage.setScene(scene);
    stage.show();
}
Dustin
  • 693
  • 8
  • 20