Search

JavaFX ๊ฒŒ์‹œํŒ ํ”„๋กœ๊ทธ๋žจ

JavaFX ๊ฒŒ์‹œํŒ ํ”„๋กœ๊ทธ๋žจ

์ค€๋น„์‚ฌํ•ญ

ํ”„๋กœ์ ํŠธ ๊ตฌ์กฐ

JavaFX_Board
โ€ข
src
โ—ฆ
application
โ–ช
controller
โ€ข
MainController.java
โ€ข
InsertController.java
โ€ข
ReadController.java
โ€ข
UpdateController.java
โ€ข
UI.java
โ–ช
DAO
โ€ข
BoardDAO.java
โ€ข
JDBConnection.java
โ–ช
DTO
โ€ข
Board.java
โ–ช
Service
โ€ข
BoardService.java
โ€ข
BoardServiceImpl.java
โ–ช
UI
โ€ข
Insert.fxml
โ€ข
Read.fxml
โ€ข
Update.fxml
โ–ช
util
โ€ข
SceneUtil.java
โ–ช
Main.java
โ–ช
application.css
โ–ช
Main.fxml
โ—ฆ
module-info.java
โ—ฆ
icon.png

๊ฐœ๋ฐœ์ˆœ์„œ

1.
DB ์„ค์ •
a.
์Šคํ‚ค๋งˆ ์ƒ์„ฑ
b.
๊ณ„์ • ์ƒ์„ฑ
c.
ํ…Œ์ด๋ธ” ์ƒ์„ฑ
2.
๋ชจ๋“ˆ ์„ค์ •
a.
module-info.java
3.
ํ”„๋ฆฌ์  ํ…Œ์ด์…˜ ๊ณ„์ธต ๊ฐœ๋ฐœ (ํ™”๋ฉด๊ฐœ๋ฐœ)
a.
Main.java
b.
Main.fxml
c.
UI
i.
Insert.fxml
ii.
Read.fxml
iii.
Update.fxml
4.
๋ฐ์ดํ„ฐ ์ ‘๊ทผ ๊ณ„์ธต ๊ฐœ๋ฐœ
a.
DTO
i.
Board.java
b.
DAO
i.
JDBConnection.java
ii.
BoardDAO.java
5.
๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง ๊ณ„์ธต ๊ฐœ๋ฐœ
a.
Service
i.
BoardService.java
ii.
BoardServiceImpl.java
6.
์ปจํŠธ๋กค๋Ÿฌ ๊ฐœ๋ฐœ
a.
util
i.
SceneUtil.java
b.
Controller
i.
UI.java
ii.
MainController.java
iii.
InsertController.java
iv.
ReadController.java
v.
UpdateController.java
7.
์Šคํƒ€์ผ๋ง
a.
application.css

ํ”„๋กœ์ ํŠธ ๋ช… : JavaFX_Board

DB ์„ค์ •

์Šคํ‚ค๋งˆ ์ƒ์„ฑ : aloha

๊ณ„์ • ์ƒ์„ฑ

โ€ข
์•„์ด๋”” : aloha
โ€ข
๋น„๋ฐ€๋ฒˆํ˜ธ : 123456

ํ…Œ์ด๋ธ” ์ƒ์„ฑ

ํ…Œ์ด๋ธ”๋ช… : board

CREATE TABLE `board` ( `board_no` int NOT NULL AUTO_INCREMENT, `title` varchar(200) NOT NULL, `writer` varchar(100) NOT NULL, `content` text , `reg_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, `upd_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (`board_no`) );
Java
๋ณต์‚ฌ

๋ชจ๋“ˆ ์„ค์ •

module-info.java

module JavaFX_Board { requires javafx.base; requires javafx.controls; requires javafx.fxml; requires java.sql; opens application to javafx.base, javafx.graphics, javafx.fxml; opens application.Controller to javafx.fxml; opens application.DTO to javafx.base; }
Java
๋ณต์‚ฌ

ํ”„๋ฆฌ์  ํ…Œ์ด์…˜ ๊ณ„์ธต ๊ฐœ๋ฐœ (ํ™”๋ฉด๊ฐœ๋ฐœ)

๋ฉ”์ธํ™”๋ฉด - Main.java

package application; import application.util.SceneUtil; import javafx.application.Application; import javafx.fxml.FXMLLoader; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.scene.image.Image; import javafx.stage.Stage; public class Main extends Application { @Override public void start(Stage stage) { try { Parent root = FXMLLoader.load(getClass().getResource("Main.fxml")); Scene scene = new Scene(root); scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm()); stage.setScene(scene); // ์Šคํ…Œ์ด์ง€ ์„ค์ • Image icon = new Image("icon.png"); stage.getIcons().add(icon); stage.setTitle("๊ฒŒ์‹œํŒ"); stage.setResizable(false); stage.show(); // ๋‹ซ๊ธฐ ์š”์ฒญ ์ด๋ฒคํŠธ ์ฒ˜๋ฆฌ stage.setOnCloseRequest(event -> { event.consume(); // ๊ธฐ์กด ์ด๋ฒคํŠธ(ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒ)๋ฅผ ์ œ๊ฑฐ. consume "์†Œ๋ฉธ์‹œํ‚ค๋‹ค" SceneUtil.getInstance().close(stage); }); } catch(Exception e) { e.printStackTrace(); } } public static void main(String[] args) { launch(args); } }
Java
๋ณต์‚ฌ

๋ฉ”์ธํ™”๋ฉด - Main.fxml

๊ธ€์“ฐ๊ธฐ ํ™”๋ฉด UI - Insert.fxml

๊ธ€์ฝ๊ธฐ ํ™”๋ฉด UI - Read.fxml

๊ธ€์ˆ˜์ • ํ™”๋ฉด UI - Update.fxml

๊ธ€์‚ญ์ œ

ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒ

ํ”„๋กœ๊ทธ๋žจ ์•„์ด์ฝ˜

icon.png
837.2KB

๋ฐ์ดํ„ฐ ์ ‘๊ทผ ๊ณ„์ธต ๊ฐœ๋ฐœ

DTO - Board.java

package application.DTO; import java.text.SimpleDateFormat; import java.util.Date; import javafx.scene.control.CheckBox; public class Board { private int boardNo; private String title; private String writer; private String content; private String regDate; private String updDate; private CheckBox cbDelete; // ๋‚ ์งœ ํ˜•์‹ SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // ์ƒ์„ฑ์ž public Board() { this("์ œ๋ชฉ์—†์Œ", "๊ธ€์“ด์ด์—†์Œ", "๋‚ด์šฉ์—†์Œ"); } public Board(String title, String writer, String content) { this.title = title; this.writer = writer; this.content = content; this.regDate = dateFormat.format(new Date()); this.updDate = dateFormat.format(new Date()); this.cbDelete = new CheckBox(); } // getter, setter public int getBoardNo() { return boardNo; } public void setBoardNo(int boardNo) { this.boardNo = boardNo; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getWriter() { return writer; } public void setWriter(String writer) { this.writer = writer; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public String getRegDate() { return regDate; } public void setRegDate(String regDate) { this.regDate = regDate; } public String getUpdDate() { return updDate; } public void setUpdDate(String updDate) { this.updDate = updDate; } public CheckBox getCbDelete() { return cbDelete; } public void setCbDelete(CheckBox cbDelete) { this.cbDelete = cbDelete; } @Override public String toString() { return "Board [boardNo=" + boardNo + ", title=" + title + ", writer=" + writer + ", content=" + content + ", regDate=" + regDate + ", updDate=" + updDate + "]"; } }
Java
๋ณต์‚ฌ

DAO - JDBConnection.java

package application.DAO; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.Statement; public class JDBConnection { public Connection con; // ์—ฐ๊ฒฐ๋œ ๋“œ๋ผ์ด๋ฒ„์— SQL์„ ์š”์ฒญํ•  ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋Š” ํด๋ž˜์Šค public Statement stmt; // SQL ์‹คํ–‰ ์š”์ฒญ์„ ํ•˜๋Š” ํด๋ž˜์Šค public PreparedStatement psmt; // Statement ์—์„œ ? ํŒŒ๋ผ๋ฏธํ„ฐ ํ™•์žฅ๊ธฐ๋Šฅ์„ ์ถ”๊ฐ€๋กœ ์ œ๊ณตํ•˜๋Š” ํด๋ž˜์Šค public ResultSet rs; // SQL ์‹คํ–‰ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ›์•„์˜ค๋Š” ํด๋ž˜์Šค // ๊ธฐ๋ณธ ์ƒ์„ฑ์ž public JDBConnection() { // JDBC ๋“œ๋ผ์ด๋ฒ„ ๋กœ๋“œ // MySQL try { // mysql-connector-j.xxx.jar ๋“œ๋ผ์ด๋ฒ„์˜ ํด๋ž˜์Šค๋ฅผ ๋กœ๋“œํ•œ๋‹ค. Class.forName("com.mysql.cj.jdbc.Driver"); // DB์— ์—ฐ๊ฒฐ // - ์—ฐ๊ฒฐ์— ํ•„์š”ํ•œ ์ •๋ณด : URL, id, pw // URL : jdbc:mysql://๋„๋ฉ”์ธ:[PORT]/[์Šคํ‚ค๋งˆ]?์˜ต์…˜ํŒŒ๋ผ๋ฏธํ„ฐ // * ๋‚ด PC์˜ IP์ฃผ์†Œ : localhost : 127.0.0.1 // * 3306 : MySQL ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ ๊ธฐ๋ณธ ํฌํŠธ String url = "jdbc:mysql://127.0.0.1:3306/aloha?serverTimezone=Asia/Seoul&allowPublicKeyRetrieval=true&useSSL=false"; String id = "aloha"; String pw = "123456"; // ์ž๋ฐ” ํ”„๋กœ๊ทธ๋žจ์—์„œ JDBC ๋“œ๋ผ์ด๋ฒ„๋ฅผ ์—ฐ๊ฒฐ์‹œ์ผœ์ฃผ๋Š” ํด๋ž˜์Šค // getConnection() ๋ฉ”์†Œ๋“œ๋กœ DB์— ์—ฐ๊ฒฐ ์š”์ฒญํ•˜๊ณ  ์ƒ์„ฑ๋œ Connection ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜๋ฐ›๋Š”๋‹ค. con = DriverManager.getConnection(url, id, pw); System.out.println("DB ์—ฐ๊ฒฐ ์„ฑ๊ณต"); } catch (Exception e) { System.err.println("DB ์—ฐ๊ฒฐ ์‹คํŒจ"); e.printStackTrace(); } } // public static void main(String[] args) { // JDBConnection jdbc = new JDBConnection(); // } }
Java
๋ณต์‚ฌ

DAO - BoardDAO.java

package application.DAO; import java.sql.SQLException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.List; import application.DTO.Board; public class BoardDAO extends JDBConnection { // ๋‚ ์งœ ํ˜•์‹ SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // ๋ฐ์ดํ„ฐ ๋ชฉ๋ก ์กฐํšŒ public List<Board> selectList() { List<Board> boardList = new ArrayList<Board>(); // SQL String sql = " SELECT * " + " FROM board "; try { stmt = con.createStatement(); // ์ฟผ๋ฆฌ ์‹คํ–‰ ๊ฐ์ฒด ์ƒ์„ฑ rs = stmt.executeQuery(sql); // ์ฟผ๋ฆฌ ์‹คํ–‰ - ๊ฒฐ๊ณผ-->rs (ResultSet) // ์กฐํšŒ ๊ฒฐ๊ณผ๋ฅผ ๋ฆฌ์ŠคํŠธ์— ์ถ”๊ฐ€ while( rs.next() ) { // next() : ์‹คํ–‰ ๊ฒฐ๊ณผ์˜ ๋‹ค์Œ ๋ฐ์ดํ„ฐ๋กœ ์ด๋™ Board board = new Board(); // ๊ฒฐ๊ณผ ๋ฐ์ดํ„ฐ ๊ฐ€์ ธ์˜ค๊ธฐ // rs.getXXX("์ปฌ๋Ÿผ๋ช…") --> ํ•ด๋‹น ์ปฌ๋Ÿผ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค // - ์‹คํ–‰ ๊ฒฐ๊ณผ์—์„œ, "์ปฌ๋Ÿผ๋ช…"์˜ ๊ฐ’์„ ํŠน์ • ํƒ€์ž…์œผ๋กœ ๋ฐ˜ํ™˜ board.setBoardNo( rs.getInt("board_no") ); board.setTitle( rs.getString("title") ); board.setWriter( rs.getString("writer") ); board.setContent( rs.getString("content") ); board.setRegDate( dateFormat.format( rs.getTimestamp("reg_date") ) ); board.setUpdDate( dateFormat.format( rs.getTimestamp("upd_date") ) ); boardList.add(board); } } catch (SQLException e) { System.err.println("๊ฒŒ์‹œ๊ธ€ ๋ชฉ๋ก ์กฐํšŒ ์‹œ, ์—๋Ÿฌ ๋ฐœ์ƒ"); e.printStackTrace(); } return boardList; } // ๋ฐ์ดํ„ฐ ์กฐํšŒ public Board select(int no) { Board board = new Board(); String sql = " SELECT * " + " FROM board " + " WHERE board_no = ? "; try { psmt = con.prepareStatement(sql); // ์ฟผ๋ฆฌ ์‹คํ–‰ ๊ฐ์ฒด ์ƒ์„ฑ (PrepareStatement) // psmt.setXXX( ์ˆœ์„œ๋ฒˆํ˜ธ, ๋งคํ•‘ํ•  ๊ฐ’ ); psmt.setInt(1, no); // ?(1) <-- boardNo(๊ธ€๋ฒˆํ˜ธ) // setXXX(์ˆœ์„œ, ๊ฐ’) : SQL ์˜ ์ง€์ •ํ•œ ์ˆœ์„œ(?)์— ์žˆ๋Š” ํŒŒ๋ผ๋ฏธํ„ฐ์— ๊ฐ’์„ ๋งคํ•‘ // - ์—ฌ๊ธฐ์„œ๋Š”, "1๋ฒˆ ? ์— ๊ธ€๋ฒˆํ˜ธ no ๊ฐ’์„ ๋งคํ•‘ํ•œ๋‹ค." rs = psmt.executeQuery(); // ์ฟผ๋ฆฌ ์‹คํ–‰ // executeQuery() // : SQL (SELECT) ์„ ์‹คํ–‰ํ•˜๊ณ  ๊ฒฐ๊ณผ๋ฅผ ResultSet ๊ฐ์ฒด๋กœ ๋ฐ˜ํ™˜ // ์กฐํšŒ ๊ฒฐ๊ณผ ๊ฐ€์ ธ์˜ค๊ธฐ if( rs.next() ) { board.setBoardNo( rs.getInt("board_no") ); board.setTitle( rs.getString("title") ); board.setWriter( rs.getString("writer") ); board.setContent( rs.getString("content") ); board.setRegDate( dateFormat.format( rs.getTimestamp("reg_date") ) ); board.setUpdDate( dateFormat.format( rs.getTimestamp("upd_date") ) ); } } catch (SQLException e) { System.err.println("๊ฒŒ์‹œ๊ธ€ ์กฐํšŒ ์‹œ, ์—๋Ÿฌ ๋ฐœ์ƒ"); e.printStackTrace(); } return board; } // ๋ฐ์ดํ„ฐ ๋“ฑ๋ก public int insert(Board board) { int result = 0; String sql = " INSERT INTO board( title, writer, content ) " + " VALUES( ?, ?, ? ) "; try { psmt = con.prepareStatement(sql); // ์ฟผ๋ฆฌ ์‹คํ–‰ ๊ฐ์ฒด ์ƒ์„ฑ psmt.setString(1, board.getTitle()); // 1๋ฒˆ ? ์— ์ œ๋ชฉ์„ ๋งคํ•‘ psmt.setString(2, board.getWriter()); // 2๋ฒˆ ? ์— ์ž‘์„ฑ์ž์„ ๋งคํ•‘ psmt.setString(3, board.getContent()); // 3๋ฒˆ ? ์— ๋‚ด์šฉ์„ ๋งคํ•‘ result = psmt.executeUpdate(); // SQL ์‹คํ–‰ ์š”์ฒญ, ์ ์šฉ๋œ ๋ฐ์ดํ„ฐ ๊ฐœ์ˆ˜๋ฅผ ๋ฐ›์•„์˜ด // ๊ฒŒ์‹œ๊ธ€ 1๊ฐœ ์“ฐ๊ธฐ ์„ฑ๊ณต ์‹œ, result : 1 // ์‹คํŒจ ์‹œ, result : 0 // executeUpdate() // : SQL (INSERT, UPDATE, DELETE)์„ ์‹คํ–‰ํ•˜๊ณ  ์ ์šฉ๋œ ๋ฐ์ดํ„ฐ ๊ฐœ์ˆ˜๋ฅผ int ํƒ€์ž…์œผ๋กœ ๋ฐ˜ํ™˜ } catch (SQLException e) { System.err.println("๊ฒŒ์‹œ๊ธ€ ๋“ฑ๋ก ์‹œ, ์—๋Ÿฌ ๋ฐœ์ƒ"); e.printStackTrace(); } return result; } // ๋ฐ์ดํ„ฐ ์ˆ˜์ • public int update(Board board) { int result = 0; String sql = " UPDATE board " + " SET title = ? " + " ,writer = ? " + " ,content = ? " + " ,upd_date = now() " + " WHERE board_no = ? " ; // - now() : ํ˜„์žฌ ๋‚ ์งœ/์‹œ๊ฐ„์„ ๋ฐ˜ํ™˜ํ•˜๋Š” MySQL ํ•จ์ˆ˜ try { psmt = con.prepareStatement(sql); psmt.setString(1, board.getTitle()); // 1๋ฒˆ ? ์— ์ œ๋ชฉ์„ ๋งคํ•‘ psmt.setString(2, board.getWriter()); // 2๋ฒˆ ? ์— ์ž‘์„ฑ์ž์„ ๋งคํ•‘ psmt.setString(3, board.getContent()); // 3๋ฒˆ ? ์— ๋‚ด์šฉ์„ ๋งคํ•‘ psmt.setInt(4, board.getBoardNo()); // 4๋ฒˆ ? ์— ๊ฒŒ์‹œ๊ธ€ ๋ฒˆํ˜ธ๋ฅผ ๋งคํ•‘ result = psmt.executeUpdate(); // SQL ์‹คํ–‰ ์š”์ฒญ, ์ ์šฉ๋œ ๋ฐ์ดํ„ฐ ๊ฐœ์ˆ˜๋ฅผ ๋ฐ›์•„์˜ด // ๊ฒŒ์‹œ๊ธ€ 1๊ฐœ ์ˆ˜์ • ์„ฑ๊ณต ์‹œ, result : 1 // ์‹คํŒจ ์‹œ, result : 0 } catch (SQLException e) { System.err.println("๊ฒŒ์‹œ๊ธ€ ์ˆ˜์ • ์‹œ, ์—๋Ÿฌ ๋ฐœ์ƒ"); e.printStackTrace(); } return result; } // ๋ฐ์ดํ„ฐ ์‚ญ์ œ public int delete(int no) { int result = 0; String sql = " DELETE FROM board " + " WHERE board_no = ? "; try { psmt = con.prepareStatement(sql); // ์ฟผ๋ฆฌ ์‹คํ–‰ ๊ฐ์ฒด ์ƒ์„ฑ psmt.setInt(1, no); // 1๋ฒˆ ? ์— ๊ธ€๋ฒˆํ˜ธ๋ฅผ ๋งคํ•‘ result = psmt.executeUpdate(); // SQL ์‹คํ–‰ ์š”์ฒญ } catch (SQLException e) { System.err.println("๊ฒŒ์‹œ๊ธ€ ์‚ญ์ œ ์‹œ, ์—๋Ÿฌ ๋ฐœ์ƒ"); e.printStackTrace(); } return result; } }
Java
๋ณต์‚ฌ

๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง ๊ณ„์ธต ๊ฐœ๋ฐœ

Service - BoardService.java

package application.service; import java.util.List; import application.DTO.Board; public interface BoardService { // ๊ฒŒ์‹œ๊ธ€ ๋ชฉ๋ก List<Board> list(); // ๊ฒŒ์‹œ๊ธ€ ์กฐํšŒ Board select(int boardNo); // ๊ฒŒ์‹œ๊ธ€ ์ถ”๊ฐ€ int insert(Board board); // ๊ฒŒ์‹œ๊ธ€ ์ˆ˜์ • int update(Board board); // ๊ฒŒ์‹œ๊ธ€ ์‚ญ์ œ int delete(int boardNo); }
Java
๋ณต์‚ฌ

Service - BoardServiceImpl.java

package application.service; import java.util.List; import application.DAO.BoardDAO; import application.DTO.Board; public class BoardServiceImpl implements BoardService { private BoardDAO boardDAO = new BoardDAO(); @Override public List<Board> list() { List<Board> boardList = (List<Board>) boardDAO.selectList(); return boardList; } @Override public Board select(int boardNo) { Board board = boardDAO.select(boardNo); return board; } @Override public int insert(Board board) { int result = boardDAO.insert(board); return result; } @Override public int update(Board board) { int result = boardDAO.update(board); return result; } @Override public int delete(int boardNo) { int result = boardDAO.delete(boardNo); return result; } }
Java
๋ณต์‚ฌ

์ปจํŠธ๋กค๋Ÿฌ ๊ฐœ๋ฐœ

Util - SceneUtil.java

package application.util; import java.io.IOException; import javafx.event.ActionEvent; import javafx.fxml.FXMLLoader; import javafx.scene.Node; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.scene.control.Alert; import javafx.scene.control.Alert.AlertType; import javafx.scene.control.ButtonType; import javafx.scene.input.MouseEvent; import javafx.stage.Stage; import javafx.stage.WindowEvent; public class SceneUtil { private static SceneUtil instance; Stage stage; Scene scene; Parent root; FXMLLoader loader; private SceneUtil() { // private ์ƒ์„ฑ์ž } public static SceneUtil getInstance() { if (instance == null) { instance = new SceneUtil(); } return instance; } /** * ํ™”๋ฉด์ด๋™ (ActionEvent) * @param event * @param fxml * @throws IOException */ public void switchScene(ActionEvent event, String fxml) throws IOException { loader = new FXMLLoader(getClass().getResource(fxml)); root = loader.load(); stage = (Stage) ((Node) event.getSource()).getScene().getWindow(); scene = new Scene(root); stage.setScene(scene); stage.show(); } /** * ํ™”๋ฉด์ด๋™ (MouseEvent) * @param event * @param fxml * @throws IOException */ public void switchScene(MouseEvent event, String fxml) throws IOException { loader = new FXMLLoader(getClass().getResource(fxml)); root = loader.load(); stage = (Stage) ((Node) event.getSource()).getScene().getWindow(); scene = new Scene(root); stage.setScene(scene); stage.show(); } /** * ํ™”๋ฉด ์ด๋™ (์ง€์ •๋œ root ์ธ์Šคํ„ด์Šค) * @param event * @param fxml * @param root * @throws IOException */ public void switchScene(ActionEvent event, String fxml, Parent root) throws IOException { stage = (Stage) ((Node) event.getSource()).getScene().getWindow(); scene = new Scene(root); stage.setScene(scene); stage.show(); } /** * ํ™”๋ฉด ์ด๋™ (์ง€์ •๋œ root ์ธ์Šคํ„ด์Šค) * @param event * @param fxml * @param root * @throws IOException */ public void switchScene(MouseEvent event, String fxml, Parent root) throws IOException { stage = (Stage) ((Node) event.getSource()).getScene().getWindow(); scene = new Scene(root); stage.setScene(scene); stage.show(); } /** * fxml ์— ์ง€์ •๋œ Controller ๊ฐ€์ ธ์˜ค๊ธฐ * @param fxml * @return * @throws IOException */ public Object getController(String fxml) throws IOException { loader = new FXMLLoader(getClass().getResource(fxml)); root = loader.load(); return loader.getController(); } /** * ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒ * @param event */ public void close(ActionEvent event) { Alert alert = new Alert(AlertType.CONFIRMATION); alert.setTitle("ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒ"); alert.setHeaderText("ํ”„๋กœ๊ทธ๋žจ์„ ์ข…๋ฃŒํ•˜์‹œ๊ฒ ์Šต๋‹ˆ๊นŒ?"); alert.setContentText("ํ”„๋กœ๊ทธ๋žจ์ด ์ข…๋ฃŒ๋ฉ๋‹ˆ๋‹ค."); if( alert.showAndWait().get() == ButtonType.OK ) { System.out.println("ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒ"); stage = (Stage) ((Node) event.getSource()).getScene().getWindow(); stage.close(); } } /** * ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒ * @param event */ public void close(MouseEvent event) { Alert alert = new Alert(AlertType.CONFIRMATION); alert.setTitle("ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒ"); alert.setHeaderText("ํ”„๋กœ๊ทธ๋žจ์„ ์ข…๋ฃŒํ•˜์‹œ๊ฒ ์Šต๋‹ˆ๊นŒ?"); alert.setContentText("ํ”„๋กœ๊ทธ๋žจ์ด ์ข…๋ฃŒ๋ฉ๋‹ˆ๋‹ค."); if( alert.showAndWait().get() == ButtonType.OK ) { System.out.println("ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒ"); stage = (Stage) ((Node) event.getSource()).getScene().getWindow(); stage.close(); } } /** * ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒ * @param event */ public void close(Stage stage) { Alert alert = new Alert(AlertType.CONFIRMATION); alert.setTitle("ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒ"); alert.setHeaderText("ํ”„๋กœ๊ทธ๋žจ์„ ์ข…๋ฃŒํ•˜์‹œ๊ฒ ์Šต๋‹ˆ๊นŒ?"); alert.setContentText("ํ”„๋กœ๊ทธ๋žจ์ด ์ข…๋ฃŒ๋ฉ๋‹ˆ๋‹ค."); if( alert.showAndWait().get() == ButtonType.OK ) { System.out.println("ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒ"); stage.close(); } } /* getter & setter */ public FXMLLoader getLoader() { return loader; } public void setLoader(FXMLLoader loader) { this.loader = loader; } public Parent getRoot() { return root; } public void setRoot(Parent root) { this.root = root; } }
Java
๋ณต์‚ฌ

Controller - UI.java

package application.Controller; public enum UI { LIST("/application/Main.fxml"), INSERT("/application/UI/Insert.fxml"), READ("/application/UI/Read.fxml"), UPDATE("/application/UI/Update.fxml"); private final String path; UI(String path) { this.path = path; } public String getPath() { return path; } }
Java
๋ณต์‚ฌ

Controller - MainController.java

package application.controller; import java.io.IOException; import java.net.URL; import java.util.ArrayList; import java.util.List; import java.util.ResourceBundle; import application.DTO.Board; import application.service.BoardService; import application.service.BoardServiceImpl; import application.util.SceneUtil; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.fxml.FXML; import javafx.fxml.FXMLLoader; import javafx.fxml.Initializable; import javafx.scene.Node; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.scene.control.TableColumn; import javafx.scene.control.TableView; import javafx.scene.control.cell.PropertyValueFactory; import javafx.scene.input.MouseEvent; import javafx.stage.Stage; public class Controller implements Initializable { @FXML private TableView<Board> boardTableView; @FXML private TableColumn<Board, Integer> colBoardNo; @FXML private TableColumn<Board, String> colTitle; @FXML private TableColumn<Board, String> colWriter; @FXML private TableColumn<Board, String> colRegDate; @FXML private TableColumn<Board, String> colUpdDate; BoardService boardService = new BoardServiceImpl(); Stage stage; Scene scene; Parent root; @Override public void initialize(URL arg0, ResourceBundle arg1) { /* ๋ฐ์ดํ„ฐ ์ดˆ๊ธฐํ™” */ List<Board> boardList = new ArrayList<Board>(); boardList = boardService.list(); // Board board1 = new Board(1,"์ œ๋ชฉ1","์ž‘์„ฑ์ž1","๋‚ด์šฉ1","2023-08-18","2023-08-18"); // Board board2 = new Board(2,"์ œ๋ชฉ2","์ž‘์„ฑ์ž2","๋‚ด์šฉ2","2023-08-18","2023-08-18"); // Board board3 = new Board(3,"์ œ๋ชฉ3","์ž‘์„ฑ์ž3","๋‚ด์šฉ3","2023-08-18","2023-08-18"); // boardList.add(board1); // boardList.add(board2); // boardList.add(board3); ObservableList<Board> list = FXCollections.observableArrayList( // board1, board2, board3 boardList ); // TablveColumn ์— ์—ด๊ณผ ์ถ”๊ฐ€ํ•  ์•„์ดํ…œ ๊ฐ์ฒด์˜ ๊ฒŒํ„ฐ ์ด๋ฆ„ ์ง€์ •ํ•˜๊ธฐ // - setCellValueFactory() : ์…€์˜ ๊ฐ’์„ ์ง€์ •ํ•˜๋Š” ๋ฉ”์†Œ๋“œ // - new PropertyValueFactory("๊ฒŒํ„ฐ์ด๋ฆ„") // * ๊ฒŒํ„ฐ์ด๋ฆ„ : get์„ ์ œ์™ธํ•œ ๊ฒŒํ„ฐ ๋ฉ”์†Œ๋“œ์˜ ์ด๋ฆ„ // ex) getBoardNo --> BoardNo // โ€ป module-info.java --> java.base ๋ชจ๋“ˆ ์ถ”๊ฐ€ colBoardNo.setCellValueFactory(new PropertyValueFactory<>("BoardNo")); colTitle.setCellValueFactory(new PropertyValueFactory<>("Title")); colWriter.setCellValueFactory(new PropertyValueFactory<>("Writer")); colRegDate.setCellValueFactory(new PropertyValueFactory<>("RegDate")); colUpdDate.setCellValueFactory(new PropertyValueFactory<>("UpdDate")); // TableView ์— ๋ฐ์ดํ„ฐ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ง€์ • // - ๋ฏธ๋ฆฌ ๋งคํ•‘๋œ TableColumn ์— ๋ฆฌ์ŠคํŠธ์˜ ์š”์†Œ ๊ฐ์ฒด์˜ ๋ณ€์ˆ˜๊ฐ’์ด ์ง€์ •๋จ boardTableView.setItems(list); /* ์ด๋ฒคํŠธ ์ฒ˜๋ฆฌ */ // ๋”๋ธ”ํด๋ฆญ ์ด๋ฒคํŠธ boardTableView.setOnMouseClicked(new EventHandler<MouseEvent>() { @Override public void handle(MouseEvent event) { // ๋”๋ธ” ํด๋ฆญ if( event.getClickCount() == 2 && boardTableView.getSelectionModel().getSelectedItem() != null ) { int boardNo = boardTableView.getSelectionModel().getSelectedItem().getBoardNo(); try { ReadController readController = (ReadController) SceneUtil.getInstance().getController(UI.READ.getPath()); readController.read(boardNo); Parent root = SceneUtil.getInstance().getRoot(); SceneUtil.getInstance().switchScene(event, UI.READ.getPath(), root); } catch (IOException e) { System.err.println("[๋ชฉ๋ก->์ฝ๊ธฐ] ํ™”๋ฉด ์ด๋™ ์ค‘ ์˜ˆ์™ธ ๋ฐœ์ƒ.."); e.printStackTrace(); } } } }); } // ๊ธ€์“ฐ๊ธฐ ํ™”๋ฉด์œผ๋กœ ์ด๋™ public void moveToInsert(ActionEvent event) throws IOException { System.out.println("๊ธ€์“ฐ๊ธฐ ํ™”๋ฉด..."); // Parent root = FXMLLoader.load(getClass().getResource(fxml)); // stage = (Stage) ((Node)event.getSource()).getScene().getWindow(); // scene = new Scene(root); // stage.setScene(scene); // stage.show(); // FXMLLoader loader = new FXMLLoader(getClass().getResource(fxml)); // root = loader.load(); // Sub ์”ฌ ๊ฐ€์ ธ์˜ด // stage = (Stage) ((Node) event.getSource()).getScene().getWindow(); // scene = new Scene(root); // stage.setScene(scene); // stage.show(); SceneUtil.getInstance().switchScene(event, UI.INSERT.getPath()); } // ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒํ•˜๊ธฐ public void close(ActionEvent event) { SceneUtil.getInstance().close(event); } }
Java
๋ณต์‚ฌ

Controller - InsertController.java

package application.controller; import java.io.IOException; import application.DTO.Board; import application.service.BoardService; import application.service.BoardServiceImpl; import application.util.SceneUtil; import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.scene.control.TextArea; import javafx.scene.control.TextField; public class InsertController { @FXML private TextField tfTitle; @FXML private TextField tfWriter; @FXML private TextArea taContent; private BoardService boardService = new BoardServiceImpl(); // ๋ชฉ๋ก ํ™”๋ฉด์œผ๋กœ ์ด๋™ public void moveToList(ActionEvent event) throws IOException { SceneUtil.getInstance().switchScene(event, UI.LIST.getPath()); } // ๊ธ€์“ฐ๊ธฐ ์ฒ˜๋ฆฌ public void insert(ActionEvent event) throws IOException { Board board = new Board(tfTitle.getText(), tfWriter.getText(), taContent.getText() ); int result = boardService.insert(board); if( result > 0 ) { System.out.println("๊ธ€์“ฐ๊ธฐ ์ฒ˜๋ฆฌ ์„ฑ๊ณต!"); SceneUtil.getInstance().switchScene(event, UI.LIST.getPath()); } } }
Java
๋ณต์‚ฌ

Controller - ReadController.java

package application.controller; import java.io.IOException; import application.DTO.Board; import application.service.BoardService; import application.service.BoardServiceImpl; import application.util.SceneUtil; import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.scene.Parent; import javafx.scene.control.TextArea; import javafx.scene.control.TextField; public class ReadController { @FXML private TextField tfTitle; @FXML private TextField tfWriter; @FXML private TextArea taContent; private BoardService boardService = new BoardServiceImpl(); int boardNo; // ๋ชฉ๋ก ํ™”๋ฉด์œผ๋กœ ์ด๋™ public void moveToList(ActionEvent event) throws IOException { SceneUtil.getInstance().switchScene(event, UI.LIST.getPath()); } // ์ˆ˜์ • ํ™”๋ฉด์œผ๋กœ ์ด๋™ public void moveToUpdate(ActionEvent event) throws IOException { UpdateController updateController = (UpdateController) SceneUtil.getInstance().getController(UI.UPDATE.getPath()); updateController.read(boardNo); Parent root = SceneUtil.getInstance().getRoot(); SceneUtil.getInstance().switchScene(event, UI.UPDATE.getPath(), root); } // ๊ธ€์ฝ๊ธฐ ์ฒ˜๋ฆฌ public void read(int boardNo) { this.boardNo = boardNo; Board board = boardService.select(boardNo); tfTitle.setText(board.getTitle()); tfWriter.setText(board.getWriter()); taContent.setText(board.getContent()); } }
Java
๋ณต์‚ฌ

Controller - UpdateController.java

package application.controller; import java.io.IOException; import application.DTO.Board; import application.service.BoardService; import application.service.BoardServiceImpl; import application.util.SceneUtil; import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.scene.control.Alert; import javafx.scene.control.ButtonType; import javafx.scene.control.TextArea; import javafx.scene.control.TextField; import javafx.scene.control.Alert.AlertType; public class UpdateController { @FXML private TextField tfTitle; @FXML private TextField tfWriter; @FXML private TextArea taContent; private BoardService boardService = new BoardServiceImpl(); int boardNo; // ๋ชฉ๋ก ํ™”๋ฉด์œผ๋กœ ์ด๋™ public void moveToList(ActionEvent event) throws IOException { SceneUtil.getInstance().switchScene(event, UI.LIST.getPath()); } // ๊ธ€์ˆ˜์ • ์ฒ˜๋ฆฌ public void update(ActionEvent event) throws IOException { Board board = new Board(tfTitle.getText(), tfWriter.getText(), taContent.getText() ); board.setBoardNo(boardNo); int result = boardService.update(board); if( result > 0 ) { System.out.println("๊ธ€์ˆ˜์ • ์ฒ˜๋ฆฌ ์„ฑ๊ณต!"); SceneUtil.getInstance().switchScene(event, UI.LIST.getPath()); } } // ๊ธ€์‚ญ์ œ ์ฒ˜๋ฆฌ public void delete(ActionEvent event) throws IOException { Alert alert = new Alert(AlertType.CONFIRMATION); alert.setTitle("๊ฒŒ์‹œ๊ธ€ ์‚ญ์ œ"); alert.setHeaderText("๊ฒŒ์‹œ๊ธ€์„ ์ •๋ง๋กœ ์‚ญ์ œํ•˜์‹œ๊ฒ ์Šต๋‹ˆ๊นŒ? (๊ธ€๋ฒˆํ˜ธ : " + boardNo + ")"); alert.setContentText("์‚ญ์ œ ์ฒ˜๋ฆฌ ํ›„์—๋Š” ๋˜๋Œ๋ฆด ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."); int result = 0; if( alert.showAndWait().get() == ButtonType.OK ) { result = boardService.delete(boardNo); } if( result > 0 ) { System.out.println("๊ธ€์‚ญ์ œ ์ฒ˜๋ฆฌ ์„ฑ๊ณต!"); SceneUtil.getInstance().switchScene(event, UI.LIST.getPath()); } } // ๊ธ€์ฝ๊ธฐ ์ฒ˜๋ฆฌ public void read(int boardNo) { this.boardNo = boardNo; Board board = boardService.select(boardNo); tfTitle.setText(board.getTitle()); tfWriter.setText(board.getWriter()); taContent.setText(board.getContent()); } }
Java
๋ณต์‚ฌ

์Šคํƒ€์ผ๋ง

application.css

/* JavaFX CSS - Leave this comment until you have at least create one rule which uses -fx-Property */ .button { -fx-background-color: linear-gradient(to bottom, derive(cornflowerblue, 20%), derive(cornflowerblue, -20%)); -fx-text-fill: white; -fx-background-radius: 5; -fx-padding: 10 20; -fx-font-size: 14; } .button:hover { -fx-background-color: linear-gradient(to bottom, derive(cornflowerblue, 10%), derive(cornflowerblue, -10%)); } /* TableView ํ—ค๋” ์Šคํƒ€์ผ */ .table-view { } .table-view .column-header { -fx-background-color: cornflowerblue; -fx-background-color: linear-gradient(to bottom, derive(cornflowerblue, 20%), derive(cornflowerblue, -20%)); -fx-text-fill: white; } .table-row-cell:filled:selected:focused, .table-row-cell:filled:selected { -fx-background-color: transparent; -fx-border-color: royalblue; } .table-row-cell:filled:selected:focused *, .table-row-cell:filled:selected * { -fx-text-fill: black; } .tableCol { -fx-font-size: 16px; -fx-alignment: CENTER; } .tableCol * { -fx-text-fill: white; } #tableColTitle { -fx-alignment: CENTER-LEFT; }
CSS
๋ณต์‚ฌ