One of the major visual components embedded in JavaFX 2.0 (1.3 was in the preview) was the TableView.
It offers a great support in the presentation of data in table format, from a performance standard to a full customization of each cell.
Initially we must bear in mind that a table is made up of rows and columns and each intersection they form a cell.
By definition a TableView JavaFX has the data source for their formation ObservableList an object with a specific structure, which I call the commonly JavaFxBean. A JavaFxBean has a structure close to JavaBeans, but their attributes are descendants (extensions) interface javafx.beans.Property (StringProperty, IntegerProperty, BooleanProperty, OBJECTPROPERTY). According to the 2.0 API documentation it has this format:
public class Person {
private StringProperty firstName;
public void setFirstName(String value) { firstName.set(value); }
public String getFirstName() { return firstName.get(); }
public StringProperty firstNameProperty() {
if (firstName == null) firstName = new StringProperty();
return firstName;
}
private StringProperty lastName;
public void setLastName(String value) { lastName.set(value); }
public String getLastName() { return lastName.get(); }
public StringProperty lastNameProperty() {
if (lastName == null) lastName = new StringProperty();
return lastName;
}
}
In my example JPA TbTlmkChamada use a class that is recovered by the class TbTlmkChamadaJpaController. As the JPA only allows the creation of specific types of attributes (in which the descendants of Property do not fit) I created a class (Proxy) TbTlmkChamadaFxBean in order to receive the database objects. This class just has the same attributes as the original class, but with the descendants of javafx.beans.Property types.
Well have not touched the TableView. The time is now.
For a basic view, we can create a TableView standard with the following format:
TbTlmkChamadaJpaController control = new TbTlmkChamadaJpaControllerImpl(telemarketingserver.TelemarketingServer.emf);
tabela = new TableView();
List<TbTlmkChamada> findTbTlmkChamadaEntities = control.findTbTlmkChamadaEntities();
List<TbTlmkChamadaFxBean> lista = new ArrayList<TbTlmkChamadaFxBean>();
for (int i=0;i < TbTlmkChamadaEntities.size();i++) {
lista.add(new TbTlmkChamadaFxBean(findTbTlmkChamadaEntities.get(i)));
}
ObservableList<TbTlmkChamadaFxBean> chamadas = FXCollections.observableList(lista);
tabela.setItems(chamadas);
TableColumn<Integer> codigoCol = new TableColumn<Integer>("Código");
codigoCol.setProperty("codigo");
TableColumn<String> dadosCol = new TableColumn<String>("Dados");
dadosCol.setProperty("dados");
TableColumn<Date> dataCol = new TableColumn<Date>("Data");
dataCol.setProperty("proxChamada");
final TableColumn<TbTlmkFila> filaCol = new TableColumn<TbTlmkFila>("Fila");
filaCol.setProperty("fila");
tabela.getColumns().addAll(codigoCol,dadosCol,filaCol,dataCol);
tabela.setPrefWidth(800);
getChildren().add(tabela);
which will result in a table like this:
As you can see the columns were placed second class attributes TbTlmkChamadaFxBean (using their toString ()).
Now you can enhance the functioning of this table. Initially the ordering of the rows (in the example code for the column):
codigoCol.setComparator(new Comparator() {
@Override
public int compare(Integer o1, Integer o2) {
return o1.compareTo(o2);
}
});
With these lines of code we set the comparator used by the column codigoCol to compare the value of the objects.
You can also modify the visible content of each cell, as in the following code:
filaCol.setCellFactory(new Callback<TableColumn<TbTlmkFila>, TableCell<TbTlmkFila>>() {
@Override
public TableCell<TbTlmkFila> call(TableColumn<TbTlmkFila> param) {
TableCell<TbTlmkFila> cell = new TableCell<TbTlmkFila>() {
@Override public void updateItem(TbTlmkFila value, boolean empty) {
super.updateItem(value, empty);
if (empty) {
setNode(null);
} else {
VBox vbox = new VBox(5);
vbox.getChildren().add(new Label(value.getNome()));
vbox.getChildren().add(new Label(value.getCliente().getNome()));
vbox.setStyle("-fx-background-color:#FF0000;");
setNode(vbox);
}
}
};
return cell;
}
});
Using the method you can create setCellFactory CellFactory's customized for each column. What is not well documented in the JavaFX API 2.0 is how you access the value of the object related to the cell. This is done by writing on the method UpdateItem. Within this method is where you set what will be presented in each cell.
The result of this code is the following table:
CellFactory as the result of a column is an object of class Node, the imagination of developers is the limit of what can be presented in a cell: Region's, ListView's, Button's, other's TableView. The possibilities are endless.
I hope this post can be useful to someone as it was for me.
Good coding for all.
Nenhum comentário:
Postar um comentário