๋ฐ์ดํฐ ์ ๋ฌ
ํ๋ฉด ๊ฐ์ ๋ฐ์ดํฐ๋ฅผ ์ ๋ฌํ๋ ๋ฐฉ๋ฒ์ ์์๋ด
๋๋ค.
๋ฐ์ดํฐ ์ ๋ฌ ๋ฐฉ๋ฒ
1.
FXML ์ปจํธ๋กค๋ฌ์ ๋ฉ์๋๋ฅผ ํตํ ๋ฐ์ดํฐ ์ ๋ฌ
2.
์ฑ๊ธํค ํจํด์ ์ด์ฉํ ์ ์ญ ๋ฐ์ดํฐ ๊ณต์
FXML ์ปจํธ๋กค๋ฌ์ ๋ฉ์๋๋ฅผ ํตํ ๋ฐ์ดํฐ ์ ๋ฌ
FXMLLoader๋ฅผ ์ฌ์ฉํ์ฌ ์ปจํธ๋กค๋ฌ๋ฅผ ๊ฐ์ ธ์ค๊ณ , ํด๋น ์ปจํธ๋กค๋ฌ์ ๋ฉ์๋๋ฅผ ํธ์ถํ์ฌ ๋ฐ์ดํฐ๋ฅผ ์ ๋ฌํ๋ ๋ฐฉ์
1. ๋ฐ์ดํฐ ์ ๋ฌ ๊ณผ์
1.
FXML ํ์ผ์ ๋ก๋ํ ๋ FXMLLoader๋ฅผ ์ฌ์ฉํ์ฌ ์ปจํธ๋กค๋ฌ ๊ฐ์ฒด๋ฅผ ๊ฐ์ ธ์จ๋ค.
2.
getController()๋ฅผ ์ด์ฉํ์ฌ ์ปจํธ๋กค๋ฌ๋ฅผ ๊ฐ์ ธ์จ๋ค.
3.
๋ฐ์ดํฐ๋ฅผ ์ ๋ฌํ ์ ์๋ ๋ฉ์๋๋ฅผ ํธ์ถํ๋ค.
4.
์๋ก์ด ํ๋ฉด์์ ๋ฉ์๋์ ๋งค๊ฐ๋ณ์๋ฅผ ํตํด ๋ฐ์ดํฐ๋ฅผ ์ ๋ฌ ๋ฐ๋๋ค.
a.
์ ๋ฌ๋ ๋ฐ์ดํฐ๋ฅผ ํ์ฉํ์ฌ UI๋ฅผ ์
๋ฐ์ดํธํ๋ค.
b.
์ ๋ฌ๋ฐ์ ๋ฐ์ดํฐ๋ฅผ ๋ณ์์ ์ง์ ํ๋ค.
2. ๋ฐ์ดํฐ ์ ๋ฌ ๊ณผ์ ์์ธ ์ค๋ช
FXML ํ์ผ๊ณผ ์ปจํธ๋กค๋ฌ ์ค๋น
โข
๋จผ์ Main.fxml๊ณผ Login.fxml์ ์ค๋นํ๋ค.
โข
๊ฐ๊ฐ์ ํ๋ฉด์ ๊ด๋ฆฌํ๋ ์ปจํธ๋กค๋ฌ(MainController์ LoginController)๋ฅผ ๋ง๋ ๋ค.
MainController์์ ๋ฐ์ดํฐ๋ฅผ ์ ๋ฌ
โข
์ฌ์ฉ์๊ฐ ๋ฒํผ์ ํด๋ฆญํ๋ฉด Login.fxml ํ๋ฉด์ผ๋ก ์ด๋ํ๋ฉด์ ๋ฐ์ดํฐ๋ฅผ ์ ๋ฌํ๋ค.
โข
FXMLLoader๋ฅผ ์ฌ์ฉํ์ฌ Login.fxml์ ๋ก๋ํ ํ getController()๋ฅผ ํตํด LoginController์ ์ธ์คํด์ค๋ฅผ ๊ฐ์ ธ์จ๋ค.
โข
setUserData() ๋ฉ์๋๋ฅผ ํธ์ถํ์ฌ ๋ฐ์ดํฐ๋ฅผ ์ ๋ฌํ๋ค.
LoginController์์ ๋ฐ์ดํฐ๋ฅผ ๋ฐ์ UI ์
๋ฐ์ดํธ
โข
์ ๋ฌ๋ฐ์ ๋ฐ์ดํฐ๋ฅผ initialize() ๋๋ ๋ณ๋์ ๋ฉ์๋์์ UI ์์(Label ๋ฑ)์ ๋ฐ์ํ๋ค.
3. ์ฝ๋ ์์
Main.fxml (๋ฉ์ธ ํ๋ฉด)
<VBox xmlns="http://javafx.com/javafx"
xmlns:fx="http://javafx.com/fxml"
fx:controller="application.MainController">
<Button text="๋ก๊ทธ์ธ ํ๋ฉด ์ด๋"
fx:id="loginButton"
onAction="#handleLogin"/>
</VBox>
XML
๋ณต์ฌ
Login.fxml (๋ก๊ทธ์ธ ํ๋ฉด)
<VBox xmlns="http://javafx.com/javafx"
xmlns:fx="http://javafx.com/fxml"
fx:controller="application.LoginController">
<Label fx:id="userLabel" text="์ฌ์ฉ์ ์ ๋ณด ์์"/>
</VBox>
XML
๋ณต์ฌ
MainController.java (๋ฐ์ดํฐ ์ ๋ฌ)
package application;
import java.io.IOException;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class MainController {
@FXML
private void handleLogin(ActionEvent event) throws IOException {
// FXML ๋ก๋
FXMLLoader loader = new FXMLLoader(getClass().getResource("Login.fxml"));
Parent root = loader.load();
// ์ปจํธ๋กค๋ฌ ๊ฐ์ ธ์ค๊ธฐ
LoginController loginController = loader.getController();
// ๋ฐ์ดํฐ ์ ๋ฌ (์: "admin" ์ฌ์ฉ์ ID ์ ๋ฌ)
loginController.setUserData("admin");
// ํ๋ฉด ์ ํ
Stage stage = (Stage) ((javafx.scene.Node) event.getSource()).getScene().getWindow();
stage.setScene(new Scene(root));
stage.show();
}
}
Java
๋ณต์ฌ
LoginController.java (๋ฐ์ดํฐ ๋ฐ๊ธฐ ๋ฐ UI ์
๋ฐ์ดํธ)
package application;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
public class LoginController {
@FXML
private Label userLabel;
// ๋ฐ์ดํฐ ์ ๋ฌ์ ์ํ ๋ฉ์๋
public void setUserData(String userId) {
userLabel.setText("ํ์ํฉ๋๋ค, " + userId + "๋!");
}
}
Java
๋ณต์ฌ
4. ๊ณผ์ ์ ๋ฆฌ
์ฑ๊ธํค ํจํด์ ์ด์ฉํ ์ ์ญ ๋ฐ์ดํฐ ๊ณต์
์ฑ๊ธํค ํด๋์ค๋ฅผ ๋ง๋ค์ด ์ฌ๋ฌ ์ปจํธ๋กค๋ฌ์์ ๋ฐ์ดํฐ๋ฅผ ๊ณต์ ํ๋ ๋ฐฉ๋ฒ
1. ๊ฐ๋
โข
์ฑ๊ธํค ๊ฐ์ฒด๋ฅผ ๋ง๋ค์ด ์ ์ญ์ ์ผ๋ก ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํ๊ณ ๊ด๋ฆฌํ๋ค.
โข
์ฌ๋ฌ ํ๋ฉด์์ ๋ฐ์ดํฐ(์: ๋ก๊ทธ์ธ ์ ๋ณด, ์ฌ์ฉ์ ์ค์ ๋ฑ)๋ฅผ ๊ณต์ ํ ์ ์๋ค.
โข
ํน์ ๋ฐ์ดํฐ๋ฅผ ์ปจํธ๋กค๋ฌ ๊ฐ์ ์ฃผ๊ณ ๋ฐ์์ผ ํ ๋ ํธ๋ฆฌํ๋ค.
2. ๋ฐ์ดํฐ ์ ์ฅ ๋ฐ ์ ๋ฌ ๊ณผ์
์ฑ๊ธํค ํด๋์ค ์์ฑ
โข
๋ฐ์ดํฐ๋ฅผ ์ ์ฅํ๋ ์ ์ญ ๊ฐ์ฒด ์ญํ ์ ํ๋ค.
โข
ํ์ํ ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํ๊ณ ๊ฐ์ ธ์ค๋ getter/setter ๋ฉ์๋๋ฅผ ์ ๊ณตํ๋ค.
MainController์์ ๋ฐ์ดํฐ ์ ์ฅ
โข
์ฌ์ฉ์๊ฐ ๋ก๊ทธ์ธํ๋ฉด DataStorage์ ์ฌ์ฉ์ ์ ๋ณด๋ฅผ ์ ์ฅํ๋ค.
LoginController์์ ๋ฐ์ดํฐ ๊ฐ์ ธ์ค๊ธฐ
โข
DataStorage์์ ์ ์ฅ๋ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ UI์ ๋ฐ์ํ๋ค.
3. ์ฝ๋ ์์
DataStorage.java (์ฑ๊ธํค ๋ฐ์ดํฐ ์ ์ฅ ํด๋์ค)
package application;
public class DataStorage {
private static DataStorage instance;
private String username;
private int userAge;
private DataStorage() {} // ์ธ๋ถ์์ ์์ฑ ๋ถ๊ฐ (private ์์ฑ์)
// ์ฑ๊ธํค ์ธ์คํด์ค ๊ฐ์ ธ์ค๊ธฐ
public static DataStorage getInstance() {
if (instance == null) {
instance = new DataStorage();
}
return instance;
}
// ๋ฐ์ดํฐ ์ ์ฅ ๋ฉ์๋
public void setUserData(String username, int userAge) {
this.username = username;
this.userAge = userAge;
}
// ์ฌ์ฉ์ ์ด๋ฆ ๊ฐ์ ธ์ค๊ธฐ
public String getUsername() {
return username;
}
// ์ฌ์ฉ์ ๋์ด ๊ฐ์ ธ์ค๊ธฐ
public int getUserAge() {
return userAge;
}
// ๋ฐ์ดํฐ ์ด๊ธฐํ (๋ก๊ทธ์์ ์)
public void clearData() {
username = null;
userAge = 0;
}
}
Java
๋ณต์ฌ
MainController.java (๋ฐ์ดํฐ ์ ์ฅ ๋ฐ ํ๋ฉด ์ด๋)
package application;
import java.io.IOException;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class MainController {
@FXML
private void handleLogin(ActionEvent event) throws IOException {
// ์ฌ์ฉ์ ์ ๋ณด ์ ์ฅ
DataStorage.getInstance().setUserData("admin", 25);
// ๋ก๊ทธ์ธ ํ๋ฉด์ผ๋ก ์ด๋
FXMLLoader loader = new FXMLLoader(getClass().getResource("Login.fxml"));
Parent root = loader.load();
Stage stage = (Stage) ((javafx.scene.Node) event.getSource()).getScene().getWindow();
stage.setScene(new Scene(root));
stage.show();
}
}
Java
๋ณต์ฌ
LoginController.java (๋ฐ์ดํฐ ๊ฐ์ ธ์ค๊ธฐ ๋ฐ UI ์
๋ฐ์ดํธ)
package application;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
public class LoginController {
@FXML
private Label userLabel;
@FXML
public void initialize() {
// DataStorage์์ ์ฌ์ฉ์ ์ ๋ณด ๊ฐ์ ธ์ค๊ธฐ
String username = DataStorage.getInstance().getUsername();
int userAge = DataStorage.getInstance().getUserAge();
if (username != null) {
userLabel.setText("ํ์ํฉ๋๋ค, " + username + "๋! ๋์ด: " + userAge);
} else {
userLabel.setText("์ฌ์ฉ์ ์ ๋ณด ์์");
}
}
}
Java
๋ณต์ฌ
initialize()
JavaFX ์ปจํธ๋กค๋ฌ์์ ์๋์ผ๋ก ํธ์ถ๋๋ ์ด๊ธฐํ ๋ฉ์๋์
๋๋ค.