Java Reference
In-Depth Information
methods and objects when needed (to get and set text). By default, listeners are inlined with anony-
mous inner classes, but you can change this to real listener objects as well. Be careful when changing
around too many things, though, as WindowBuilder might then not be able to convert your changes
to a GUI layout it understands. This immediately illustrates both the advantages and disadvantages of
visual GUI designers. They provide a great, easy method to quickly prototype a GUI (image building
the GUI above by hand), but can lose their head once you start making too many custom changes.
In general, especially when you're starting out, you are advised not to fall into the luring trap of visual
designers promising rapid development. If you can construct your GUI with some nimble nested com-
ponents and simple layout managers, then by all means go ahead. If you do need to build complex
forms and don't want to waste time laying out everything by hand, WindowBuilder provides a great
alternative. The designer is also a helpful way to get acquainted quickly with unfamiliar components.
Finally, it is worth noting that you can also download other plug‐ins to make WindowBuilder aware
of GUI toolkits other than AWT/Swing, such as SWT. Other IDEs (Netbeans especially) also come
with great visual designers, and there are some commercial ones as well (Jvider, JFormDesigner, and
others).
Javafx: the road ahead?
Swing has been around for a long time, and although the toolkit is immensely robust, it is starting to
show its age. You have seen this already when talking about custom painting, or when taking a look
at some of the . . . less nice‐looking look and feels. With high‐resolution retina displays, smartphones,
tablets, rich Internet applications, and hardware‐accelerated desktop compositors, the Java commu-
nity started to desire a more modern UI toolkit. JavaFX was kickstarted to answer this need, and the
UI toolkit has been around since 2008. With the release of Java 8, JavaFX became an integral part of
the JRE (and JDK), so that the latest version of JavaFX went from 2.2 to simply JavaFX 8.
Sadly, this beginner book can't go into depth on all aspects of JavaFX, but when you are familiar with
Swing, switching to JavaFX, should you want to, doesn't pose too many hurdles. Answering the ques-
tion whether you should use JavaFX is not so straightforward. If you're itching to use the latest and
greatest, then by all means go ahead. Keep in mind, however, that the community surrounding JavaFX
is still young, so it will be much easier to find Swing‐related libraries, support, and help when you need
it. Finally, the multitude of GUIs in Java were built and are still being built with Swing, so if you're
looking to apply your skills in a real‐life setting, you still need to be acquainted with Swing.
The chapter concludes with two example projects built with JavaFX. In the first example, you'll
learn how to build the BMI calculator again. If you're following along, you can just copy the old
BMICalculator class and throw out anything that has to do with AWT or Swing. You'll also see a
few things commented out so you don't forget to deal with them later:
public class BMICalculatorFX {
//private final JTextField txtMass = makePrettyTextField();
//private final JTextField txtHeight = makePrettyTextField();
//private final JButton btnCalc = makePrettyButton("Calculate BMI");
public BMICalculatorFX() {
/*
btnCalc.addActionListener(new ActionListener() {
 
Search WWH ::




Custom Search