0

I have google'd around a bit without an answer. In FXML I know how to reference a css, a style, etc using the styleClass and style tags. I'd like to know if it is possible to reference a single css variable adhoc.

For instance, if I want to set the padding of a pane is it possible to achieve the following, or something similar:

example.css

/* ---------- Constants ---------- */
*{
    margin_small: 1.0em;
    margin_large: 2.0em;
}

example fxml

<padding>
    <Insets bottom="margin_small" left="margin_small" right="margin_small" top="margin_large" />
</padding>

The alternative would be to make a css style for every combination of these, or to reference them with the style tag. I'd prefer to avoid both of those options.

Is this possible ?

amdixon
  • 3,814
  • 8
  • 25
  • 34
Kevin
  • 1,626
  • 16
  • 30
  • You cannot do what you are trying to do in the sample code in your question. Maybe the information here will help: [using em units in FXML via expression binding](http://stackoverflow.com/a/23706030/1155209), but I am not sure because you have not [explained what the problem you are trying to solve is](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). – jewelsea May 04 '15 at 10:07
  • Thanks for the reply. There isn't a specific problem, I would just like a tidy way of using a global variable for things like margins. And since the fxml is responsible for the layout, it would be messy to put those constants or logic in the controllers. I essentially want to do what is possible in android's xml, where you can reference a values file. But in javafx it would be css instead of values – Kevin May 04 '15 at 10:12
  • Finally decided to create a POJO with a property for each constant I want to reference. Then in each FXML file I define an instance of the POJO and reference them that way. Not as neat as I'd like, but still a lot better than having it in each controller. And way better than hardcoding – Kevin May 07 '15 at 12:27
  • You might want to post the details or sample code for your solution as a [self-answer](http://stackoverflow.com/help/self-answer). – jewelsea May 07 '15 at 17:04

1 Answers1

0

I couldn't get to the solution I actually wanted, I would've hoped to be able to include a file (fxml, css, values, etc) and reference directly. The best I could do is to create a POJO with a property for each constant, then defining an instance of the POJO in fxml.

The problem with this is each fxml will be creating a new instance of a class, which is a little wasteful seeing as the constants are static in nature.

The following is what I did:

FXMLConstants.java

// Class instance containing global variables to be referenced in fxml files. This is to allow us to use constants similarly to how Android's xml structure does
public class FXMLConstants
{
    private static final DoubleProperty marginSmall = new SimpleDoubleProperty(10);
    private static final DoubleProperty marginMedium = new SimpleDoubleProperty(15);
    private static final DoubleProperty marginLarge = new SimpleDoubleProperty(25);

    public Double getMarginSmall()
    {
        return marginSmall.getValue();
    }

    public Double getMarginMedium()
    {
        return marginMedium.getValue();
    }

    public Double getMarginLarge()
    {
        return marginLarge.getValue();
    }
}

Example.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import java.net.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<?import com.example.FXMLConstants?>

<GridPane xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">

    <fx:define>
        <FXMLConstants fx:id="fxmlConsts" />
    </fx:define>

    <padding>
        <Insets bottom="$fxmlConsts.marginMedium" left="$fxmlConsts.marginLarge" right="$fxmlConsts.marginLarge" top="$fxmlConsts.marginMedium" />
    </padding>

    <children>
        <Label text="Test" GridPane.columnIndex="0" GridPane.rowIndex="0" />
    </children>
</GridPane>
Kevin
  • 1,626
  • 16
  • 30