I recently started learning the JavaFX API, after I already experience in Swing.
I noticed, that even a lot of classes were already well implemented in AWT and Swing, they were effectively re-implemented in JavaFX. This includes:
javafx.scene.paint.Color
javafx.event.ActionEvent
vs.
java.awt.Color
java.awt.event.ActionEvent
and much more, even though it could've easily require to use them. I assume that this is to:
- Decouple JavaFX the most possible, from the other libraries (so new developers shouldn't even know of their existence..., OK).
- Leverage Java 8 lambda expressions.
- Make use of Java 5 generics and enum types.
- Design with FXML in mind.
- Bindings... JavaFX's version of magic.
If my assumptions are true, why didn't they include a new implementation of:
javax.swing.undo
package?
Although I understand that undo has really nothing to do with the user interface, so, it has nothing to do with Swing too. If for any reason they decided to include it in the javax.swing
package, so could they include it in JavaFX.
Why they "forgot" to implement this, is a good question. I would argue, that JavaFX is still in development (that should say everything). However, I needed this long ago and I implemented my own approach using the Command Pattern. As shown below, this is not much effort and very simple.
First you will need to create a Interface called Command, to execute some operations in your application.
Next you will need some class called History to save your executed commands and to undo them.
In your FXML you then create a button for your GUI which should invoke the undo function of your application. In your FXML create a button, such as the following one:
In your controller class you reference the button from your FXML.
As you can see, the best thing is that the History class is a Singleton. So you can access the class from everywhere.
Inherit from Command interface to implement a new command. Use some buttons or similar GUI elements for new functionality and execute the custom command using your history.
With this approach you will be able to undo your operation. It is also possible to implement some redo functionality. For that just add a redo button in FXML and the appropriate method in History class and Command interface.
For more information on the command pattern, have a look here.
Happy Coding!