6.8 Graphics Supplement

Now you see it; now you don’t.

—COMMON MAGICIAN’S SAYING

In this section we show you how to add buttons and icons to a JavaFX application. A button is an object in your application that has a button-like appearance and that does something when you click it with your mouse. An icon is a small picture.

Adding Buttons

You create a button object in the same way that you create a label object, but you use the class Button instead of the class Label. import the Button class from javafx.scene.control.Button. For example, the application in Listing 6.21 creates a button as follows:

Listing 6.21 Adding Buttons to JavaFx Application

Program Output

If the user clicks either of these buttons, nothing happens.

Button btnSunny ...

Get Java: An Introduction to Problem Solving and Programming, 8th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.