├── src
├── main
│ ├── resources
│ │ ├── application.properties
│ │ ├── fxml
│ │ │ ├── Main.fxml
│ │ │ └── tabs
│ │ │ │ ├── TabPane.fxml
│ │ │ │ ├── LoggerTab.fxml
│ │ │ │ └── ConsoleTab.fxml
│ │ └── specs
│ │ │ ├── Skylab
│ │ │ ├── Shuttle
│ │ │ └── Apollo
│ └── java
│ │ └── com
│ │ └── mvp
│ │ └── java
│ │ ├── controllers
│ │ ├── LoggerTabController.java
│ │ ├── TabPaneManger.java
│ │ └── ConsoleTabController.java
│ │ ├── spring
│ │ └── config
│ │ │ └── AppJavaConfig.java
│ │ ├── services
│ │ └── MissionsService.java
│ │ └── Main.java
└── test
│ └── java
│ └── com
│ └── mvp
│ └── java
│ └── ConsoleTabTests.java
├── .gitignore
├── README.md
└── pom.xml
/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 | specs.dir=/specs/
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *.class
2 |
3 | # Mobile Tools for Java (J2ME)
4 | .mtj.tmp/
5 |
6 | # Package Files #
7 | *.jar
8 | *.war
9 | *.ear
10 |
11 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
12 | hs_err_pid*
13 |
--------------------------------------------------------------------------------
/src/main/java/com/mvp/java/controllers/LoggerTabController.java:
--------------------------------------------------------------------------------
1 | package com.mvp.java.controllers;
2 |
3 | import javafx.fxml.FXML;
4 | import javafx.scene.control.TextArea;
5 | import org.springframework.stereotype.Component;
6 |
7 | @Component
8 | public class LoggerTabController {
9 |
10 | @FXML private TextArea loggerTxtArea;
11 |
12 | public TextArea getLoggerTxtArea() {
13 | return loggerTxtArea;
14 | }
15 |
16 | }
17 |
--------------------------------------------------------------------------------
/src/main/resources/fxml/Main.fxml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/src/main/java/com/mvp/java/spring/config/AppJavaConfig.java:
--------------------------------------------------------------------------------
1 | package com.mvp.java.spring.config;
2 |
3 | import java.io.PrintWriter;
4 | import java.io.StringWriter;
5 | import org.springframework.context.annotation.Bean;
6 | import org.springframework.context.annotation.Configuration;
7 | import org.springframework.context.annotation.Scope;
8 |
9 | @Configuration
10 | public class AppJavaConfig {
11 |
12 | @Bean(name = "stringPrintWriter") @Scope("prototype")
13 | public PrintWriter printWriter(){
14 | // useful when dumping stack trace to file
15 | return new PrintWriter(new StringWriter());
16 | }
17 |
18 | }
19 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # springboot-javafx-tutorial
2 | Show you how to Integrate Spring Boot with JavaFX - Tutorial
3 |
4 | Specifically shows you how to integrate Spring with JavaFX 8 via Spring Boot.
5 | Describes how to use Spring as the main Java FX Controller Factory and
6 | how to use @Autowired into your Java FX Controllers to get access
7 | to your other Spring Services (@Service) and beans (either via @Component or
8 | JavaConfig @Configuration).
9 |
10 | The example also showcases how you can take advantage of Spring Test for
11 | you Integration tests.
12 |
13 | You can follow along via MVP Java's YouTube Video Tutorial
14 | "Integrating Spring Boot with JavaFX" https://youtu.be/hjeSOxi3uPg
15 |
--------------------------------------------------------------------------------
/src/main/java/com/mvp/java/controllers/TabPaneManger.java:
--------------------------------------------------------------------------------
1 | package com.mvp.java.controllers;
2 |
3 | import javafx.scene.control.TextArea;
4 | import org.springframework.beans.factory.annotation.Autowired;
5 | import org.springframework.stereotype.Component;
6 |
7 | @Component
8 | public class TabPaneManger {
9 |
10 | private final ConsoleTabController consoleTabController;
11 | private final LoggerTabController loggerTabController;
12 |
13 | @Autowired
14 | public TabPaneManger(ConsoleTabController consoleTabController, LoggerTabController loggerTabController) {
15 | this.consoleTabController = consoleTabController;
16 | this.loggerTabController = loggerTabController;
17 | }
18 |
19 | public TextArea getVisualLog() {
20 | return loggerTabController.getLoggerTxtArea();
21 | }
22 |
23 | }
24 |
--------------------------------------------------------------------------------
/src/test/java/com/mvp/java/ConsoleTabTests.java:
--------------------------------------------------------------------------------
1 | package com.mvp.java;
2 |
3 | import com.mvp.java.services.MissionsService;
4 | import java.io.IOException;
5 | import static org.junit.Assert.*;
6 | import org.junit.Test;
7 | import org.junit.runner.RunWith;
8 | import org.springframework.beans.factory.annotation.Autowired;
9 | import org.springframework.boot.test.SpringApplicationConfiguration;
10 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
11 |
12 | @RunWith(SpringJUnit4ClassRunner.class)
13 | @SpringApplicationConfiguration(classes = Main.class)
14 | public class ConsoleTabTests {
15 |
16 | @Autowired MissionsService missionsService;
17 |
18 | @Test
19 | public void retrieveMissionInfoSucessfully() throws IOException {
20 | String info = missionsService.getMissionInfo("Apollo");
21 | assertTrue(!info.isEmpty());
22 | }
23 |
24 | }
25 |
--------------------------------------------------------------------------------
/src/main/resources/specs/Skylab:
--------------------------------------------------------------------------------
1 | Skylab
2 | ======
3 |
4 | Skylab was a space station launched and operated by NASA and was the United
5 | States' first space station. Skylab orbited Earth from 1973 to 1979, and
6 | included a workshop, a solar observatory, and other systems. It was launched
7 | unmanned by a modified Saturn V rocket, with a weight of 170,000 pounds
8 | (77,111 kg).[1] Three manned expeditions to the station, conducted between
9 | May 1973 and February 1974 using the Apollo Command/Service Module (CSM) atop
10 | the smaller Saturn IB, each delivered a three-astronaut crew. On the last two
11 | manned missions, an additional Apollo / Saturn IB stood by ready to rescue the
12 | crew in orbit if it was needed.
13 |
14 | Specifications
15 |
16 | Crew Size: 3. Orbital Storage: 730 days. Habitable Volume: 361.00 m3. RCS
17 | Coarse No x Thrust: Reaction wheels. Electric System: 11.00 average kW.
18 |
19 | Gross mass: 76,295 kg (168,201 lb).
20 | Height: 36.12 m (118.50 ft).
21 | Span: 21.00 m (68.00 ft).
22 | First Launch: 1973.05.14.
23 | Number: 1 .
24 |
--------------------------------------------------------------------------------
/src/main/java/com/mvp/java/services/MissionsService.java:
--------------------------------------------------------------------------------
1 | package com.mvp.java.services;
2 |
3 | import java.io.BufferedReader;
4 | import java.io.IOException;
5 | import java.io.InputStream;
6 | import java.io.InputStreamReader;
7 | import org.springframework.beans.factory.annotation.Value;
8 | import org.springframework.stereotype.Service;
9 |
10 | @Service
11 | public class MissionsService {
12 |
13 | @Value(("${specs.dir}"))
14 | private String specsPath;
15 |
16 | public String getMissionInfo(String missionName) throws IOException {
17 | final StringBuilder fileContents = new StringBuilder(2000);
18 | final InputStream is = this.getClass().getResourceAsStream(specsPath + missionName);
19 |
20 | try (BufferedReader br = new BufferedReader(new InputStreamReader(is));) {
21 | String line;
22 | while ((line = br.readLine()) != null) {
23 | fileContents.append(line).append("\n");
24 | }
25 | }
26 | return fileContents.toString();
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/src/main/resources/fxml/tabs/TabPane.fxml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
--------------------------------------------------------------------------------
/src/main/resources/specs/Shuttle:
--------------------------------------------------------------------------------
1 | the Shuttle
2 | ===========
3 |
4 | Since 1981, NASA space shuttles have been rocketing from the Florida coast into
5 | Earth orbit. The five orbiters — Columbia, Challenger, Discovery, Atlantis and
6 | Endeavour — have flown more than 130 times, carrying over 350 people into space
7 | and travelling more than half a billion miles, more than enough to reach Jupiter.
8 |
9 |
10 | SPECIFICATIONS
11 |
12 | First launch: 12-Apr-1981
13 | Number launches: 114 to end-2005
14 | Launch sites: KSC pads 39A/39B
15 | Principal uses: US manned capability to beyond 2010 (four reuseable Orbiter fleet), 25,000 kg payload delivery to LEO, satellite retrieval/in situ repair, short-duration science platform, Space Station assembly/servicing
16 | Vehicle success rate: 98.25% to end-2005
17 | Performance:
18 | LEO (204 km, 28.45o): 21,140 kg OV-102, 24,950 kg OV-103/104/105; each additional
19 | 1 km altitude reduces capacity by 25 kg, each crewmember beyond 5-person size
20 | reduces capacity by 230 kg into basic orbit
21 | LEO (204 km, 57o): 14,800 kg OV-102, 18,600 kg OV-103/104/105 (57o is usually the
22 | highest inclination flown from Florida, but dog-legging provides access to higher angles)
23 | LEO (204 km, 98o Sun-synchronous VAFB): 13,426 kg OV-103/104/105, assuming use if
24 | Advanced Solid Rocket Motors. Neither VAFB nor ASRM will be used
25 | Molniya (925 x 39,450 km, 63o): 3,563 kg using IUS via 222 km, 57o parking orbit
26 | Crossrange: 2,040 km max; 1,020-1,300 km used operationally by NASA
--------------------------------------------------------------------------------
/src/main/resources/specs/Apollo:
--------------------------------------------------------------------------------
1 | Apollo (spacecraft)
2 | ================================================
3 | The Apollo spacecraft was composed of three parts designed to accomplish the American Apollo
4 | program's goal of landing astronauts on the Moon by the end of the 1960s and returning them
5 | safely to Earth. The expendable (single-use) spacecraft consisted of a combined
6 | Command/Service Module (CSM) and a Lunar Module (LM).
7 |
8 | Two additional components
9 | complemented the spacecraft stack for space vehicle assembly: a Spacecraft
10 | Lunar Module Adapter (SLA) designed to shield the LM from the aerodynamic
11 | stress of launch, and to connect the CSM to the Saturn launch vehicle;
12 | and a Launch Escape System (LES) to carry the crew in the Command Module safely away from
13 | the launch vehicle in the event of a launch emergency.
14 |
15 | The design was based on the Lunar Orbit Rendezvous approach: two docked spacecraft were sent
16 | to the Moon and went into lunar orbit. While the LM separated and landed, the CSM remained
17 | in orbit. After the lunar excursion, the two craft rendezvoused and docked in lunar orbit,
18 | and the CSM returned the crew to Earth. The Command Module was the only part of the space
19 | vehicle that returned with the crew to the Earth's surface.
20 |
21 |
22 | Specifications
23 |
24 | Length minus BPC: 32 ft 6 in (9.92 m)
25 | Length with BPC: 39 ft 5 in (12.02 m)
26 | Diameter: 2 ft 2 in (0.66 m)
27 | Total mass: 9,200 pounds (4,200 kg)
28 | Thrust, 36,000 ft: 147,000 pounds-force (650 kN)
29 | Thrust, maximum: 200,000 pounds-force (890 kN)
30 | Burn time: 4.0 seconds
--------------------------------------------------------------------------------
/src/main/resources/fxml/tabs/LoggerTab.fxml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
--------------------------------------------------------------------------------
/src/main/java/com/mvp/java/Main.java:
--------------------------------------------------------------------------------
1 | package com.mvp.java;
2 |
3 | import javafx.application.Application;
4 | import javafx.fxml.FXMLLoader;
5 | import javafx.scene.Parent;
6 | import javafx.scene.Scene;
7 | import javafx.stage.Stage;
8 | import org.springframework.boot.SpringApplication;
9 | import org.springframework.boot.autoconfigure.SpringBootApplication;
10 | import org.springframework.context.ConfigurableApplicationContext;
11 |
12 | @SpringBootApplication
13 | public class Main extends Application {
14 |
15 | private ConfigurableApplicationContext springContext;
16 | private Parent rootNode;
17 |
18 | public static void main(final String[] args) {
19 | Application.launch(args);
20 | }
21 |
22 | @Override
23 | public void init() throws Exception {
24 | springContext = SpringApplication.run(Main.class);
25 | FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/fxml/Main.fxml"));
26 | fxmlLoader.setControllerFactory(springContext::getBean);
27 | rootNode = fxmlLoader.load();
28 | }
29 |
30 | @Override
31 | public void start(Stage stage) throws Exception {
32 | stage.setScene(new Scene(rootNode));
33 | stage.show();
34 | }
35 |
36 | @Override
37 | public void stop() throws Exception {
38 | springContext.close();
39 | }
40 |
41 | }
42 |
43 |
44 |
45 |
46 |
47 | //Options ...
48 | //gets called for each Controller that you want DI. If you put a println
49 | //in before the return, you can see which Controller gets DI by spring
50 |
51 | //via good old anonymous class
52 | // loader.setControllerFactory(new Callback, Object>() {
53 | // @Override
54 | // public Object call(Class> clazz) {
55 | // return springContext.getBean(clazz);
56 | // }
57 | // });
58 |
59 | // via lambda
60 | // loader.setControllerFactory((clazz) -> springContext.getBean(clazz));
61 |
62 | // via method reference
63 | // loader.setControllerFactory(springContext::getBean);
64 |
--------------------------------------------------------------------------------
/src/main/java/com/mvp/java/controllers/ConsoleTabController.java:
--------------------------------------------------------------------------------
1 | package com.mvp.java.controllers;
2 |
3 | import com.mvp.java.services.MissionsService;
4 | import java.io.IOException;
5 | import java.io.PrintWriter;
6 | import javafx.collections.FXCollections;
7 | import javafx.collections.ObservableList;
8 | import javafx.fxml.FXML;
9 | import javafx.scene.control.ListView;
10 | import javafx.scene.control.TextArea;
11 | import javafx.scene.input.MouseEvent;
12 | import org.springframework.beans.factory.annotation.Autowired;
13 | import org.springframework.beans.factory.annotation.Qualifier;
14 | import org.springframework.stereotype.Component;
15 |
16 | @Component
17 | public class ConsoleTabController {
18 |
19 | @FXML private TextArea missionOverviewText;
20 | @FXML private ListView missionsList;
21 |
22 | @Autowired @Qualifier("stringPrintWriter")
23 | private PrintWriter stackTraceWriter;
24 |
25 | @Autowired MissionsService service;
26 | private TabPaneManger tabManager;
27 |
28 | public void initialize() {
29 | ObservableList missions = FXCollections.observableArrayList("Apollo", "Shuttle", "Skylab");
30 | missionsList.setItems(missions);
31 | }
32 |
33 | @FXML
34 | private void onMouseClicked(MouseEvent event) {
35 | missionOverviewText.clear();
36 | final String selectedItem = missionsList.getSelectionModel().getSelectedItem();
37 | missionOverviewText.positionCaret(0);
38 | missionOverviewText.appendText(getInfo(selectedItem));
39 | }
40 |
41 | @Autowired
42 | private void setTabManager(TabPaneManger tabManager){
43 | this.tabManager = tabManager;
44 | }
45 |
46 | public String getInfo(String selectedItem) {
47 | String missionInfo = null ;
48 |
49 | try {
50 | missionInfo = service.getMissionInfo(selectedItem);
51 | getLog().appendText("Sucessfully retrieved mission info for " + selectedItem + "\n");
52 | } catch (IOException exception) {
53 | exception.printStackTrace (stackTraceWriter);
54 | getLog().appendText(stackTraceWriter.toString() + "\n");
55 | }
56 |
57 | return missionInfo;
58 | }
59 |
60 | public TextArea getMissionOverviewText() {
61 | return missionOverviewText;
62 | }
63 |
64 | public ListView getMissionsList() {
65 | return missionsList;
66 | }
67 |
68 | private TextArea getLog(){
69 | return tabManager.getVisualLog();
70 | }
71 |
72 | }
73 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 | 4.0.0
5 |
6 | com.mvp.java
7 | springbootJavaFXTutorial
8 | 0.0.1-SNAPSHOT
9 | jar
10 |
11 | springbootJavaFXTutorial
12 | Mission Desciptions
13 |
14 |
15 | org.springframework.boot
16 | spring-boot-starter-parent
17 | 1.3.5.RELEASE
18 |
19 |
20 |
21 |
22 | UTF-8
23 | 1.8
24 |
25 |
26 |
27 |
28 | org.springframework.boot
29 | spring-boot-starter-security
30 |
31 |
32 |
33 | org.springframework.boot
34 | spring-boot-starter-test
35 | test
36 |
37 |
38 |
39 | org.testfx
40 | testfx-core
41 | 4.0.1-alpha
42 | test
43 |
44 |
45 |
46 | org.testfx
47 | testfx-junit
48 | 4.0.1-alpha
49 | test
50 |
51 |
52 |
53 |
54 |
55 |
56 | org.springframework.boot
57 | spring-boot-maven-plugin
58 |
59 |
60 | org.apache.maven.plugins
61 | maven-compiler-plugin
62 | 2.3.2
63 |
64 | true
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
--------------------------------------------------------------------------------
/src/main/resources/fxml/tabs/ConsoleTab.fxml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
--------------------------------------------------------------------------------