You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* Remove controlsfx dialogs and use the now included JavaFX dialogs instead.
* Reference the updated example downloads (v1.1).
* Add an info box for `setCellValueFactory` when used with `IntegerProperty` and `DoubleProperty` in part 2.
* Correct some minor typos.
description: "Learn how to set up a JavaFX project. This is part one of a seven-part tutorial about designing, programming and deploying an address application with JavaFX."
@@ -40,8 +40,8 @@ sidebars:
40
40
paging: 7
41
41
- header: "Download Sources"
42
42
body:
43
-
- text: Part 1 as Eclipse Project <em>(requires at least JDK 8u20)</em>
* Eclipse 4.3 or greater with e(fx)clipse plugin. The easiest way is to download the preconfigured distro from the [e(fx)clipse website](http://efxclipse.bestsolution.at/install.html#all-in-one). As an alternative you can use an [update site](http://www.eclipse.org/efxclipse/install.html) for your Eclipse installation.
90
-
*[Scene Builder 2.0](http://www.oracle.com/technetwork/java/javase/downloads/javafxscenebuilder-info-2157684.html) or greater
89
+
* Eclipse 4.4 or greater with e(fx)clipse plugin. The easiest way is to download the preconfigured distro from the [e(fx)clipse website](http://efxclipse.bestsolution.at/install.html#all-in-one). As an alternative you can use an [update site](http://www.eclipse.org/efxclipse/install.html) for your Eclipse installation.
90
+
*[Scene Builder 8.0](http://gluonhq.com/products/downloads/) (provided by Gluon because [Oracle only ships it in source code form](http://www.oracle.com/technetwork/java/javase/downloads/sb2download-2177776.html)).
91
91
92
92
93
93
### Eclipse Configurations
@@ -168,6 +168,8 @@ Right-click on the view package and create a new *FXML Document* called `PersonO
168
168
169
169
Right-click on `PersonOverview.fxml` and choose *Open with Scene Builder*. Now you should see the Scene Builder with just an *AncherPane* (visible under Hierarchy on the left).
170
170
171
+
(If Scene Builder does not open, go to *Window | Preferences | JavaFX* and set the correct path to your Scene Builder installation).
172
+
171
173
1. Select the *Anchor Pane* in your Hierarchy and adjust the size under Layout (right side):
6. Select the *TableView* choose *constrained-resize* for the *Column Resize Policy* (under Properties). This ensures that the colums will always take up all available space.
187
+
6. Select the *TableView*and choose *constrained-resize* for the *Column Resize Policy* (under Properties). This ensures that the colums will always take up all available space.
10. Add the three buttons at the bottom. Tip: Select all of them, right-click and call *Wrap In | HBox*. This groups them together. You might need to specify a *spacing* inside the HBox. Then, also set anchors (right and bottom) so they stay in the right place.
200
+
10. Add a *ButtonBar*at the bottom. Add three buttons to the bar. Now, set anchors (right and bottom) to the *ButtonBar*so it stays in the right place.
* With JavaFX it's common to use [`Properties`](http://docs.oracle.com/javase/8/javafx/api/javafx/beans/property/Property.html) for all fields of a model class. A `Property`allow us, for example, to automatically be notified when the `lastName` or any other variable is changed. This helps us keep the view in sync with the data. To learn more about `Properties` read [Using JavaFX Properties and Binding](http://docs.oracle.com/javase/8/javafx/properties-binding-tutorial/binding.htm).
219
+
* With JavaFX it's common to use [`Properties`](http://docs.oracle.com/javase/8/javafx/api/javafx/beans/property/Property.html) for all fields of a model class. A `Property`allows us, for example, to automatically be notified when the `lastName` or any other variable is changed. This helps us keep the view in sync with the data. To learn more about `Properties` read [Using JavaFX Properties and Binding](http://docs.oracle.com/javase/8/javafx/properties-binding-tutorial/binding.htm).
220
220
*[`LocalDate`](http://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html), the type we're using for `birthday`, is part of the new [Date and Time API for JDK 8](http://docs.oracle.com/javase/tutorial/datetime/iso/).
221
221
222
222
@@ -279,7 +279,7 @@ From those collections, we need the `ObservableList`. To create a new `Observabl
279
279
280
280
Now let's finally get some data into our table. We'll need a controller for our `PersonOverview.fxml`.
281
281
282
-
1. Create a normal class inside the **view** package called `PersonOverviewController.java`. (We must put it in the same package as the `PersonOverview.fxml`, otherwise the SceneBuilder won't find it - at least not in the current version).
282
+
1. Create a normal class inside the **view** package called `PersonOverviewController.java`. (We must put it in the same package as the `PersonOverview.fxml`, otherwise the SceneBuilder won't find it.
283
283
2. We'll add some instance variables that give us access to the table and the labels inside the view. The fields and some methods have a special `@FXML` annotation. This is necessary for the fxml file to have access to private fields and private methods. After we have everything set up in the fxml file, the application will automatically fill the variables when the fxml file is loaded. So let's add the following code:
284
284
285
285
<divclass="alert alert-info">
@@ -363,6 +363,20 @@ Now this code will probably need some explaining:
363
363
* The `setCellValueFactory(...)` that we set on the table colums are used to determine which field inside the `Person` objects should be used for the particular column. The arrow `->` indicates that we're using a Java 8 feature called *Lambdas*. (Another option would be to use a [PropertyValueFactory](http://docs.oracle.com/javase/8/javafx/api/), but this is not type-safe).
364
364
365
365
366
+
<divclass="alert alert-info">
367
+
<p>
368
+
We're only using `StringProperty` values for our table columns in this example. When you want to use `IntegerProperty` or `DoubleProperty`, the `setCellValueFactory(...)` must have an additional `asObject()`:
This is necessary because of a bad design decision of JavaFX (see <a href="https://community.oracle.com/thread/2575601">this discussion</a>).
376
+
</p>
377
+
</div>
378
+
379
+
366
380
### Connecting MainApp with the PersonOverviewController
367
381
368
382
The `setMainApp(...)` method must be called by the `MainApp` class. This gives us a way to access the `MainApp` object and get the list of `Persons` and other things. Replace the `showPersonOverview()` method with the following. It contains two additional lines:
@@ -380,13 +394,13 @@ public void showPersonOverview() {
@@ -291,14 +291,7 @@ There will be an `ArrayIndexOutOfBoundsException` because it could not remove a
291
291
292
292
To ignore such an error is not very nice, of course. We should let the user know that he/she must select a person before deleting. (Even better would be if we disabled the button so that the user doesn't even have the chance to do something wrong.)
293
293
294
-
We'll add a popup dialog to inform the user. You'll need to **add a library** for the [Dialogs](/blog/javafx-8-dialogs/):
295
-
296
-
1. Download this [controlsfx-8.0.6_20.jar](https://github.com/marcojakob/tutorial-javafx-8/releases/download/v1.0/controlsfx-8.0.6_20.jar) (you could also get it from the [ControlsFX Website](http://fxexperience.com/controlsfx/)).
297
-
**Important: The ControlsFX must be version `8.0.6_20` or greater to work with `JDK 8u20` and above as there was a breaking change introduced in that version.**
298
-
2. Create a **lib** subfolder in the project and add the controlsfx-jar file to this folder.
299
-
3. Add the library to your project's **classpath**: In Eclipse *right-click on the jar file* | *Build Path* | *Add to Build Path*. Now Eclipse knows about the library.
With some changes made to the `handleDeletePerson()` method, we can show a simple popup dialog whenever the user pushes the delete button while no person is selected in the table:
alert.setContentText("Please select a person in the table.");
317
+
318
+
alert.showAndWait();
324
319
}
325
320
}
326
321
</pre>
327
322
328
323
<divclass="alert alert-info">
329
-
For more examples on how to use Dialogs read <aclass="alert-link"href="/blog/javafx-8-dialogs/">JavaFX 8 Dialogs</a>.
324
+
For more examples on how to use Dialogs read my blog post about <aclass="alert-link"href="/blog/javafx-dialogs-official/">JavaFX Dialogs</a>.
330
325
</div>
331
326
332
327
333
-
334
328
*****
335
329
336
330
337
331
## The New and Edit Dialogs
338
332
339
-
The new and edit actions are a bit more work: We'll need a custom dialog (i.e. a new stage) with a form to ask the user for details about the person.
333
+
The new and edit actions are a bit more work: We'll need a custom dialog (that means a new stage) with a form to ask the user for details about the person.
340
334
341
335
342
336
### Design the Dialog
@@ -347,8 +341,6 @@ The new and edit actions are a bit more work: We'll need a custom dialog (i.e. a
347
341
2. Use a `GridPane`, `Label`s, `TextField`s and `Button`s to create a Dialog like the following:
@@ -329,7 +329,7 @@ It looks much nicer with a custom icon:
329
329
330
330
### The Icon File
331
331
332
-
A possible place to get free icons is [Icon Finder](http://www.iconfinder.com). I downloaded a little [address book icon](http://www.iconfinder.com/icondetails/86957/32/).
332
+
A possible place to get free icons is [Icon Finder](http://www.iconfinder.com). I downloaded a little [address book icon](https://www.iconfinder.com/icons/86957/address_book_icon#size=32).
333
333
334
334
Create a (normal) folder inside your AddressApp project called **resources** and a subfolder called **images** in it. Put the icon of your choice inside the images folder. Your folder structure should look something like this now:
335
335
@@ -373,7 +373,7 @@ In [Tutorial Part 5](/java/javafx-8-tutorial-part5/) we will add XML storage for
373
373
374
374
##### Some other articles you might find interesting
0 commit comments