Java/JavaFX 구현

Java FX // 구현 // TableView // Adapter (abstract class)

문스코딩 2018. 7. 27. 13:09

JAVA FX

TableView Adapter

업데이트 :: 2018.07.26



TableView adapter

TableView을 이용한 Dynamic Adapter Class (abstract)

public abstract class TableAdapter<T> {

    List<T> tableList = new ArrayList<>();
    TableView origin;
    ObservableList list;

    /* 생성자 */
    public TableAdapter(TableView origin) {
        this.origin = origin;
    }

    /* 초기화 */
    public void init(String[] colums) {
        list = FXCollections.observableArrayList();
        for (int i = 0; i < origin.getColumns().size(); i++) {
            TableColumn tc = (TableColumn) origin.getColumns().get(i);
            tc.setCellValueFactory(new PropertyValueFactory(colums[i]));
             tc.setStyle("-fx-alignment:CENTER;");
        }
        origin.setItems(list);
    }

    /* 모두제거 */
    public void clear() {
        tableList.clear();
        render();
    }

    /* 객체얻기 */
    public T get(int index) {
        if(index < 0 || index >= tableList.size()) return null;
        return tableList.get(index);
    }

    /* 객체추가 */
    public void add(T t) {
        tableList.add(t);
        render();
    }

    /* 객체수정 */
    public void set(int index, T t) {
        if(index < 0 || index >= tableList.size()) return;
        tableList.set(index, t);
        render();
    }

    /* 객체제거 */
    public void remove(int index) {
        if(index < 0 || index >= tableList.size()) return;
        tableList.remove(index);
        render();
    }

    /* 업데이트 */
    private void render() {
        list = FXCollections.observableArrayList(tableList);
        origin.setItems(list);
    }

}

Created by SDM

작성자 정보

e-mail :: jm921106@naver.com || jm921106@gmail.com

github :: https://github.com/moonscoding

반응형