1

I am absolutely new to coding and am stuck with some "binding" code. I have a Record class with getters and setters, and a records Observable list which together populate a Tableview in a controller called RecordController. I want to populate another tableview on a separate AdminController file both with different view fxml's.

How can I bind the SimpleStringProperties in my Record.java class in such a manner that when they get updated by by the set method( by any UI user interaction) in RecordContoller ..the (editatble) Tableview in the AdminController gets populated with the new values.

Some bits of the code.....

public class Record {

private final SimpleStringProperty stationName;
private final SimpleStringProperty staffName;
private final SimpleStringProperty staffNumber;
private final SimpleStringProperty agencyName;
private final SimpleStringProperty aircraftType;
private final SimpleStringProperty issueDate;
private final SimpleStringProperty validTill;


public Record(String stationName, String staffName,
        String staffNumber, String agencyName, String aircraftType,
        String issueDate, String validTill) {
    this.stationName = new SimpleStringProperty(stationName);
    this.staffName = new SimpleStringProperty(staffName);
    this.staffNumber = new SimpleStringProperty(staffNumber);
    this.aircraftType = new SimpleStringProperty(aircraftType);
    this.agencyName = new SimpleStringProperty(agencyName);
    this.issueDate = new SimpleStringProperty(issueDate);
    this.validTill = new SimpleStringProperty(validTill);

}

public String getStationName() {

    return stationName.getValue();
}

public String getStaffName() {
    return staffName.getValue();
}

public String getStaffNumber() {
    return staffNumber.getValue();
}

public String getAgencyName() {
    return agencyName.getValue();
}

public String getAircraftType() {
    return aircraftType.getValue();
}

public String getIssueDate() {
    return issueDate.getValue();
}

public String getValidTill() {
    return validTill.getValue();
}

}

And the RecordController with the first Tableview.....

columnStation.setCellValueFactory(new PropertyValueFactory<>("stationName"));
    columnStaffName.setCellValueFactory(new PropertyValueFactory<>("staffName"));
    columnStaffNumber.setCellValueFactory(new PropertyValueFactory<>("staffNumber"));
    columnAgency.setCellValueFactory(new PropertyValueFactory<>("agencyName"));
    columnAircraftType.setCellValueFactory(new PropertyValueFactory<>("aircraftType"));
    columnIssueDate.setCellValueFactory(new PropertyValueFactory<>("issueDate"));
    columnValidTill.setCellValueFactory(new PropertyValueFactory<>("validTill"));


    recordTableView.setItems(null);
    recordTableView.setItems(records);

The records observable list being .....

private ObservableList<Record> records=FXCollections.observableArrayList();

The adminController for the other fxml also has another tableview which I want to populate in some way with the property variables in the Record.java (class / bean ? ).

2 Answers2

2

To recover from the -1 my question received...

In case a fellow newbie stumbles on this question. The answer lies in the following post.

Passing Parameters JavaFX FXML

Community
  • 1
  • 1
0

If you use the JavaFX properties pattern, then changes to the properties in your Record instances will be automatically reflected in the table view. So your Record class should look like

public class Record {

    private final StringProperty stationName;
    private final StringProperty staffName;
    private final StringProperty staffNumber;
    private final StringProperty agencyName;
    private final StringProperty aircraftType;
    private final StringProperty issueDate;
    private final StringProperty validTill;


    public Record(String stationName, String staffName,
            String staffNumber, String agencyName, String aircraftType,
            String issueDate, String validTill) {
        this.stationName = new SimpleStringProperty(stationName);
        this.staffName = new SimpleStringProperty(staffName);
        this.staffNumber = new SimpleStringProperty(staffNumber);
        this.aircraftType = new SimpleStringProperty(aircraftType);
        this.agencyName = new SimpleStringProperty(agencyName);
        this.issueDate = new SimpleStringProperty(issueDate);
        this.validTill = new SimpleStringProperty(validTill);

    }

    public StringProperty stationNameProperty() {
        return stationName ;
    }

    public final String getStationName() {
        return stationNameProperty().get();
    }

    public final void setStationName(String stationName) {
        stationNameProperty().set(stationName);
    }

    // similarly for other properties...

}

Note that many IDEs (e.g. Netbeans, or Eclipse with the E(fx)clipse plugin) will generate these methods for you.

Now, as long as you are changing the same Record objects that are in the table's items list, changes to those Records will automatically result in changes in the table: there is no need for additional binding.

James_D
  • 201,275
  • 16
  • 291
  • 322
  • Thanks James for your reply , sorry I should clarify a bit , The Tableview IN one fxml ( To view the records) is already being updated without any issue. I have a search form for the record on that view. The issue is , I have another fxml (admin view ) which has a table which I also want updated whenever the variables in record change. – supernova.surfer Dec 17 '16 at 15:27
  • @supernova.surfer So that's not very clear... If you can make it work once, you can presumably make it work again. You should probably update your question so that it contains a [MCVE] (reduce the table and record class to one or two columns, and include both fxml files, similarly minimized, and controllers) – James_D Dec 17 '16 at 15:31
  • Basically the record view ( via its controller) sets theRecord variables from some mySQL queries. The admin view ( via its controller) also has a tableview which I want updated with the same variables. I'm not sure how I can do that. – supernova.surfer Dec 17 '16 at 15:32
  • Ok James , I'll attempt to do as you suggested – supernova.surfer Dec 17 '16 at 15:40
  • Just a question to try and undestand...... if I have 2 Tableviews with 2 controller class files for each , how can I use the above "record" Observable list such that it can update both the Tableviews. – supernova.surfer Jan 01 '17 at 02:33
  • Just share the same `ObservableList` with both controllers – James_D Jan 01 '17 at 05:56
  • Thats the problem , I dont know how to implement that – supernova.surfer Jan 01 '17 at 11:25
  • I have tried various options and methods when I try to populate the second table I get null pointer exceptions, I have tried making the list public and returning it via a function, I have tried looking at binding – supernova.surfer Jan 01 '17 at 15:31