Java
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Object-Oriented Java
-
Section 4: Collections Framework
-
Section 5: Exception Handling
-
Section 6: Generics
-
Section 7: Functional Java
-
Section 8: Concurrency
-
Section 9: I/O and NIO
-
Section 10: JVM Internals
-
Section 11: Modern Java Features
-
Section 12: Build Tools and Project Structure
-
Section 13: Testing
-
Section 14: Databases and Persistence
-
Section 15: Networking
-
Section 16: Design and Best Practices
-
Section 17: Reflection and Annotations
-
Section 18: Logging and Diagnostics
-
Section 19: Date, Time, and Internationalization
-
Section 20: Java Platform Module System
-
Section 21: Security in Java
-
Section 22: Advanced Collections and Data Structures
-
Section 23: More Concurrency Patterns
-
Section 24: Compression, Files, and System Integration
-
Section 25: GUI Programming
-
Section 26: Practical Projects
-
Section 27: Data Structures and Algorithms
-
Section 28: Interview and Algorithm Practice
-
Section 29: JSON and Data Interchange
-
Section 30: More Concurrency Utilities
-
Section 31: More Collections and Streams Practice
-
Section 32: More File and System Programming
-
Section 33: Standard Library Deep Dive
-
Section 34: More Practice and Drills
-
Section 35: More Testing and Quality
-
Section 36: More Design Patterns and Architecture
-
Section 37: Career and Ecosystem
-
Section 38: More OOP and Architecture Practice
-
Section 39: More Enterprise Concepts
-
Section 40: Advanced JavaFX
-
Section 41: More Interview Practice
211: Building a Simple JavaFX Application
I've seen this happen a lot: a developer comes to me frustrated because their JavaFX app won't start, and when I look at their code, they've tried to instantiate a Stage directly inside the main method. They treat it like any other Java object—you call new Stage(), you set a title, and you call show(). It seems logical, right?
"I can just create a Stage wherever I want"
Here is what that broken approach looks like:
public class MyApp { public static void main(String[] args) { Stage stage = new Stage(); // This will throw an IllegalStateException stage.setTitle("My App"); stage.show(); } }If you run this, Java will scream at you with an
IllegalStateException. Why? Because JavaFX isn't just a collection of classes; it's a toolkit that requires a specific runtime environment to be initialized before any UI components can exist. You can't just "new" a window into existence without the JavaFX platform being alive and kicking in the background.The Application Lifecycle and the Stage-Scene Graph
To get a window on the screen, you have to play by the JavaFX rules. You don't call the UI logic in
main; instead, you extend theApplicationclass and override thestartmethod. When you calllaunch(args)in your main method, JavaFX does the invisible work of spinning up the toolkit and then handing you a primaryStagevia thestartmethod.Think of it as a theater production. The
Stageis the actual window (the physical frame). TheSceneis the current "set" or layout you're showing on that stage. Inside the scene, you have a "Scene Graph"—a tree of nodes like buttons, labels, and text fields. You can't have a scene without a stage, and you can't have a stage without a scene.Building a Simple Tip Calculator
Let's build something slightly more useful than a blank window. We'll create a simple Tip Calculator. This will show you how to layout components and handle a basic event.
import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.*; import javafx.scene.layout.VBox; import javafx.stage.Stage; public class TipCalculator extends Application { @Override public void start(Stage primaryStage) { // 1. Create the UI components Label label = new Label("Enter Bill Amount:"); TextField inputField = new TextField(); Button calcButton = new Button("Calculate 20% Tip"); Label resultLabel = new Label("Tip: $0.00"); // 2. Define the behavior (Event Handling) calcButton.setOnAction(e -> { try { double bill = Double.parseDouble(inputField.getText()); double tip = bill * 0.20; resultLabel.setText(String.format("Tip: $%.2f", tip)); } catch (NumberFormatException ex) { resultLabel.setText("Please enter a valid number!"); } }); // 3. Arrange components in a layout (VBox stacks things vertically) VBox root = new VBox(10); // 10px spacing between elements root.getChildren().addAll(label, inputField, calcButton, resultLabel); // 4. Assemble the Stage and Scene Scene scene = new Scene(root, 300, 200); primaryStage.setTitle("QuickTip"); primaryStage.setScene(scene); primaryStage.show(); } public static void main(String[] args) { launch(args); } }I used a
VBoxhere because it's the quickest way to get a vertical stack. In a real professional app, you'd likely useGridPaneor FXML for more complex layouts, but for a simple tool, keep it lean. Notice how thesetOnActionuses a lambda expression—this is the standard way to handle clicks in JavaFX. It's concise and keeps the logic right where the button is defined.
📋 Practical Task
Build a "Character Counter for Social Media" Application
Your task is to create a JavaFX application that helps a user keep track of how many characters they've typed, specifically for a platform like X (Twitter) which has a character limit.
- Create a window titled "Post Length Checker".
- Include a
TextAreawhere the user can type their post. - Include a
Labelthat updates in real-time (or via a button) to show the current character count. - Bonus: Make the label text turn red if the character count exceeds 280 characters.
Remember to extend Application and use the start method to initialize your UI components and scene.
There are no comments for now.