è¯ãæéãããããã¹ã¯ïŒ
ç§ã®ãããªåãäœãã®ãšåãæ人ãããªãã®äžã«ããããšãé¡ã£ãŠããŸãã
äºå®ãç§ã¯ãã€ããã¬ã³ããªãŒãªã€ã³ã¿ãŒãã§ãŒã¹ã«åãçµãã§ããŸããã ç§ã¯ããŸããŠãŒã¶ãŒæåã§ã¯ãªãã¢ããªã±ãŒã·ã§ã³ã«ã€ã©ã€ã©ããŸãããããã¯ç¹ã«äŒæ¥éçºã«åœãŠã¯ãŸããŸãã ãŸããJavaã§èšè¿°ãããã¯ã©ã€ã¢ã³ãã¢ããªã±ãŒã·ã§ã³ã¯é»ããŠã£ã³ããŠã§ããããšãå€ããGUIã¢ããªã±ãŒã·ã§ã³ã«ã¯æççã§ãã
以åã¯ãSwingãŸãã¯AWTã§ã¯ããã¹ãŠãéåžžã«æ²ããã£ãããããããJavaFX 8ããåã§ãããå¿åã¯ã©ã¹ãæžãããšã¯ã¹ãã²ããã£ã³ãŒãã«å€ãã£ãŠããã ããããã©ã ãåŒã®åºçŸã«ããããã¹ãŠãå€æŽãããã³ãŒãã¯ããã·ã³ãã«ã§æ確ã«ãªããããçŸãããªããŸããã ãããžã§ã¯ãã§JavaFXã䜿çšããããšã¯åã³ã§ããã
ããã§ãç§ã¯ãJava Spring Frameworkã«æé©ãªããŒã«ãšãJavaFX GUIãäœæãã䟿å©ãªããŒã«ãæ¥ç¶ãããšããã¢ã€ãã¢ãæãã€ããŸãããããã«ãããã¯ã©ã€ã¢ã³ããã¹ã¯ãããã¢ããªã±ãŒã·ã§ã³ã§ãã¹ãŠã®Springæ©èœã䜿çšã§ããããã«ãªããŸãã ãããã¯ãŒã¯ã®ãªãŒãã³ã¹ããŒã¹ã§æ¢ããŠãããã¹ãŠã®æ
å ±voeidnoãåéããã®ã§ããããå
±æããããšã«ããŸããã ãŸã第äžã«ããã®èšäºã¯åå¿è
ã察象ãšããŠãããããå€ãã®è©³çŽ°ã®äžéšã¯ããŸãã«ãå¹³å¡ã§åçŽãªå ŽåããããŸãããèšäºã®æŽåæ§ã倱ããªãããã«ããããçç¥ããããããŸããã

ç§ã®æ±ºå®ã«å¯Ÿãã建èšçãªæ¹å€ã楜ãã¿ã«ããŠããŸãã
ç«ã®äžã§èª°ãæ°ã«ããŠãã ããã
å°ããªã¢ããªã±ãŒã·ã§ã³ãäœæããŠã¿ãŸãããã ãã®ãããªåå§çãªã¿ã¹ã¯ããããšä»®å®ããŸãïŒè£œåããŒã¿ãããŒã¿ããŒã¹ãããã©ãŒã ã®ããŒãã«ã«ããŒãããã¢ããªã±ãŒã·ã§ã³ãäœæããå¿
èŠããããããŒãã«ã®åè¡ãã¯ãªãã¯ãããšã補åã«é¢ãããã詳现ãªããŒã¿ãå«ãè¿œå ã®ãŠã£ã³ããŠãéããŸãã ãã®ãµãŒãã¹ã䜿çšããŠãããŒã¿ããŒã¹ã«ããŒã¿ãå
¥åããŸãã 補åãå«ãããŒãã«ã®åœããŒã¿ãçæããããŒã¿ããŒã¹ã«ããããæ£åžžã«è¿œå ããŸããã
次ã®ããšãããããŸãã
ã¡ã€ã³ãã©ãŒã ã¯ã次ã®ã³ã³ããŒãã³ãã§æ§æãããŠããŸãã
1.ãããŠã³ããŒãããšããããã¹ãã®ãã¿ã³
2.ãIDãããååãããæ°éãããäŸ¡æ Œããã£ãŒã«ãã®ããTableView
æ©èœç
- ã¢ããªã±ãŒã·ã§ã³ãèµ·åãããšãã³ã³ããã¹ãã§Bean DataSourceãäœæãããããŒã¿ããŒã¹ãæ¥ç¶ãããŸãã æ¥ç¶ããŒã¿ã¯æ§æãã¡ã€ã«ã«ãããŸãã ProductsããŒãã«ãã4ã€ã®ãã£ãŒã«ããå°åºããå¿
èŠããããŸãã
- [ããŠã³ããŒã]ãã¿ã³ãã¯ãªãã¯ãããšãTableViewã«ããŒãã«ã®ããŒã¿ãå
¥åãããŸãã
- ããŒãã«è¡ãããã«ã¯ãªãã¯ãããšãè¿œå ã®ãŠã£ã³ããŠãéãããã¹ãŠã®è£œåãã£ãŒã«ãã衚瀺ãããŸãã
䜿çšã¹ã¿ãã¯ïŒ
JavaFX 8
æ¥ã®JDBC
SQLite 3
IntelliJ IDEA Community Edition 2017JavaFXãããžã§ã¯ãã®äœæ
Mavenã¢ãŒãã¿ã€ãã䜿çšããŠãIDEAã§æ°ãããããžã§ã¯ããäœæããŸãã ç§ãã¡ãèŠãæåã®æ§é ã¯ãMavenãããžã§ã¯ãã®éåžžã«æšæºçãªãã®ã§ãã
SpringFXExample âââ.idea âââsrc â âââmain â â âââjava â â âââresources â âââtest âââââââpom.xml âââââââSpringFXExample.iml External Libraries
ã¢ãžã¥ãŒã«ãšãããžã§ã¯ãã«å¿
èŠãªèšèªã¬ãã«ãèšå®ãããã«ããå®è¡ãå±é->ã³ã³ãã€ã©-> Javaã³ã³ãã€ã©ã®èšå®ã§ã¢ãžã¥ãŒã«ã®ã¿ãŒã²ãããã€ãã³ãŒãããŒãžã§ã³ãå€æŽããŸãã JDKã®ããŒãžã§ã³ã«å¿ããŠã
ããã§ãçºçããããšãJavaFXã¢ããªã±ãŒã·ã§ã³ã«å€æããå¿
èŠããããŸãã ç§ãååŸããããããžã§ã¯ãã®æ§é ã以äžã«ç€ºããŸãããçæ³çã§ãããšäž»åŒµãããã®ã§ã¯ãããŸããã
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
javaãã£ã¬ã¯ããªã«org.nameããã±ãŒãžãäœæããŸãïŒãŸãã¯åã«groupIdãšåãå€ã䜿çšããŸãïŒã ã€ã³ã¿ãŒãã§ã€ã¹ã®ã¢ããªã±ãŒã·ã§ã³ãã³ã³ãããŒã©ãŒãã«ã¹ã¿ã èŠçŽ ããŠãŒãã£ãªãã£ã®ãšã³ããªãã€ã³ãã¯ãã¢ããªããã±ãŒãžã«é
眮ãããŸãã ä»ã®ãã¹ãŠã¯ãã¢ãã«ããã±ãŒãžã®ã¢ããªã±ãŒã·ã§ã³ã§äœ¿çšããããšã³ãã£ãã£ã«çŽæ¥é¢ä¿ããŸãã ãªãœãŒã¹ã§ã¯ããã¥ãŒãã£ã¬ã¯ããªãäœæãã* .fxmlãfxmlãã©ã«ããŒã«ã* .cssãã¹ã¿ã€ã«ãã©ã«ããŒã«ãç»åãimageãã©ã«ããŒã«ä¿åããŸãã
ã¡ã€ã³ã® FXMLãã³ãã¬ãŒãã§ãã¢ããªã±ãŒã·ã§ã³ã®å€èŠ³ãã³ãã¬ãŒããèšå®ããŸãã ããŒãã«ã®å€èŠ³ã
èšå®ãã
productTableãã³ãã¬ãŒããå«ãŸããŸãã
MainControllerã¯ã¡ã€ã³ã³ã³ãããŒã©ãŒã§ãããããŠã³ããŒããã¿ã³ã®ã¯ãªãã¯ãåŠçãã1ã€ã®æ¹æ³ã䜿çšããŸãã
ProductTableControllerããŒãã«ã³ã³ãããŒã©ãŒã
ã¢ããªã±ãŒã·ã§ã³ãã
Launcherã
æ¡åŒµããstartã¡ãœããã§éåžžã®æ¹æ³ã§main.fxmlãããŒãããŸãã
ProductDaoã¯ã©ã¹ã¯åŸã§äœ¿çšããŸãã ãã ãã
Productã¯JavaBeanã®æŠå¿µã«ã€ããŠèšè¿°ããŸãã
ãã¡ã€ã«ã®å
容ã«ç§»åããŸãã
main.fxml <?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"/> <fx:include AnchorPane.topAnchor="40" AnchorPane.leftAnchor="10" AnchorPane.bottomAnchor="10" source="productTable.fxml"/> </AnchorPane>
productTable.fxml <?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>
MainController.java 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("...");
ProductTableController.java 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; public void initialize() { id.setCellValueFactory(new PropertyValueFactory<>("id")); name.setCellValueFactory(new PropertyValueFactory<>("name")); quantity.setCellValueFactory(new PropertyValueFactory<>("quantity")); price.setCellValueFactory(new PropertyValueFactory<>("price")); } public void fillTable(List<Product> products) { productTable.setItems(FXCollections.observableArrayList(products)); } }
Launcher.java 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(); } }
Product.java 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; } }
ãã¹ãŠãæ©èœããããšã確èªãå§ããŸãã

æåã®ãã«ã
mavenããã±ãŒãžã䜿çšããŠJARãæ§ç¯ããããšããŠã
ãŸã ã
pom.xmlã«æ¬¡ã®æ§æãè¿œå ããŸãïŒãããžã§ã¯ãã«Java 9ããããŸãããããã¯ãã¹ãŠã®æ©èœã䜿çšãããšããæå³ã§ã¯ãªããæ°ãããããžã§ã¯ãã«ææ°ã®ããŒã«ãéžæããã ãã§ãïŒã
<properties> <maven.compiler.source>9</maven.compiler.source> <maven.compiler.target>9</maven.compiler.target> </properties>
ããã³maven-jar-pluginïŒ
<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>
pom.xml <?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>
ç°å¢å€æ°ãé©åã«æ§æãããŠããå Žåãçµæã®jarããã¯ããŒã ãå®è¡ããããšããŸãã
start java -jar target\SpringFXExample-1.0.jar
ãŸãã¯ã次ã®ã³ã³ãã³ãã§run.batã䜿çšããŸãã
set JAVA_HOME=PATH_TO_JDK\bin set JAVA_CMD=%JAVA_HOME%\java start %JAVA_CMD% -jar target\SpringFXExample-1.0.jar
å人çã«ã¯ãPCã§ç°ãªãJDKã䜿çšããŠããããããã®æ¹æ³ã§ã¢ããªã±ãŒã·ã§ã³ãèµ·åããŸãã

ãšããã§ã端æ«ãé衚瀺ã«ããããã«ããã¿ã³ãã¯ãªãã¯ããããšãã«ããã¹ãåºåããã§ãã¯ããå¿
èŠãããçŸåšã®ã±ãŒã¹ã®ããã«ã
javaã§ã¯ãªã
javawãåŒã³åºããŸãã
æ¥ãè¿œå
ããã§ãSpringã®æéã§ããã€ãŸãã
ãªãœãŒã¹ã« application-context.xmlãäœæããå°ãå€æŽããã·ãŒã³ããŒããŒãäœæã
ãŸã ã ããã«ãSpring loader for JavaFXã®ã¢ã€ãã¢ã¯ç§ã®ãã®ã§ã¯ãªãããšã«æ³šæããŠãã ãããããã¯ãããã¯ãŒã¯ã®ãªãŒãã³ã¹ããŒã¹ã§ãã§ã«èŠãŠããŸãã ããããç§ã¯ãããå°ãåèããŸããã
ãŸãæåã«pom.xmlãç·šéããŸãã æ¥ããŒãžã§ã³ãè¿œå
<properties> <maven.compiler.source>9</maven.compiler.source> <maven.compiler.target>9</maven.compiler.target> <spring.version>5.0.3.RELEASE</spring.version> </properties>
ããã³äŸåé¢ä¿spring-contextãspring-jdbcãsqlite-jdbcã
<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>
pom.xml <?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>
config.propertiesæ§æ
ãã¡ã€ã«ãäœæã
ãŸã ã 次ã®ããŒã¿ãå«ãŸããŸãã
ïŒã¡ã€ã³ã·ãŒã³ã®ã¿ã€ãã«
title = JavaFXãšSpring BootïŒ
ïŒDBæ¥ç¶æ§æ
db.url = jdbcïŒsqliteïŒPATH_TO_DB / test_db
db.user =ãŠãŒã¶ãŒ
db.password =ãã¹ã¯ãŒã
db.driver = org.sqlite.JDBC
application-context.xmlã次ã®ã³ã³ãã³ããå«ããªãœãŒã¹ã«è¿œå ã
ãŸããå°ãªããšãæ¥ã«ç²ŸéããŠããã°ã以äžã®å
容ãç解ããã®ã«åé¡ã¯ãªããšæããŸãã
application-context.xml <?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>
ApplicationContextAwareã€ã³ã¿ãŒãã§ãŒã¹ãæ¡åŒµããæœè±¡ã³ã³ãããŒã©ãŒ
ControllerãäœæããŠãä»»æã®ã³ã³ãããŒã©ãŒããã³ã³ããã¹ããååŸã§ããããã«ããŸãã
Controller.java 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; } }
次ã«ã
SpringStageLoaderã·ãŒã³
ããŒããŒãå®è£
ããŸãã ããã¯ãããŸããŸãªã·ãŒã³ããŠã£ã³ããŠãããŒãã§ãããŠãŒãã£ãªãã£ã¯ã©ã¹ã®ãããªãã®ã«ãªããããããã«èšå€§ãªãã®ã«ãªãããšãããããŸããã
SpringStageLoader.java 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;
Launcherã¯ã©ã¹ã®startã¡ãœãããå°ãæžãæããŸãã ãŸããã³ã³ããã¹ãã®åæåãšãã®ãªãªãŒã¹ãè¿œå ããŸãã
Launcher.java 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(); } }
ControllerããMainControllerã¯ã©ã¹ãç¶æ¿ãããã¹ãŠã®ã³ã³ãããŒã©ãŒã«
Componentã¢ãããŒã·ã§ã³ãè¿œå ããããšãå¿ããªãã§ãã ãããããã«ããã
ã³ã³ããŒãã³ãã¹ãã£ã³ãä»ããŠã³ã³ããã¹ãã«è¿œå ããããã³ãªã©ã®ã³ã³ããã¹ãããã³ã³ãããŒã©ãŒãååŸãŸãã¯æ¿å
¥ããŸãã ããã§ãªãå ŽåãäŸå€ãçºçããŸã
org.springframework.beans.factory.NoSuchBeanDefinitionExceptionïŒå©çšå¯èœãªã¿ã€ã 'org.name.app.controller.MainController'ã®ä¿®é£ŸBeanã¯ãããŸãã
éå§ãããšããŠã£ã³ããŠã¿ã€ãã«ã®ããã¹ããããããã£ã«ç»é²ãããããã«ãªã£ãŠããããšãããããŸãã

ãã ããããŒã¿ã®èªã¿èŸŒã¿ã¯ãŸã åèšèšãããŠãããã補åã«é¢ãã詳现æ
å ±ã衚瀺ãããŠããŸããã
ProductDaoã¯ã©ã¹ãå®è£
ããŸã
ProductDao.java 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; @Autowired public ProductDao(DataSource dataSource) { this.template = new JdbcTemplate(dataSource); } public List<Product> getAllProducts(){ String sql = "SELECT * FROM product"; return template.query(sql, new BeanPropertyRowMapper<>(Product.class)); } }
ããã§ãã¡ã€ã³ã³ã³ãããŒã©ãŒã«2ã3è¡è¿œå ããã®ã§ããã¿ã³ãã¯ãªãã¯ãããšãããŒãã«ã«ããŒã¿ãèªã¿èŸŒãŸããŸãã
MainController.java 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); } }
補åã®è©³çŽ°ãå«ãæ°ãããŠã£ã³ããŠãéãããšãå®çŸããŸãã ãããè¡ãã«ã¯ã
productDetailsãã³ãã¬ãŒããš
ProductDetailsModalStageã·ãŒã³ã䜿çšããŸãã
productDetails.fxml <?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>
ProductDetailsModalStage.java 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); } }
SpringStageLoader㧠ãå¥ã®ã¡ãœãããè¿œå ããŸãã
public static Scene loadScene(String fxmlName) throws IOException { return new Scene(load(fxmlName)); }
ProductTableControlleråæåã¡ãœããã«æ°è¡ãè¿œå ããŸãã
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; });
éå§ããŠçµæã確èªããŸãã

åé¡ã¯ãã³ã³ããã¹ãã®é·ãåæåã§ã
ãããŠãããäžã€èå³æ·±ããããã¯ããããŸãã ã³ã³ããã¹ããé·æéåæåããããšä»®å®ããŸãããã®å ŽåããŠãŒã¶ãŒã¯ã¢ããªã±ãŒã·ã§ã³ãèµ·åããŠãããã©ãããç解ã§ããŸããã ãããã£ãŠãæ確ã«ããããã«ãã³ã³ããã¹ãã®åæåäžã«ã¹ãã©ãã·ã¥ã¹ã¯ãªãŒã³ãè¿œå ããå¿
èŠããããŸãã
FXMLLoaderã䜿çšããŠãéåžžã®æ¹æ³ã§ã¹ã¯ãªãŒã³ã»ãŒããŒã䜿çšããŠã·ãŒã³ã
èšè¿°ããŸãã ãªããªã ãã®æç¹ã§ã³ã³ããã¹ããåæåãããŸãã éãã³ã³ããã¹ãã®åæåã¯ãThread.sleepïŒ10000ïŒãåŒã³åºãããšã§ã·ãã¥ã¬ãŒããããŸãã
åçä»ãã®ãã³ãã¬ãŒãïŒ
splash.fxml <?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>
ã¹ã¯ãªãŒã³ã»ãŒããŒã¢ããªã±ãŒã·ã§ã³ãããŠã³ããŒãããããã«ä¿®æ£ããã
ã©ã³ãã£ãŒLauncher.java 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); } @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(); } }
å¿
èŠãªãã®ãçµã¿ç«ãŠãå®è¡ããååŸããŸãã
ã¢ããªGIFãèµ·åãã æçµçµç«ãŠç¶
æåŸã®ã¹ããããæ®ã£ãã ããã¯JARããã«ãããããã§ãããSpringã䜿çšããŸãã ãããè¡ãã«ã¯ãå¥ã®
maven-shade-pluginãã©ã°ã€ã³ã
pomã«è¿œå ã
ãŸã ã
pom.xml-æçµããŒãžã§ã³ <?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>
ãã®ãããªç°¡åãªæ¹æ³ã§ãSpringãšJavaFXãåéã«ããããšãã§ããŸãã æçµãããžã§ã¯ãæ§é ïŒ
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
GitHubã®ãœãŒã¹ã
ããŒã¿ããŒã¹å
ã®ããŒãã«çšã®PRODUCTS.sqlãã¡ã€ã«ããããŸãã