Last active
August 29, 2015 14:21
-
-
Save ebysofyan/a7b4f9d337c16a7bb20a to your computer and use it in GitHub Desktop.
JavaFX TableModel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* To change this license header, choose License Headers in Project Properties. | |
* To change this template file, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
package com.eby.book; | |
import com.eby.helper.ListConverter; | |
import com.eby.orm.entity.Book; | |
import com.eby.orm.entity.Category; | |
import java.util.ArrayList; | |
import java.util.Collection; | |
import java.util.Iterator; | |
import java.util.List; | |
import java.util.Set; | |
import javafx.collections.FXCollections; | |
import javafx.collections.ObservableList; | |
import javafx.scene.control.Label; | |
import javafx.scene.control.TableCell; | |
import javafx.scene.control.TableColumn; | |
import javafx.scene.control.cell.PropertyValueFactory; | |
import javafx.util.Callback; | |
/** | |
* | |
* @author 3b1 | |
*/ | |
public class BookTableModel { | |
private List<Book> list; | |
private ObservableList<Book> row; | |
private Collection<TableColumn<Book, String>> column; | |
public BookTableModel() { | |
list = new ArrayList<Book>(); | |
row = FXCollections.observableArrayList(list); | |
column = new ArrayList<TableColumn<Book, String>>(); | |
this.initColumn(); | |
} | |
private void initColumn() { | |
column.add(this.addTableColumn("ID", "id")); | |
column.add(this.addTableColumn("TITLE", "title")); | |
column.add(this.addTableColumn("SYNOPSIS", "synopsis")); | |
column.add(this.addTableColumn("PUBLISHER", "publisher")); | |
column.add(this.addTableColumn("AUTHOR", "author")); | |
column.add(this.getCategory()); | |
} | |
private TableColumn addTableColumn(String tableHeader, String entityAttribute) { | |
TableColumn t = new TableColumn(tableHeader); | |
t.setCellValueFactory(new PropertyValueFactory<Book, String>(entityAttribute)); | |
return t; | |
} | |
private TableColumn getCategory() { | |
TableColumn t = this.addTableColumn("CATEGORY", "categories"); | |
t.setCellFactory(new Callback< TableColumn<Book, Set>, TableCell<Book, Set>>() { | |
@Override | |
public TableCell<Book, Set> call(TableColumn<Book, Set> param) { | |
TableCell<Book, Set> bookcatCell = new TableCell<Book, Set>() { | |
protected void updateItem(Set set, boolean empty) { | |
if (set != null) { | |
String out = ""; | |
Iterator<Category> iter = set.iterator(); | |
while (iter.hasNext()) { | |
out = out + iter.next().getName() + ", "; | |
} | |
Label label = new Label(out); | |
setGraphic(label); | |
}else{ | |
setGraphic(null); | |
} | |
} | |
}; | |
return bookcatCell; | |
} | |
; | |
}); | |
return t; | |
} | |
public Collection<TableColumn<Book, String>> getAllColumn() { | |
return column; | |
} | |
public ObservableList<Book> getItem() { | |
return row; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment