Skip to content

Commit 26c4176

Browse files
authored
[BAEL-5774] Update to the class names (#18885)
* [BAEL-6602] Copying text to clipboard in Java * [BAEL-5774] Constructor vs. initialize() in JavaFX * [BAEL-5774] fix: classes and contructor names * [BAEL-5774] Updated class names in accordance to the article * [BAEL-5774] Proper arguments for MetricsCollector and User constructors * [BAEL-5774] userService properly initialized * [BAEL-5774] Moved the snippets to a standalone javafx-2 module
1 parent 87449d3 commit 26c4176

File tree

12 files changed

+342
-24
lines changed

12 files changed

+342
-24
lines changed

javafx-2/pom.xml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<parent>
7+
<groupId>com.baeldung</groupId>
8+
<artifactId>parent-modules</artifactId>
9+
<version>1.0.0-SNAPSHOT</version>
10+
</parent>
11+
12+
<artifactId>javafx-2</artifactId>
13+
<packaging>jar</packaging>
14+
<name>javafx-2</name>
15+
16+
<dependencies>
17+
<dependency>
18+
<groupId>org.openjfx</groupId>
19+
<artifactId>javafx-controls</artifactId>
20+
<version>${javafx.version}</version>
21+
</dependency>
22+
<dependency>
23+
<groupId>org.openjfx</groupId>
24+
<artifactId>javafx-fxml</artifactId>
25+
<version>${javafx.version}</version>
26+
</dependency>
27+
</dependencies>
28+
29+
<build>
30+
<plugins>
31+
<plugin>
32+
<groupId>org.openjfx</groupId>
33+
<artifactId>javafx-maven-plugin</artifactId>
34+
<version>${javafx-maven-plugin.version}</version>
35+
<configuration>
36+
<mainClass>com.baeldung.javafx2.Main</mainClass>
37+
</configuration>
38+
</plugin>
39+
</plugins>
40+
</build>
41+
42+
<properties>
43+
<javafx.version>19</javafx.version>
44+
<javafx-maven-plugin.version>0.0.8</javafx-maven-plugin.version>
45+
</properties>
46+
</project>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.baeldung;
2+
3+
import javafx.application.Application;
4+
import javafx.fxml.FXMLLoader;
5+
import javafx.scene.Parent;
6+
import javafx.scene.Scene;
7+
import javafx.scene.control.Label;
8+
import javafx.scene.layout.AnchorPane;
9+
import javafx.stage.Stage;
10+
import java.io.IOException;
11+
12+
public class Main extends Application {
13+
14+
public static void main(String[] args) {
15+
launch(args);
16+
}
17+
18+
@Override
19+
public void start(Stage primaryStage) throws Exception {
20+
try {
21+
FXMLLoader loader = new FXMLLoader(getClass().getResource("app-label.fxml"));
22+
Parent root = loader.load();
23+
primaryStage.setScene(new Scene(root));
24+
} catch (IOException e) {
25+
System.err.println("View failed to load: " + e.getMessage());
26+
primaryStage.setScene(new Scene(new Label("UI failed to load")));
27+
}
28+
29+
primaryStage.setTitle("Title goes here");
30+
primaryStage.show();
31+
}
32+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.baeldung.controller;
2+
3+
import javafx.fxml.FXML;
4+
import javafx.scene.control.Label;
5+
6+
public class ControllerAnnotation {
7+
private final String appName;
8+
9+
@FXML
10+
private Label appNameLabel;
11+
12+
public ControllerAnnotation(String name) {
13+
this.appName = name;
14+
}
15+
16+
@FXML
17+
public void initialize() {
18+
this.appNameLabel.setText(this.appName);
19+
}
20+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.baeldung.controller;
2+
3+
import java.net.URL;
4+
import java.util.ResourceBundle;
5+
6+
import javafx.fxml.FXML;
7+
import javafx.fxml.Initializable;
8+
import javafx.scene.control.Label;
9+
10+
public class MainController implements Initializable {
11+
12+
private final Logger logger;
13+
private final MetricsCollector metrics;
14+
private final String appName;
15+
16+
@FXML
17+
private Label statusLabel;
18+
19+
@FXML
20+
private Label appNameLabel;
21+
22+
public MainController(String name) {
23+
this.logger = Logger.getLogger(MainController.class.getName());
24+
this.metrics = new MetricsCollector("dashboard-controller");
25+
this.appName = name;
26+
27+
logger.info("DashboardController created");
28+
metrics.incrementCounter("controller.instances");
29+
}
30+
31+
@Override
32+
public void initialize(URL location, ResourceBundle resources) {
33+
this.appNameLabel.setText(this.appName);
34+
this.statusLabel.setText("App is ready!");
35+
logger.info("UI initialized successfully");
36+
}
37+
38+
// Placeholder classes for demo
39+
static class Logger {
40+
private final String name;
41+
private Logger(String name) { this.name = name; }
42+
public static Logger getLogger(String name) { return new Logger(name); }
43+
public void info(String msg) { System.out.println("[INFO] " + msg); }
44+
}
45+
46+
static class MetricsCollector {
47+
private final String source;
48+
public MetricsCollector(String source) { this.source = source; }
49+
public void incrementCounter(String key) {
50+
System.out.println("Metric incremented: " + key + " (source: " + source + ")");
51+
}
52+
}
53+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.baeldung.controller;
2+
3+
import java.net.URL;
4+
import java.util.ResourceBundle;
5+
6+
import javafx.fxml.FXML;
7+
import javafx.fxml.Initializable;
8+
import javafx.scene.control.Label;
9+
10+
public class ProfileController implements Initializable {
11+
12+
private final UserService userService;
13+
private User currentUser;
14+
15+
@FXML
16+
private Label usernameLabel;
17+
18+
public ProfileController(UserService userService) {
19+
this.userService = userService;
20+
this.currentUser = userService.getCurrentUser();
21+
}
22+
23+
@Override
24+
public void initialize(URL location, ResourceBundle resources) {
25+
usernameLabel.setText("Welcome, " + this.currentUser.getName());
26+
}
27+
28+
// Placeholder classes for demo
29+
static class UserService {
30+
private final User user;
31+
32+
UserService() {
33+
this.user = new User("Baeldung");
34+
}
35+
36+
public User getCurrentUser() {
37+
return this.user;
38+
}
39+
}
40+
41+
static class User {
42+
private String name;
43+
44+
public User(String name) {
45+
this.name = name;
46+
}
47+
48+
public String getName() {
49+
return name;
50+
}
51+
52+
public void setName(String name) {
53+
this.name = name;
54+
}
55+
}
56+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<?import javafx.scene.control.Label?>
4+
<?import javafx.scene.layout.StackPane?>
5+
6+
<StackPane xmlns:fx="http://javafx.com/fxml" fx:controller="your.package.YourController">
7+
<Label fx:id="appNameLabel" text="Example App" />
8+
</StackPane>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<?import javafx.scene.control.Label?>
4+
<?import javafx.scene.layout.StackPane?>
5+
6+
<StackPane xmlns:fx="http://javafx.com/fxml" fx:controller="your.package.YourController">
7+
<Label fx:id="statusLabel" text="Loading" />
8+
</StackPane>

javafx/src/main/java/com/baeldung/controller/ControllerInitializable.java

Lines changed: 0 additions & 24 deletions
This file was deleted.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.baeldung.controller;
2+
3+
import java.net.URL;
4+
import java.util.ResourceBundle;
5+
6+
import javafx.fxml.FXML;
7+
import javafx.fxml.Initializable;
8+
import javafx.scene.control.Label;
9+
10+
public class MainController implements Initializable {
11+
12+
private final Logger logger;
13+
private final MetricsCollector metrics;
14+
private final String appName;
15+
16+
@FXML
17+
private Label statusLabel;
18+
19+
@FXML
20+
private Label appNameLabel;
21+
22+
public MainController(String name) {
23+
this.logger = Logger.getLogger(MainController.class.getName());
24+
this.metrics = new MetricsCollector("dashboard-controller");
25+
this.appName = name;
26+
27+
logger.info("DashboardController created");
28+
metrics.incrementCounter("controller.instances");
29+
}
30+
31+
@Override
32+
public void initialize(URL location, ResourceBundle resources) {
33+
this.appNameLabel.setText(this.appName);
34+
this.statusLabel.setText("App is ready!");
35+
logger.info("UI initialized successfully");
36+
}
37+
38+
// Placeholder classes for demo
39+
static class Logger {
40+
private final String name;
41+
private Logger(String name) { this.name = name; }
42+
public static Logger getLogger(String name) { return new Logger(name); }
43+
public void info(String msg) { System.out.println("[INFO] " + msg); }
44+
}
45+
46+
static class MetricsCollector {
47+
private final String source;
48+
public MetricsCollector(String source) { this.source = source; }
49+
public void incrementCounter(String key) {
50+
System.out.println("Metric incremented: " + key + " (source: " + source + ")");
51+
}
52+
}
53+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.baeldung.controller;
2+
3+
import java.net.URL;
4+
import java.util.ResourceBundle;
5+
6+
import javafx.fxml.FXML;
7+
import javafx.fxml.Initializable;
8+
import javafx.scene.control.Label;
9+
10+
public class ProfileController implements Initializable {
11+
12+
private final UserService userService;
13+
private User currentUser;
14+
15+
@FXML
16+
private Label usernameLabel;
17+
18+
public ProfileController(UserService userService) {
19+
this.userService = userService;
20+
this.currentUser = userService.getCurrentUser();
21+
}
22+
23+
@Override
24+
public void initialize(URL location, ResourceBundle resources) {
25+
usernameLabel.setText("Welcome, " + this.currentUser.getName());
26+
}
27+
28+
// Placeholder classes for demo
29+
static class UserService {
30+
private final User user;
31+
32+
UserService() {
33+
this.user = new User("Baeldung");
34+
}
35+
36+
public User getCurrentUser() {
37+
return this.user;
38+
}
39+
}
40+
41+
static class User {
42+
private String name;
43+
44+
public User(String name) {
45+
this.name = name;
46+
}
47+
48+
public String getName() {
49+
return name;
50+
}
51+
52+
public void setName(String name) {
53+
this.name = name;
54+
}
55+
}
56+
}

0 commit comments

Comments
 (0)