Inside the Code
When you work with the JavaFX Data Source component, as well as the JavaFX Composer, you'll notice that you don't need to manually write any code. However, there may be times you'd like to go further than this example or what the Composer allows you to do. Fortunately, you can take a look at the raw JavaFX Script that's generated, which you can use as a starting point in the future. To do this, switch the file to Source mode in NetBeans, and expand the "Generated Code" section in the listing.
As you scroll through the code, you'll find the object declarations for the components you dragged onto the Scene. For instance, the code below is for the two list view controls you added, and includes the bind statements that were generated by the Composer:
public-read def listView: ListView = ListView {
layoutX: 17.0
layoutY: 35.0
layoutInfo: __layoutInfo_listView
items: bind for(record in jdbcBooks.getRecordSet().all()) /
record.getString("NAME")
}
public-read def listView2: ListView = ListView {
layoutX: 288.0
layoutY: 35.0
layoutInfo: __layoutInfo_listView2
items: bind for(record in jdbcPatron.getRecordSet().all()) /
"{record.getString("FIRSTNAME")} {record.getString("LASTNAME")}"
}
Connecting the two JDBC data source components involves only two straightforward object declarations:
public-read def jdbcBooks: DbDataSource = DbDataSource {
connectionString: "jdbc:derby:/db/LibraryApp/librarydb"
user: "dbuser"
password: "dbuser"
query: "select * from APP.BOOK"
}
public-read def jdbcPatron: DbDataSource = DbDataSource {
connectionString: "jdbc:derby:/db/LibraryApp/librarydb"
user: "dbuser"
password: "dbuser"
query: "select * from APP.PATRON"
}
Since JavaFX Script is a declarative language (as opposed to an imperative language), you simply state what you want to do, and the run-time takes care of how to do it. The rest of the code includes labels and the button, along with some layout, but the JavaFX Script object declarations and the bind statements that connect them do all of the work. Listing 1 contains the the source for the entire application.
package desktopapplication;
/**
* @author ericjbruno
*/
public class Main {
def __layoutInfo_listView: javafx.scene.layout.LayoutInfo = javafx.scene.layout.LayoutInfo {
width: 254.0
}
public-read def listView: javafx.scene.control.ListView = javafx.scene.control.ListView {
layoutX: 17.0
layoutY: 35.0
layoutInfo: __layoutInfo_listView
items: bind for(record in jdbcBooks.getRecordSet().all()) record.getString("NAME")
}
def __layoutInfo_listView2: javafx.scene.layout.LayoutInfo = javafx.scene.layout.LayoutInfo {
width: 178.0
}
public-read def listView2: javafx.scene.control.ListView = javafx.scene.control.ListView {
layoutX: 288.0
layoutY: 35.0
layoutInfo: __layoutInfo_listView2
items: bind for(record in jdbcPatron.getRecordSet().all()) "{record.getString("FIRSTNAME")} {record.getString("LASTNAME")}"
}
public-read def label: javafx.scene.control.Label = javafx.scene.control.Label {
layoutX: 17.0
layoutY: 13.0
text: "Library Books:"
}
public-read def label2: javafx.scene.control.Label = javafx.scene.control.Label {
layoutX: 288.0
layoutY: 13.0
text: "Library Patrons:"
}
public-read def btnCheckOut: javafx.scene.control.Button = javafx.scene.control.Button {
layoutX: 395.0
layoutY: 293.0
text: "Check Out"
}
public-read def scene: javafx.scene.Scene = javafx.scene.Scene {
width: 480.0
height: 320.0
content: getDesignRootNodes ()
}
public-read def jdbcBooks: org.netbeans.javafx.datasrc.DbDataSource = org.netbeans.javafx.datasrc.DbDataSource {
connectionString: "jdbc:derby:/db/LibraryApp/librarydb"
user: "dbuser"
password: "dbuser"
query: "select * from APP.BOOK"
}
public-read def jdbcPatron: org.netbeans.javafx.datasrc.DbDataSource = org.netbeans.javafx.datasrc.DbDataSource {
connectionString: "jdbc:derby:/db/LibraryApp/librarydb"
user: "dbuser"
password: "dbuser"
query: "select * from APP.PATRON"
}
init {
}
public-read def currentState: org.netbeans.javafx.design.DesignState = org.netbeans.javafx.design.DesignState {
names: [ ]
timelines: [
]
}
public function getDesignRootNodes (): javafx.scene.Node[] {
[ listView, listView2, label, label2, btnCheckOut, ]
}
public function getDesignScene (): javafx.scene.Scene {
scene
}
}
function run (): Void {
var design = Main {};
javafx.stage.Stage {
title: "Main"
scene: design.getDesignScene ()
}
}
Conclusion
When working with JavaFX Script alone, performing tasks such as UI layout and database connectivity are very straightforward, and far less complicated compared with Java and Swing. Additionally, NetBeans and the JavaFX Composer take it a step further, and make building applications -- even database applications -- as simple as drag-and-drop. As with most frameworks, there's always some coding involved, but in this case it amounted to modifying two lines of code (the two bind statements). Working with files and web-based data in comma-delimited, line-deliminted, XML, or JSON formats are equally as straightforward. I plan to extend this article to cover those two formats in the near future.


