Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java FX 8 - Tableview Display Objects Within Objects

Tags:

java

javafx-8

I'm trying to load a Tableview component from a list of Objects, in my case client objects. A client object contains an Address Object and Vice Versa. The Client and Address object are defined as follows;

public class Client{

   private String firstName;
   private String lastName;
   private String emailAddress;
   private String phoneNumber;

   private Address clientsAddress;

   public Client(){
     //blah blah
   }

   //getters
   ...
   //setters
   ...
}

public class Address{

   //address lines + county
   private String a1;
   private String a2;
   private String a3;
   private String county;

   private Client client;

   public Address(){
     //blah blah
   }

   //getters
   ...
   //setters
   ...
}

I can get the Client's fields to appear in the table view mapping the field name inside the client Object to the Column.

final TableView<Client> tableView = new TableView<>();
final TableColumn firstNameCol = new TableColumn("First Name");
final TableColumn lastNameCol = new TableColumn("Last Name");
final TableColumn emailCol = new TableColumn("Email Address");
final TableColumn addressCol1 = new TableColumn("Address Line 1");
final TableColumn addressCol2 = new TableColumn("Address Line 2");

firstNameCol.setMinWidth(90);
firstNameCol.setMaxWidth(110);
firstNameCol.setCellValueFactory(new PropertyValueFactory<>("firstName"));

lastNameCol.setMinWidth(90);
lastNameCol.setMaxWidth(110);
lastNameCol.setCellValueFactory(new PropertyValueFactory<>("lastName"));

emailCol.setMinWidth(100);
emailCol.setMaxWidth(210);
emailCol.setCellValueFactory(new PropertyValueFactory<>("emailAddress"));

//Don't know how to access the Address Object inside Client Object in Next two parts

addressColumn1.setMinWidth(100);
addressColumn1.setMaxWidth(200);
addressColumn1.setCellValueFactory(new PropertyValueFactory<>(""));

adddessColumn2.setMinWidth(100);
addressColumn2.setMaxWidth(200);
addressColumn2.setCellValueFactory(new PropertyValueFactory<>(""));

tableView.getColumns().addAll(firstNameCol, lastNameCol, emailCol, addressColumn1, addressColumn2);

My problem is however, is there any way of accessing the address object inside the client object, or any way of mapping the field name in the Address object to the table column. Any help would be appreciated, Thanks.

like image 774
PalinDrome555 Avatar asked Apr 24 '26 00:04

PalinDrome555


1 Answers

Thanks for the tip, I got it working by overriding the call method which is able to access the client object. Then I was able to access the Address Object and return a SimpleStringProperty and update each column appropriately.

addressColumn1.setCellValueFactory(new Callback<CellDataFeatures<Client, String>, ObservableValue<String>>() {
        @Override
        public ObservableValue<String> call(CellDataFeatures<Client, String> c) {
            return new SimpleStringProperty(c.getValue().getAddress().getA1());                
        }
}); 
like image 119
PalinDrome555 Avatar answered Apr 26 '26 13:04

PalinDrome555



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!