Search icon CANCEL
Subscription
0
Cart icon
Cart
Close icon
You have no products in your basket yet
Save more on your purchases!
Savings automatically calculated. No voucher code required
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Mastering JavaFX 10

You're reading from  Mastering JavaFX 10

Product type Book
Published in May 2018
Publisher Packt
ISBN-13 9781788293822
Pages 268 pages
Edition 1st Edition
Languages
Author (1):
Sergey Grinev Sergey Grinev
Profile icon Sergey Grinev
Toc

Table of Contents (15) Chapters close

Preface 1. Stages, Scenes, and Layout 2. Building Blocks – Shapes, Text, and Controls 3. Connecting Pieces – Binding 4. FXML 5. Animation 6. Styling Applications with CSS 7. Building a Dynamic UI 8. Effects 9. Media and WebView 10. Advanced Controls and Charts 11. Packaging with Java9 Jigsaw 12. 3D at a Glance 13. What's Next? 14. Other Books You May Enjoy

Clock demo

To demonstrate the topics covered in this chapter, I have written a small clock application.

It will become more complex with each upcoming chapter; for the first release it just shows a current local time in text form and updates it every second, demonstrating Stage/Scene usage, one of the layout managers, and the Application FX Thread workflow:

See the inline comments for details about the program:

// chapter1/clock/ClockOne.java
public class ClockOne extends Application {
// we are allowed to create UI objects on non-UI thread
private final Text txtTime = new Text();

private volatile boolean enough = false;

// this is timer thread which will update out time view every second
Thread timer = new Thread(() -> {
SimpleDateFormat dt = new SimpleDateFormat("hh:mm:ss");
while(!enough) {
try {
// running "long" operation not on UI thread
Thread.sleep(1000);
} catch (InterruptedException ex) {}
final String time = dt.format(new Date());
Platform.runLater(()-> {
// updating live UI object requires JavaFX App Thread
txtTime.setText(time);
});
}
});

    @Override
public void start(Stage stage) {
// Layout Manager
BorderPane root = new BorderPane();
root.setCenter(txtTime);

// creating a scene and configuring the stage
Scene scene = new Scene(root, 200, 150);
stage.initStyle(StageStyle.UTILITY);
stage.setScene(scene);

timer.start();
stage.show();
}

// stop() method of the Application API
@Override
public void stop() {
// we need to stop our working thread after closing a window
// or our program will not exit
enough = true;
}

public static void main(String[] args) {
launch(args);
}
}
You have been reading a chapter from
Mastering JavaFX 10
Published in: May 2018 Publisher: Packt ISBN-13: 9781788293822
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}