Controller
JavaFX์์ FXML๋ก ์ค๊ณ๋ UI์ Java ์ฝ๋(๋ก์ง)๋ฅผ ์ฐ๊ฒฐํ๋ ์ญํ ์ ํ๋ ํด๋์ค์
๋๋ค.
์ด๋ฒคํธ ์ฒ๋ฆฌ
JavaFX ์ปจํธ๋กค๋ฌ๋ UI์์ ๋ฐ์ํ๋ ์ด๋ฒคํธ(๋ฒํผ ํด๋ฆญ, ํค ์
๋ ฅ ๋ฑ)๋ฅผ ๊ฐ์งํ๊ณ ์ฒ๋ฆฌํฉ๋๋ค.
์ฃผ์ ์ด๋ฒคํธ ์ข
๋ฅ
์ด๋ฒคํธ ํ์
| ์ค๋ช
|
onAction | ๋ฒํผ ํด๋ฆญ, ๋ฉ๋ด ์ ํ ๋ฑ์ ์ก์
์ฒ๋ฆฌ |
onMouseClicked | ๋ง์ฐ์ค ํด๋ฆญ ์ด๋ฒคํธ |
onKeyPressed | ํค๋ณด๋ ์
๋ ฅ ๊ฐ์ง |
onMouseEntered | ๋ง์ฐ์ค๊ฐ ์ปจํธ๋กค์ ๋ค์ด์์ ๋ |
onMouseExited | ๋ง์ฐ์ค๊ฐ ์ปจํธ๋กค์ ๋ ๋ ๋ |
FXML์์ Controller ์ง์ ํ๋ ๋ฐฉ๋ฒ
FXML์์ ํน์ Java ํด๋์ค๊ฐ ์ปจํธ๋กค๋ฌ ์ญํ ์ ํ๋๋ก ์ง์ ํ๋ ค๋ฉด fx:controller ์์ฑ์ ์ฌ์ฉํฉ๋๋ค.
1.
๋ ์ด์์ ์์์ fx:controller ์์ฑ์ ์์ฑ
2.
fx:controller ์์ฑ ๊ฐ์ผ๋ก ์ปจํธ๋กค๋ฌ ํด๋์ค๋ฅผ ์ง์
<Layout fx:controller="application.MainController"></Layout>
Java
๋ณต์ฌ
FXML์์ Controller ์ง์
<?xml version="1.0" encoding="UTF-8"?>
<VBox xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="com.example.MyController">
<Label fx:id="label" text="Hello, JavaFX!"/>
<Button text="ํด๋ฆญ" onAction="#handleButtonClick"/>
</VBox>
XML
๋ณต์ฌ
fx:controller="com.example.MyController"
โ com.example.MyController ํด๋์ค๊ฐ ์ด FXML์ ์ปจํธ๋กค๋ฌ ์ญํ ์ ํจ.
Controller์์ ์ด๋ฒคํธ์ ๋ฉ์๋ ์ฐ๊ฒฐํ๋ ๋ฐฉ๋ฒ
FXML ํ์ผ์์ ํน์ ์ปจํธ๋กค๋ฌ๋ฅผ ์ง์ ํ ๋ค, ์ปจํธ๋กค ์์์ ์ด๋ฒคํธ ์์ฑ์ ๋ฉ์๋๋ฅผ #method ํํ๋ก ์ง์ ํ๊ณ , @FXML ์ด๋
ธํ
์ด์
์ ์ฐ๊ฒฐ๋ ๋ฉ์๋์ ๋ถ์ฌ์ค๋ค.
1.
FXML ๋ ์ด์์ ์์์ ์ปจํธ๋กค๋ฌ ์ง์
2.
ํน์ ์ปจํธ๋กค ์์์ ์ด๋ฒคํธ ์์ฑ์ #method ํํ๋ก ๋ฉ์๋๋ช
์ง์
3.
๋ฉ์๋์ @FXML ์ด๋
ธํ
์ด์
์ง์
<Layout fx:controller="application.MainController"></Layout>
Java
๋ณต์ฌ
<Button onAction="#clickButton">๋ฒํผ</Button>
Java
๋ณต์ฌ
@FXML
public void clickButton(ActionEvent event) throws IOException {
System.out.println("๋ฒํผ ํด๋ฆญ!");
}
Java
๋ณต์ฌ
@FXML์ ์ฌ์ฉํ์ฌ FXML์ UI ์์๋ฅผ Java ๊ฐ์ฒด ์ ์ธ
package com.example;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
public class MyController {
@FXML private Label label; // FXML์์ fx:id="label"์ธ ์์ ๊ฐ์ ธ์ค๊ธฐ
@FXML private Button button; // ๋ฒํผ ๊ฐ์ ธ์ค๊ธฐ
@FXML
private void handleButtonClick() { // ๋ฒํผ ํด๋ฆญ ์ด๋ฒคํธ ํธ๋ค๋ฌ
label.setText("๋ฒํผ์ด ํด๋ฆญ๋์์ต๋๋ค!");
}
}
Java
๋ณต์ฌ
์์ ์ฝ๋
(1) ๋ก๊ทธ์ธ ํ๋ฉด (login.fxml + LoginController.java)
[FXML - login.fxml]
<?xml version="1.0" encoding="UTF-8"?>
<VBox xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="com.example.LoginController" alignment="CENTER" spacing="10">
<Label text="๋ก๊ทธ์ธ"/>
<TextField fx:id="usernameField" promptText="์์ด๋ ์
๋ ฅ"/>
<PasswordField fx:id="passwordField" promptText="๋น๋ฐ๋ฒํธ ์
๋ ฅ"/>
<Button text="๋ก๊ทธ์ธ" onAction="#handleLogin"/>
<Label fx:id="messageLabel" text="" style="-fx-text-fill: red;"/>
</VBox>
XML
๋ณต์ฌ
[Controller - LoginController.java]
package com.example;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
public class LoginController {
@FXML private TextField usernameField;
@FXML private PasswordField passwordField;
@FXML private Label messageLabel;
@FXML
private void handleLogin() {
String username = usernameField.getText();
String password = passwordField.getText();
if ("admin".equals(username) && "1234".equals(password)) {
messageLabel.setText("๋ก๊ทธ์ธ ์ฑ๊ณต!");
messageLabel.setStyle("-fx-text-fill: green;");
} else {
messageLabel.setText("์์ด๋ ๋๋ ๋น๋ฐ๋ฒํธ๊ฐ ํ๋ ธ์ต๋๋ค.");
}
}
}
Java
๋ณต์ฌ
(2) ๊ฒ์ํ ๋ชฉ๋ก (board.fxml + BoardController.java)
[FXML - board.fxml]
<?xml version="1.0" encoding="UTF-8"?>
<BorderPane xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="com.example.BoardController">
<top>
<Label text="๊ฒ์ํ ๋ชฉ๋ก" style="-fx-font-size: 16px;"/>
</top>
<center>
<TableView fx:id="boardTable">
<columns>
<TableColumn text="๋ฒํธ" fx:id="colId"/>
<TableColumn text="์ ๋ชฉ" fx:id="colTitle"/>
<TableColumn text="์์ฑ์" fx:id="colAuthor"/>
<TableColumn text="๋ ์ง" fx:id="colDate"/>
</columns>
</TableView>
</center>
<bottom>
<HBox alignment="CENTER" spacing="10">
<Button text="์ ๊ธ ์์ฑ" onAction="#handleNewPost"/>
</HBox>
</bottom>
</BorderPane>
XML
๋ณต์ฌ
[Controller - BoardController.java]
package com.example;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.Button;
import javafx.scene.control.cell.PropertyValueFactory;
public class BoardController {
@FXML private TableView<Post> boardTable;
@FXML private TableColumn<Post, Integer> colId;
@FXML private TableColumn<Post, String> colTitle;
@FXML private TableColumn<Post, String> colAuthor;
@FXML private TableColumn<Post, String> colDate;
@FXML private Button newPostButton;
private final ObservableList<Post> posts = FXCollections.observableArrayList();
@FXML
private void initialize() {
colId.setCellValueFactory(new PropertyValueFactory<>("id"));
colTitle.setCellValueFactory(new PropertyValueFactory<>("title"));
colAuthor.setCellValueFactory(new PropertyValueFactory<>("author"));
colDate.setCellValueFactory(new PropertyValueFactory<>("date"));
boardTable.setItems(posts);
}
@FXML
private void handleNewPost() {
posts.add(new Post(posts.size() + 1, "์๋ก์ด ๊ฒ์๊ธ", "๊ด๋ฆฌ์", "2025-04-03"));
}
}
Java
๋ณต์ฌ
[Post.java (๋ฐ์ดํฐ ๋ชจ๋ธ ํด๋์ค)]
package com.example;
public class Post {
private final int id;
private final String title;
private final String author;
private final String date;
public Post(int id, String title, String author, String date) {
this.id = id;
this.title = title;
this.author = author;
this.date = date;
}
public int getId() { return id; }
public String getTitle() { return title; }
public String getAuthor() { return author; }
public String getDate() { return date; }
}
Java
๋ณต์ฌ
์ ๋ฆฌ
โข
FXML์์ fx:controller ์์ฑ์ผ๋ก Java ์ปจํธ๋กค๋ฌ๋ฅผ ์ฐ๊ฒฐ.
โข
@FXML์ ์ด์ฉํด UI ์์๋ฅผ Java ์ฝ๋์์ ์ ์ด.
โข
๋ฒํผ ํด๋ฆญ ๋ฑ์ ์ด๋ฒคํธ๋ฅผ Java ๋ฉ์๋(onAction="#methodName")์ ์ฐ๊ฒฐ.
โข
initialize() ๋ฉ์๋๋ฅผ ์ฌ์ฉํด ์ด๊ธฐ UI ์ค์ .
์ด์ JavaFX์ MVC ๊ตฌ์กฐ์์ FXML๊ณผ ์ปจํธ๋กค๋ฌ๋ฅผ ํ์ฉํ์ฌ UI์ ๋ก์ง์ ๋ถ๋ฆฌํ ์ ์์ต๋๋ค! 