이번에는 BorderPane을 그대로 사용합니다.


BorderPane root = (BorderPane)FXMLLoader.load(getClass().getResource("main.fxml"));


primaryStage.setTitle("주소록 작성");


실행 시 창 크기의 가로를 조금 늘려주겠습니다.


primaryStage.setWidth(615);





BorderPane을 지우지 않고 그대로 사용하겠습니다.


Library에 Pane을 검색하여 좌측 아래의 Hierarchy Border의 Top부분에 끌어다 놓고 안에 TextField 4가지, Button 2가지


TableView는 Center에 끌어다가 배치합니다.


TableColumn은 아래와 같이 추가합니다.




컬럼 이름에 오른쪽 클릭해서 Copy & Paste, Duplicate 둘 중 마음에 드는걸 사용.





Pane : Layout : Pref Width : 600, Pref Wifth : 80


TextField : Properties : Prompt Text - 이름


   Code : fx:id - nameField


TextField : Properties : Prompt Text - 주소


   Code : fx:id - addrField


TextField : Properties : Prompt Text - 폰번호


   Code : fx:id - phoneField


TextField : Properties : Prompt Text - 이메일


   Code : fx:id - mailField


Button : Properties : Prompt Text - 등록


Code : fx:id - regist


Button : Properties : Prompt Text - 초기화


Code : fx:id - init


TableView : Code : fx:id - table


TableColumn : Properties : Prompt Text - Name


   Code : fx:id - name


TableColumn : Properties : Prompt Text - Address


   Code : fx:id - address


TableColumn : Properties : Prompt Text - Phone


   Code : fx:id - phone


TableColumn : Properties : Prompt Text - E-mail


   Code : fx:id - email



package app;

import java.net.URL;
import java.util.ResourceBundle;

import app.model.TableRowModel;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;

public class MainController implements Initializable {
	// 필드
	@FXML private TextField nameField;
	@FXML private TextField addrField;
	@FXML private TextField phoneField;
	@FXML private TextField mailField;

	// 테이블&컬럼
	@FXML private TableView<TableRowModel> table;
	@FXML private TableColumn<TableRowModel, String> name;
	@FXML private TableColumn<TableRowModel, String> address;
	@FXML private TableColumn<TableRowModel, String> phone;
	@FXML private TableColumn<TableRowModel, String> email;


	ObservableList<TableRowModel> list = null;
	@Override
	public void initialize(URL location, ResourceBundle resources) {
		System.out.println("FXML Load Complete");
		list = FXCollections.observableArrayList();
		// Model 객체가 list에 추가
		// lee, seoul, 010-1234-5678, email
		list.add(new TableRowModel("lee", "seoul", "no phone", "email"));

		// 각 필드를 CellData 인터페이스에
		name.setCellValueFactory(cellData -> cellData.getValue().getName());
		address.setCellValueFactory(cellData -> cellData.getValue().getAddress());
		phone.setCellValueFactory(cellData -> cellData.getValue().getPhone());
		email.setCellValueFactory(cellData -> cellData.getValue().getEmail());

		table.setItems(list);
	}
	public void regist() {
		list.add(new TableRowModel(nameField.getText(), addrField.getText(), phoneField.getText(), mailField.getText()));
		name.setCellValueFactory(cellData -> cellData.getValue().getName());
		address.setCellValueFactory(cellData -> cellData.getValue().getAddress());
		phone.setCellValueFactory(cellData -> cellData.getValue().getPhone());
		email.setCellValueFactory(cellData -> cellData.getValue().getEmail());
		// setItems는 자동으로 감지됨

		// 사용자 편의성을 위해 초기화
		nameField.setText("");
		addrField.setText("");
		phoneField.setText("");
		mailField.setText("");
		
		// 리스트의 모든 값 출력 하기
		// TableRowModel을 새로 list를 넣어 향상된 for문으로 출력
		for(TableRowModel model : list) {
			System.out.println(
					model.getName().get()+" | "+
					model.getAddress().get()+" | "+
					model.getPhone().get()+" | "+
					model.getEmail().get()
			);
		}
	}
	public void init() {
		list.clear();
		// 리스트를 직접 핸들링 한 경우는 이 부분이 필요
		table.setItems(list);
	}

}
package app.model;

import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;

public class TableRowModel {
	private StringProperty name;
	private StringProperty address;
	private StringProperty phone;
	private StringProperty email;
	// 생성자 (Alt + Shift + S 로 쉽게 등록(Constructor))
	public TableRowModel(String name, String address, String phone, String email) {
		this.name = new SimpleStringProperty(name);
		this.address = new SimpleStringProperty(address);
		this.phone = new SimpleStringProperty(phone);
		this.email = new SimpleStringProperty(email);
	}
	// 게터세터 (Alt + Shift + S 로 쉽게 등록)
	public StringProperty getName() {
		return name;
	}
	public void setName(StringProperty name) {
		this.name = name;
	}
	public StringProperty getAddress() {
		return address;
	}
	public void setAddress(StringProperty address) {
		this.address = address;
	}
	public StringProperty getPhone() {
		return phone;
	}
	public void setPhone(StringProperty phone) {
		this.phone = phone;
	}
	public StringProperty getEmail() {
		return email;
	}
	public void setEmail(StringProperty email) {
		this.email = email;
	}
	
}





+ Recent posts