JavaFX: Alternate Text On Mouse Click
Hey guys! Today, we're diving into a cool JavaFX project where we'll make text alternate with each mouse click. We’ll be displaying "Welcome to Java" and "LearningJavaFX" each time you click the mouse. Sounds fun, right? Let's get started!
Setting Up the Project
First things first, let's set up our JavaFX project. Make sure you have your IDE ready, like IntelliJ or Eclipse, and that you have the JavaFX libraries configured. If you’re starting from scratch, create a new JavaFX project. This will give you a basic project structure to work with. If you have an existing project, you can just create a new class file for our code.
Creating the Main Class
Now, let's create our main class. This class will extend javafx.application.Application
, which is the entry point for JavaFX applications. We’ll need to override the start
method, where we’ll set up our scene, layout, and handle the mouse click events. Here’s the basic structure:
package com.example.demo;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class AlternateTextApp extends Application {
@Override
public void start(Stage primaryStage) {
// Our code will go here
}
public static void main(String[] args) {
launch(args);
}
}
This code sets up the basic structure for our JavaFX application. We have a class AlternateTextApp
that extends Application
. The start
method is where we’ll build our UI, and the main
method launches the application. Pretty straightforward, huh?
Building the UI
Next, we need to build our user interface. For this project, we’ll use a Label
to display the text and a StackPane
to hold the label. A StackPane
is great because it layers its children on top of each other, which is perfect for our simple text display. Let's add the necessary imports and set up the initial UI elements.
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
@Override
public void start(Stage primaryStage) {
Label textLabel = new Label("Welcome to Java");
StackPane root = new StackPane(textLabel);
Scene scene = new Scene(root, 300, 200);
primaryStage.setTitle("Alternate Text");
primaryStage.setScene(scene);
primaryStage.show();
}
In this snippet, we create a Label
with the initial text "Welcome to Java". Then, we create a StackPane
and add the label to it. We create a Scene
with the StackPane
as the root node, set the scene size to 300x200, set the title of the stage, and finally, show the stage. This sets up the basic display for our application. Now, let's move on to the fun part: handling the mouse clicks!
Handling Mouse Clicks
The core of our application is the mouse click handling. We want to alternate between the two messages each time the user clicks on the window. To do this, we'll add an event handler to our StackPane
. This handler will listen for mouse click events and update the text of the Label
accordingly.
Adding the Event Handler
Let's add the event handler to our StackPane
. We'll use the setOnMouseClicked
method to set up an event handler that will be called every time the mouse is clicked.
String message1 = "Welcome to Java";
String message2 = "LearningJavaFX";
boolean isMessage1 = true;
Label textLabel = new Label(message1);
StackPane root = new StackPane(textLabel);
root.setOnMouseClicked(event -> {
if (isMessage1) {
textLabel.setText(message2);
} else {
textLabel.setText(message1);
}
isMessage1 = !isMessage1;
});
Here’s what’s happening in this code:
- We define two
String
variables,message1
andmessage2
, to hold our messages. - We use a
boolean
variable,isMessage1
, to keep track of which message is currently displayed. - We create a
Label
and initialize it with the first message. - We set up the mouse click event handler using a lambda expression. Inside the handler:
- We check the value of
isMessage1
. If it’strue
, we set the text of the label tomessage2
. If it’sfalse
, we set it tomessage1
. - We then toggle the value of
isMessage1
usingisMessage1 = !isMessage1
.
- We check the value of
This ensures that each click alternates the text between the two messages. Now, let’s put it all together and see the complete code.
Putting It All Together
Here’s the complete code for our application:
package com.example.demo;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class AlternateTextApp extends Application {
@Override
public void start(Stage primaryStage) {
String message1 = "Welcome to Java";
String message2 = "LearningJavaFX";
boolean isMessage1 = true;
Label textLabel = new Label(message1);
StackPane root = new StackPane(textLabel);
root.setOnMouseClicked(event -> {
if (isMessage1) {
textLabel.setText(message2);
} else {
textLabel.setText(message1);
}
isMessage1 = !isMessage1;
});
Scene scene = new Scene(root, 300, 200);
primaryStage.setTitle("Alternate Text on Click");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Running the Application
To run the application, simply compile and run the AlternateTextApp
class. If you’re using an IDE, you can usually right-click on the class and select