npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

fxml2j

v0.1.2

Published

JavaFX FXML parser and corresponding Java code generator.

Downloads

12

Readme

fxml2j

Purpose

This library is for generating code for JavaFX applications which use FXML for UI layout.

While FXML is a clean and convenient format for representing the UI layout of the application, current tooling (i.e. - SceneBuilder, et al) do not allow you to synchronize the FXML code to the underlying JavaFX Controller code. The underlying Java code can be quite verbose, and is tedious to manually write.

The 'fxml2j' library will BOTH generate an implementation of the Java Controller class, as well as show you ongoing "diffs" between your FXML and Controller class as each of these two code artifacts evolves. The library will discover the fx:id elements, and #-prefixed action methods, and generate the appropriate @FXML annotated Java code.

Examples

Setup

Add fxml2j to your project or package.json file:

npm install fxml2j

Require fxml2j in your code:

Fxml2j  = require("fxml2j").Fxml2j

Create your 'fxml2j.json' configuration file, for example:

{
  "javafx_src_dir": "workspace/ExampleApp/src",
  "fxml_filename":  "com/joakim/example/Example.fxml",

  "generate": true,
  "diff":     true,
  "verbose":  true,

  "multiWordTagMappings": {
    "borderpane":    "BorderPane",
    "combobox":      "ComboBox",
    "gridpane":      "GridPane",
    "tablecolumn":   "TableColumn",
    "tableview":     "TableView",
    "tabpane":       "TabPane",
    "textfield":     "TextField",
    "addadditional": "AddAdditional"
  }
}

Alternatively, rather than reading a JSON file, construct a similar configuration object in your code to pass to Fxml2j.

The 'javafx_src_dir' property should point to the root source directory of your JavaFX codebase. This examples shows an Eclipse workspace location.

The 'fxml_filename' property points to your main FXML file relative to the 'javafx_src_dir'. The current version of fxml2j does not handle fx:include tags, but this will be added in a future release. The Example.fxml is in this GitHub repository.

Only one Controller class is currently supported by fxml2j. This classname is obtained by parsing the FXML and identifying the 'fx:controller'. The generated controller code is written as a *.txt file in the controller directory - see example output filenames below. This is so that it does not overlay the current corresponding *.java file.

Note: this library is implemented with CoffeeScript, and these examples are also in CoffeeScript.

Fxml2j

Using the fxml2j library:

Fxml2j = require("./lib/fxml2j.js").Fxml2j

config_json = fs.readFileSync('fxml2j.json', 'utf-8')
config_obj  = JSON.parse(config_json)

fxml = new Fxml2j(config_obj)
fxml.process()

Example output from the above CoffeeScript code is shown below.

Notice that the differences between the FXML and the correspondingare Java Controller class are identified. The fxml2j library currently requires, for ease of parsing, that your @FXML annotations are "inlined" into your variable and method declaration lines.

fxml parsed; controller: com.joakim.example.ExampleController  ui components: 50
controller_package:   com.joakim.example
controller_classname: ExampleController
controller_filename:  workspace/ExampleApp/src/com/joakim/example/ExampleController.java
file written:         workspace/ExampleApp/src/com/joakim/example/ExampleController.txt
the controller file currently exists, line count: 103

Differences - Add - 10
    @FXML private Label            osNameLabel;
    @FXML private Label            buildTimestampLabel;
    @FXML private TableColumn      partsTableViewFilesize;
    @FXML private ComboBox         webEnvironmentsCbox;
    @FXML public void postAllBtnClicked(ActionEvent e) {
    @FXML private Label            osNameLabel;
    @FXML private Label            buildTimestampLabel;
    @FXML private TableColumn      partsTableViewFilesize;
    @FXML private ComboBox         webEnvironmentsCbox;
    @FXML public void postAllBtnClicked(ActionEvent e) {
Differences - Delete - 1
    @FXML private ComboBox         webEnvironmentsOldnameCbox;

Example FXML code. The 'fx:controller' and 'fx:id' components are parsed by fxml2j, as are the onXxxx="#xxx" event handler method names.

...

<BorderPane maxHeight="-Infinity" maxWidth="-Infinity"
  minHeight="-Infinity" minWidth="-Infinity" prefHeight="650.0"
  prefWidth="1200.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"
  fx:controller="com.joakim.example.ExampleController" >

...

        <Button fx:id="postSelectedBtn" mnemonicParsing="false" prefWidth="200.0"
          onAction="#postSelectedBtnClicked" text="Post Selected" />
        <Label text=" " />

...

Example Generated Java code:


package com.joakim.example;

import javafx.scene.control.BorderPane;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.Text;
import javafx.scene.control.TextField;

public class ExampleController {

    // Instance variables - fx:id UI components:
    
    ...
    
    @FXML private Button           postSelectedBtn;


    ...

    @FXML public void postSelectedBtnClicked(ActionEvent e) {

    }

    ...
}

With these diffs identified, the intent is for you to copy-and-paste the Added code into your controller, while manually deleting the identified Deletions. In cases where the Controller class does not exist, simply rename the generated ".txt" file to ".java".

Release History

  • 2015-03-03   v0.1.2  Fixed main in package.json
  • 2015-02-24   v0.1.1  README updated with example FXML and Java code
  • 2015-02-22   v0.1.0  initial working version