Results 1 to 3 of 3
  1. #1
    s22380 is offline Member
    Join Date
    Aug 2016
    Posts
    4
    Rep Power
    0

    Default Make a JavaFX application work in JavaFXML

    I'm trying to get JavaFX applications to work in my JavaFXML application but this never works. Most tutorials, examples, guides are presented in JavaFX application, it would be nice to get it to work.
    So how can I get this nice sample code to work in my JavaFXML application initialize method below:

    Java FX Code:
    @Override
        public void initialize(URL url, ResourceBundle rb) {
            // TODO
        }

    The JAVAFX application sample code:

    Java FX Code:
    package javafxapplication13;
    
      import javafx.application.Application;
    import javafx.geometry.HPos;
    import javafx.geometry.VPos;
    import javafx.scene.*;
    import javafx.scene.control.*;
    import javafx.scene.layout.*;
    import javafx.stage.Stage;
    
    public class JavaFXApplication13 extends Application {
      @Override public void start(final Stage stage) {
        // create a grid with some sample data.
        GridPane grid = new GridPane();
        grid.addRow(0, new Label("1"), new Label("2"), new Label("3"));
        grid.addRow(1, new Label("A"), new Label("B"), new Label("C"));
        
        // make all of the Controls and Panes inside the grid fill their grid cell, 
        // align them in the center and give them a filled background.
        // you could also place each of them in their own centered StackPane with 
        // a styled background to achieve the same effect.
        for (Node n: grid.getChildren()) {
          if (n instanceof Control) {
            Control control = (Control) n;
            control.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
            control.setStyle("-fx-background-color: cornsilk; -fx-alignment: center;");
          }
          if (n instanceof Pane) {
            Pane pane = (Pane) n;
            pane.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
            pane.setStyle("-fx-background-color: cornsilk; -fx-alignment: center;");
          }
        }
    
        // style the grid so that it has a background and gaps around the grid and between the 
        // grid cells so that the background will show through as grid lines.
        grid.setStyle("-fx-background-color: palegreen; -fx-padding: 2; -fx-hgap: 2; -fx-vgap: 2;");
        // turn layout pixel snapping off on the grid so that grid lines will be an even width.
        grid.setSnapToPixel(false);
    
        // set some constraints so that the grid will fill the available area.
        ColumnConstraints oneThird = new ColumnConstraints();
        oneThird.setPercentWidth(100/3.0);
        oneThird.setHalignment(HPos.CENTER);
        grid.getColumnConstraints().addAll(oneThird, oneThird, oneThird);
        RowConstraints oneHalf = new RowConstraints();
        oneHalf.setPercentHeight(100/2.0);
        oneHalf.setValignment(VPos.CENTER);
        grid.getRowConstraints().addAll(oneHalf, oneHalf);
        
        // layout the scene in a stackpane with some padding so that the grid is centered 
        // and it is easy to see the outer grid lines.
        StackPane layout = new StackPane();
        layout.setStyle("-fx-background-color: whitesmoke; -fx-padding: 10;");
        layout.getChildren().addAll(grid);
        stage.setScene(new Scene(layout, 600, 400));
        stage.show();
        
        // can be uncommented to show the grid lines for debugging purposes, but not particularly useful for styling purposes.
        //grid.setGridLinesVisible(true);
      }
      public static void main(String[] args) { launch(); }
    }
    Thanks for your help!

  2. #2
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    398
    Rep Power
    5

    Default Re: Make a JavaFX application work in JavaFXML

    When you start your project, you have to make up your mind whether you want to create the GUI within the Java class, as you did above, or whether you want to use an FXML file. For the case of a FXML setup your project has an FXML file and a (Java) FXMLLoader handles the interaction with that file. The work on FXML files is usually done with "Scenebuilder". The FXML file defines a "Controller Class" which allows you to interact with the FXML GUI from Java.
    The "how to" was briefly discussed here in a different thread.

    P.S.: the setStyle() method can only be used to define a CSS style sheet to be used with this specific node.
    Last edited by benji2505; 08-03-2016 at 10:28 PM.

  3. #3
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    398
    Rep Power
    5

    Default Re: Make a JavaFX application work in JavaFXML

    I stand corrected: I tried to use the setStyle() method to set a node style with fxml and it worked. My understanding was that it can only be used to set a style in reference to a css sheet. So, error on my end. In reference to your other thread I also tried to turn the gridlines on and off with the setStyle method and it worked as well, so the syntax seems to be correct.

Similar Threads

  1. Javafx make arc open and close while moving
    By Markmywoods in forum New To Java
    Replies: 3
    Last Post: 05-01-2016, 09:21 PM
  2. Replies: 0
    Last Post: 12-07-2014, 06:27 PM
  3. how to make jar file for javafx in eclipse?
    By sandeep43 in forum JavaFX
    Replies: 1
    Last Post: 05-10-2012, 08:44 AM
  4. How to intall my javaFX application on mobile
    By abuhmead1987 in forum JavaFX
    Replies: 1
    Last Post: 09-30-2010, 05:27 PM
  5. problem with playing video in javafx application
    By kiranblighty in forum JavaFX
    Replies: 1
    Last Post: 02-27-2010, 12:09 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •