Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

TableView with dynamic and updatable Columns

LaureanoFPJan 30 2015 — edited Feb 5 2015

Hi!!

Im trying to code a TableView with dynamic columns and I saw a lot of examples like this one: Creating columns dynamically

But any of those would work for my needs.

Its very simple:

I got a List of Customers, and each one has a List of Buys.

A Buy has a String "buyDetail" and a Date for the ship.

My TableView need to have the first column with the name of the Customer, and one column more for every day of existings ships.

We dont know previously wich days will be used.

If the amount of money is superior of 100, for example, I need to be able of applying different styles.

Example:

Customer2015/01/022015/01/032015/01/09
Morgan$400 (buyDetail)0$100
Luis00$20
Steven$1000
Hulk0$5$32

I cant use the Properties because i dont know how many Buys will have each Customer.

My best try (only for the first column) was this, but I cant get the Buy updated if I edit the value in the cell:

I didnt try to write the others columns code because I feel that im doing it really wrong..

This Shows the Customer´s names, but I cant handle if that data is edited.

table = new TableView<Customer>();

ObservableList<Customer> lista = FXCollections.observableList(registros);

table.setItems(lista);

TableColumn<Customer, Customer> customerNameColumn = new TableColumn<Customer, Customer>("");

  customerNameColumn.setCellValueFactory(new Callback<CellDataFeatures<Customer, Customer>, ObservableValue<Customer>>() {

  public ObservableValue<Customer> call(CellDataFeatures<Customer, Customer> p) {

  return new SimpleObjectProperty(p.getValue());

  }

  });

  customerNameColumn.setCellFactory(column -> {return new TableCell<Customer, Customer>() {

  @Override

  protected void updateItem(Customer item, boolean empty) {

  super.updateItem(item, empty);

  if (item == null || empty) {

  } else {

  setText(item.getName());

  //APPLY STYLE

  }

  }

  };

  });

  table.getColumns().addAll(customerNameColumn);

Message was edited by: user13425433

This post has been answered by bouye-JavaNet on Feb 4 2015
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 5 2015
Added on Jan 30 2015
3 comments
2,664 views