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
๊ธ์ญ์
ํ๋ก๊ทธ๋จ ์ข ๋ฃ
ํ๋ก๊ทธ๋จ ์์ด์ฝ
๋ฐ์ดํฐ ์ ๊ทผ ๊ณ์ธต ๊ฐ๋ฐ
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
๋ณต์ฌ