SpringFXExample ├──.idea ├──src │ ├──main │ │ ├──java │ │ └──resources │ └──test ├──────pom.xml └──────SpringFXExample.iml External Libraries
SpringFXExample ├──.idea ├──src │ ├──main │ │ ├──java │ │ │ └──org.name │ │ │ ├──app │ │ │ │ ├──controller │ │ │ │ │ ├──MainController.java │ │ │ │ │ └──ProductTableController.java │ │ │ │ └──Launcher.java │ │ │ └──model │ │ │ ├──dao │ │ │ │ └─ProductDao.java │ │ │ └──Product.java │ │ └──resources │ │ └──view │ │ ├──fxml │ │ │ ├──main.fxml │ │ │ └──productTable.fxml │ │ ├──style │ │ └──image │ └──test ├──────pom.xml └──────SpringFXExample.iml External Libraries
<?xml version="1.0" encoding="UTF-8"?> <?import javafx.scene.layout.AnchorPane?> <?import javafx.scene.control.Button?> <AnchorPane xmlns="http://javafx.com/javafx" xmlns:fx="http://javafx.com/fxml" fx:controller="org.name.app.controller.MainController" prefHeight="400.0" prefWidth="400.0"> <Button fx:id="load" text="" AnchorPane.topAnchor="10" AnchorPane.leftAnchor="10" onMouseClicked="#onClickLoad"/> <!-- TableView fxml --> <fx:include AnchorPane.topAnchor="40" AnchorPane.leftAnchor="10" AnchorPane.bottomAnchor="10" source="productTable.fxml"/> </AnchorPane>
<?xml version="1.0" encoding="UTF-8"?> <?import javafx.scene.control.TableColumn?> <?import javafx.scene.control.TableView?> <TableView fx:id="productTable" prefWidth="350.0" xmlns="http://javafx.com/javafx/8.0.121" xmlns:fx="http://javafx.com/fxml/1" fx:controller="org.name.app.controller.ProductTableController"> <columns> <TableColumn fx:id="id" prefWidth="30.0" text="ID"/> <TableColumn fx:id="name" prefWidth="200.0" text=""/> <TableColumn fx:id="quantity" prefWidth="50.0" text="-"/> <TableColumn fx:id="price" prefWidth="50.0" text=""/> </columns> </TableView>
package org.name.app.controller; import javafx.fxml.FXML; import javafx.scene.control.Button; public class MainController { @FXML private Button load; /** * */ @FXML public void onClickLoad() { System.out.println("..."); // TODO: DAO // TODO: } }
package org.name.app.controller; import javafx.collections.FXCollections; import javafx.fxml.FXML; import javafx.scene.control.TableColumn; import javafx.scene.control.TableView; import javafx.scene.control.cell.PropertyValueFactory; import org.name.model.Product; import java.util.List; public class ProductTableController { @FXML private TableColumn<Integer, Product> id; @FXML private TableColumn<String, Product> name; @FXML private TableColumn<Integer, Product> quantity; @FXML private TableColumn<String, Product> price; @FXML private TableView<Product> productTable; /** * value factory */ public void initialize() { id.setCellValueFactory(new PropertyValueFactory<>("id")); name.setCellValueFactory(new PropertyValueFactory<>("name")); quantity.setCellValueFactory(new PropertyValueFactory<>("quantity")); price.setCellValueFactory(new PropertyValueFactory<>("price")); } /** * * @param products */ public void fillTable(List<Product> products) { productTable.setItems(FXCollections.observableArrayList(products)); } }
package org.name.app; import javafx.application.Application; import javafx.fxml.FXMLLoader; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.stage.Stage; public class Launcher extends Application { public static void main(String[] args) { launch(args); } public void start(Stage stage) throws Exception { Parent root = FXMLLoader.load(getClass() .getResource("/view/fxml/main.fxml")); stage.setTitle("JavaFX Maven Spring"); stage.setScene(new Scene(root)); stage.show(); } }
package org.name.model; public class Product { private int id; private String name; private int quantity; private String price; private String guid; private int tax; public Product() { } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getQuantity() { return quantity; } public void setQuantity(int quantity) { this.quantity = quantity; } public String getPrice() { return price; } public void setPrice(String price) { this.price = price; } public String getGuid() { return guid; } public void setGuid(String guid) { this.guid = guid; } public int getTax() { return tax; } public void setTax(int tax) { this.tax = tax; } }
<properties> <maven.compiler.source>9</maven.compiler.source> <maven.compiler.target>9</maven.compiler.target> </properties>
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>3.0.2</version> <configuration> <archive> <manifest> <addClasspath>true</addClasspath> <classpathPrefix>lib/</classpathPrefix> <mainClass>org.name.app.Launcher</mainClass> </manifest> </archive> </configuration> </plugin> </plugins> </build>
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.name</groupId> <artifactId>SpringFXExample</artifactId> <version>1.0</version> <properties> <maven.compiler.source>9</maven.compiler.source> <maven.compiler.target>9</maven.compiler.target> </properties> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>3.0.2</version> <configuration> <archive> <manifest> <addClasspath>true</addClasspath> <classpathPrefix>lib/</classpathPrefix> <mainClass>org.name.app.Launcher</mainClass> </manifest> </archive> </configuration> </plugin> </plugins> </build> </project>
start java -jar target\SpringFXExample-1.0.jar
set JAVA_HOME=PATH_TO_JDK\bin set JAVA_CMD=%JAVA_HOME%\java start %JAVA_CMD% -jar target\SpringFXExample-1.0.jar
<properties> <maven.compiler.source>9</maven.compiler.source> <maven.compiler.target>9</maven.compiler.target> <spring.version>5.0.3.RELEASE</spring.version> </properties>
<dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.xerial</groupId> <artifactId>sqlite-jdbc</artifactId> <version>3.7.2</version> </dependency> </dependencies>
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.name</groupId> <artifactId>SpringFXExample</artifactId> <version>1.0</version> <properties> <maven.compiler.source>9</maven.compiler.source> <maven.compiler.target>9</maven.compiler.target> <spring.version>5.0.3.RELEASE</spring.version> </properties> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>3.0.2</version> <configuration> <archive> <manifest> <addClasspath>true</addClasspath> <classpathPrefix>lib/</classpathPrefix> <mainClass>org.name.app.Launcher</mainClass> </manifest> </archive> </configuration> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.xerial</groupId> <artifactId>sqlite-jdbc</artifactId> <version>3.7.2</version> </dependency> </dependencies> </project>
# Headline main scene
title = JavaFX & Spring Boot!
# DB Connection Configuration
db.url = jdbc: sqlite: PATH_TO_DB / test_db
db.user = user
db.password = password
db.driver = org.sqlite.JDBC
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:property-placeholder location="file:config.properties" ignore-unresolvable="true"/> <context:component-scan base-package="org.name"/> <bean name="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="url" value="${db.url}"/> <property name="driverClassName" value="${db.driver}"/> <property name="username" value="${db.user}"/> <property name="password" value="${db.password}"/> </bean> </beans>
package org.name.app.controller; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; public abstract class Controller implements ApplicationContextAware { private ApplicationContext context; public ApplicationContext getContext() { return context; } @Override public void setApplicationContext(ApplicationContext context) throws BeansException { this.context = context; } }
package org.name.app; import javafx.application.Platform; import javafx.fxml.FXMLLoader; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.stage.Stage; import org.springframework.beans.BeansException; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.stereotype.Component; import java.io.IOException; @Component public class SpringStageLoader implements ApplicationContextAware { private static ApplicationContext staticContext; // @Value("${title}") private String appTitle; private static String staticTitle; private static final String FXML_DIR = "/view/fxml/"; private static final String MAIN_STAGE = "main"; /** * fxml * @param fxmlName *.fxml * @return Parent * @throws IOException - */ private static Parent load(String fxmlName) throws IOException { FXMLLoader loader = new FXMLLoader(); // setLocation , productTable.fxml, // javafx.fxml.LoadException: Base location is undefined. loader.setLocation(SpringStageLoader.class.getResource(FXML_DIR + fxmlName + ".fxml")); // setLocation loader loader.setClassLoader(SpringStageLoader.class.getClassLoader()); loader.setControllerFactory(staticContext::getBean); return loader.load(SpringStageLoader.class.getResourceAsStream(FXML_DIR + fxmlName + ".fxml")); } /** * . , * @return * @throws IOException - */ public static Stage loadMain() throws IOException { Stage stage = new Stage(); stage.setScene(new Scene(load(MAIN_STAGE))); stage.setOnHidden(event -> Platform.exit()); stage.setTitle(staticTitle); return stage; } /** * ApplicationContextAware, .. */ @Override public void setApplicationContext(ApplicationContext context) throws BeansException { SpringStageLoader.staticContext = context; SpringStageLoader.staticTitle = appTitle; } }
package org.name.app; import javafx.application.Application; import javafx.stage.Stage; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.io.IOException; public class Launcher extends Application { private static ClassPathXmlApplicationContext context; public static void main(String[] args) { launch(args); } /** * */ @Override public void init() { context = new ClassPathXmlApplicationContext("application-context.xml"); } @Override public void start(Stage stage) throws IOException { SpringStageLoader.loadMain().show(); } /** * */ @Override public void stop() throws IOException { context.close(); } }
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.name.app.controller.MainController' available
package org.name.model.dao; import org.name.model.Product; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.BeanPropertyRowMapper; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Component; import javax.sql.DataSource; import java.util.List; @Component public class ProductDao { private JdbcTemplate template; /** * dataSource JdbcTemplate */ @Autowired public ProductDao(DataSource dataSource) { this.template = new JdbcTemplate(dataSource); } /** * . .. Product JavaBean * BeanPropertyRowMapper. */ public List<Product> getAllProducts(){ String sql = "SELECT * FROM product"; return template.query(sql, new BeanPropertyRowMapper<>(Product.class)); } }
package org.name.app.controller; import javafx.fxml.FXML; import javafx.scene.control.Button; import org.name.model.dao.ProductDao; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component public class MainController extends Controller { @FXML private Button load; private ProductTableController tableController; private ProductDao productDao; @Autowired public MainController(ProductTableController tableController, ProductDao productDao) { this.tableController = tableController; this.productDao = productDao; } /** * */ @FXML public void onClickLoad() { tableController.fillTable(productDao.getAllProducts()); load.setDisable(true); } }
<?xml version="1.0" encoding="UTF-8"?> <?import javafx.scene.control.*?> <?import javafx.scene.layout.*?> <AnchorPane xmlns="http://javafx.com/javafx/8.0.121" xmlns:fx="http://javafx.com/fxml/1"> <children> <GridPane> <columnConstraints> <ColumnConstraints prefWidth="150.0"/> <ColumnConstraints prefWidth="300.0"/> </columnConstraints> <rowConstraints> <RowConstraints prefHeight="30.0"/> <RowConstraints prefHeight="30.0"/> <RowConstraints prefHeight="30.0"/> <RowConstraints prefHeight="30.0"/> <RowConstraints prefHeight="30.0"/> <RowConstraints prefHeight="30.0"/> </rowConstraints> <Label fx:id="name" style="-fx-font-weight: bold;-fx-padding: 3px;" prefWidth="450" GridPane.columnSpan="2" alignment="CENTER"/> <Label style="-fx-font-weight: bold; -fx-padding: 3px;" GridPane.rowIndex="1" text=":"/> <Label fx:id="guid" style="-fx-padding: 3px;" GridPane.rowIndex="1" GridPane.columnIndex="1"/> <Label style="-fx-font-weight: bold; -fx-padding: 3px;" GridPane.rowIndex="2" text=" :"/> <Label fx:id="quantity" style="-fx-padding: 3px;" GridPane.rowIndex="2" GridPane.columnIndex="1"/> <Label style="-fx-font-weight: bold; -fx-padding: 3px;" GridPane.rowIndex="3" text=":"/> <Label fx:id="price" style="-fx-padding: 3px;" GridPane.rowIndex="3" GridPane.columnIndex="1"/> <Label style="-fx-font-weight: bold; -fx-padding: 3px;" GridPane.rowIndex="4" text=" :"/> <Label fx:id="costOfAll" style="-fx-padding: 3px;" GridPane.rowIndex="4" GridPane.columnIndex="1"/> <Label style="-fx-font-weight: bold; -fx-padding: 3px;" GridPane.rowIndex="5" text=":"/> <Label fx:id="tax" style="-fx-padding: 3px;" GridPane.rowIndex="5" GridPane.columnIndex="1"/> </GridPane> </children> </AnchorPane>
package org.name.app.controller; import javafx.scene.Scene; import javafx.scene.control.Label; import javafx.stage.Modality; import javafx.stage.Stage; import org.name.app.SpringStageLoader; import org.name.model.Product; import java.io.IOException; public class ProductDetailsModalStage extends Stage { private Label name; private Label guid; private Label quantity; private Label price; private Label costOfAll; private Label tax; public ProductDetailsModalStage() { this.initModality(Modality.WINDOW_MODAL); this.centerOnScreen(); try { Scene scene = SpringStageLoader.loadScene("productDetails"); this.setScene(scene); name = (Label) scene.lookup("#name"); guid = (Label) scene.lookup("#guid"); quantity = (Label) scene.lookup("#quantity"); price = (Label) scene.lookup("#price"); costOfAll = (Label) scene.lookup("#costOfAll"); tax = (Label) scene.lookup("#tax"); } catch (IOException e) { e.printStackTrace(); } } public void showDetails(Product product) { name.setText(product.getName()); guid.setText(product.getGuid()); quantity.setText(String.valueOf(product.getQuantity())); price.setText(product.getPrice()); costOfAll.setText("$" + getCostOfAll(product)); tax.setText(String.valueOf(product.getTax()) + " %"); setTitle(" : " + product.getName()); show(); } private String getCostOfAll(Product product) { int quantity = product.getQuantity(); double priceOfOne = Double.parseDouble(product .getPrice() .replace("$", "")); return String.valueOf(quantity * priceOfOne); } }
public static Scene loadScene(String fxmlName) throws IOException { return new Scene(load(fxmlName)); }
productTable.setRowFactory(rf -> { TableRow<Product> row = new TableRow<>(); row.setOnMouseClicked(event -> { if (event.getClickCount() == 2 && (!row.isEmpty())) { ProductDetailsModalStage stage = new ProductDetailsModalStage(); stage.showDetails(row.getItem()); } }); return row; });
<?xml version="1.0" encoding="UTF-8"?> <?import javafx.scene.image.Image?> <?import javafx.scene.image.ImageView?> <?import javafx.scene.layout.AnchorPane?> <AnchorPane xmlns="http://javafx.com/javafx" mouseTransparent="true"> <ImageView> <Image url="@/view/image/splash.png"/> </ImageView> </AnchorPane>
package org.name.app; import javafx.application.Application; import javafx.application.Platform; import javafx.fxml.FXMLLoader; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.scene.paint.Color; import javafx.stage.Stage; import javafx.stage.StageStyle; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.io.IOException; public class Launcher extends Application { private static ClassPathXmlApplicationContext context; private Stage splashScreen; public static void main(String[] args) { launch(args); } /** * UI . init() UI Platform.runLater() * @throws Exception */ @Override public void init() throws Exception { Platform.runLater(this::showSplash); Thread.sleep(10000); context = new ClassPathXmlApplicationContext("application-context.xml"); Platform.runLater(this::closeSplash); } @Override public void start(Stage stage) throws IOException { SpringStageLoader.loadMain().show(); } /** * */ @Override public void stop() { context.close(); } /** * . */ private void showSplash() { try { splashScreen = new Stage(StageStyle.TRANSPARENT); splashScreen.setTitle("Splash"); Parent root = FXMLLoader.load(getClass().getResource("/view/fxml/splash.fxml")); Scene scene = new Scene(root, Color.TRANSPARENT); splashScreen.setScene(scene); splashScreen.show(); } catch (IOException e) { e.printStackTrace(); } } /** * */ private void closeSplash() { splashScreen.close(); } }
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.name</groupId> <artifactId>SpringFXExample</artifactId> <version>1.0</version> <properties> <maven.compiler.source>9</maven.compiler.source> <maven.compiler.target>9</maven.compiler.target> <spring.version>5.0.3.RELEASE</spring.version> </properties> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>3.0.2</version> <configuration> <archive> <manifest> <addClasspath>true</addClasspath> <classpathPrefix>lib/</classpathPrefix> <mainClass>org.name.app.Launcher</mainClass> </manifest> </archive> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>3.1.0</version> <executions> <execution> <goals> <goal>shade</goal> </goals> <configuration> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"> <resource>META-INF/spring.handlers</resource> </transformer> <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"> <resource>META-INF/spring.schemas</resource> </transformer> </transformers> </configuration> </execution> </executions> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.xerial</groupId> <artifactId>sqlite-jdbc</artifactId> <version>3.7.2</version> </dependency> </dependencies> </project>
SpringFXExample ├──.idea ├──src │ ├──main │ │ ├──java │ │ │ └──org.name │ │ │ ├──app │ │ │ │ ├──controller │ │ │ │ │ ├──Controller.java │ │ │ │ │ ├──MainController.java │ │ │ │ │ ├──ProductTableController.java │ │ │ │ │ └──ProductDetailsModalStage.java │ │ │ │ ├──Launcher.java │ │ │ │ └──SpringStageLoader.java │ │ │ └──model │ │ │ ├──dao │ │ │ │ └─ProductDao.java │ │ │ └──Product.java │ │ └──resources │ │ ├──view │ │ │ ├──fxml │ │ │ │ ├──main.fxml │ │ │ │ ├──productDetails.fxml │ │ │ │ ├──productTable.fxml │ │ │ │ └──splash.fxml │ │ │ ├──style │ │ │ └──image │ │ │ └──splash.png │ │ └──application-context.xml │ └──test ├──────config.properties.xml ├──────pom.xml ├──────SpringFXExample.iml └──────test-db.xml External Libraries
Source: https://habr.com/ru/post/348850/
All Articles