How to display different value with ComboBoxTableCell?
Posted
by
Philippe Jean
on Stack Overflow
See other posts from Stack Overflow
or by Philippe Jean
Published on 2012-11-01T16:20:46Z
Indexed on
2012/11/01
23:00 UTC
Read the original article
Hit count: 5189
I try to use ComboxBoxTableCell
without success.
The content of the cell display the right value for the attribute of an object. But when the combobox is displayed, all items are displayed with the toString
object method and not the attribute.
I tryed to override updateItem
of ComboBoxTableCell
or to provide a StringConverter
but nothing works.
Do you have some ideas to custom comboxbox list display in a table cell ?
I put a short example below to see quickly the problem. Execute the app and click in the cell, you will see the combobox with toString
value of the object.
package javafx2;
import javafx.application.Application;
import javafx.beans.property.adapter.JavaBeanObjectPropertyBuilder;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableColumn.CellDataFeatures;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.ComboBoxTableCell;
import javafx.stage.Stage;
import javafx.util.Callback;
import javafx.util.StringConverter;
public class ComboBoxTableCellTest extends Application {
public class Product {
private String name;
public Product(String name) {
this.name = name;
}
public String getName() { return name; }
public void setName(String name) { this.name = name; }
}
public class Command {
private Integer quantite;
private Product product;
public Command(Product product, Integer quantite) {
this.product = product;
this.quantite = quantite;
}
public Integer getQuantite() { return quantite; }
public void setQuantite(Integer quantite) { this.quantite = quantite; }
public Product getProduct() { return product; }
public void setProduct(Product product) { this.product = product; }
}
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
Product p1 = new Product("Product 1");
Product p2 = new Product("Product 2");
final ObservableList<Product> products = FXCollections.observableArrayList(p1, p2);
ObservableList<Command> commands = FXCollections.observableArrayList(new Command(p1, 20));
TableView<Command> tv = new TableView<Command>();
tv.setItems(commands);
TableColumn<Command, Product> tc = new TableColumn<Command, Product>("Product");
tc.setMinWidth(140);
tc.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<Command,Product>, ObservableValue<Product>>() {
@Override
public ObservableValue<Product> call(CellDataFeatures<Command, Product> cdf) {
try {
JavaBeanObjectPropertyBuilder<Product> jbdpb = JavaBeanObjectPropertyBuilder.create();
jbdpb.bean(cdf.getValue());
jbdpb.name("product");
return (ObservableValue) jbdpb.build();
} catch (NoSuchMethodException e) {
System.err.println(e.getMessage());
}
return null;
}
});
final StringConverter<Product> converter = new StringConverter<ComboBoxTableCellTest.Product>() {
@Override
public String toString(Product p) {
return p.getName();
}
@Override
public Product fromString(String s) {
// TODO Auto-generated method stub
return null;
}
};
tc.setCellFactory(new Callback<TableColumn<Command,Product>, TableCell<Command,Product>>() {
@Override
public TableCell<Command, Product> call(TableColumn<Command, Product> tc) {
return new ComboBoxTableCell<Command, Product>(converter, products) {
@Override
public void updateItem(Product product, boolean empty) {
super.updateItem(product, empty);
if (product != null) {
setText(product.getName());
}
}
};
}
});
tv.getColumns().add(tc);
tv.setEditable(true);
Scene scene = new Scene(tv, 140, 200);
stage.setScene(scene);
stage.show();
}
}
© Stack Overflow or respective owner