Search in sources :

Example 1 with Change

use of javafx.collections.ListChangeListener.Change in project jOOQ by jOOQ.

the class BarChartSample method start.

@Override
public void start(Stage stage) {
    stage.setTitle("Country statistics with jOOQ and JavaFX");
    Label error = new Label("");
    error.setPadding(new Insets(10.0));
    // Create two charts, plotting a specific metric, each.
    BarChart<String, Number> chart1 = chart(COUNTRIES.GDP_PER_CAPITA, "GDP per Capita", "USD");
    BarChart<String, Number> chart2 = chart(COUNTRIES.GOVT_DEBT, "Government Debt", "% of GDP");
    TableView<CountriesRecord> table = table(COUNTRIES);
    Runnable refresh = () -> {
        table.getItems().clear();
        table.getItems().addAll(ctx().fetch(COUNTRIES).sortAsc(COUNTRIES.YEAR).sortAsc(COUNTRIES.CODE));
        chartRefresh(chart1);
        chartRefresh(chart2);
        error.setText("");
        selected = ctx().newRecord(COUNTRIES);
    };
    refresh.run();
    table.getSelectionModel().getSelectedItems().addListener((Change<? extends CountriesRecord> c) -> {
        if (c.getList().isEmpty())
            selected = ctx().newRecord(COUNTRIES);
        else
            for (CountriesRecord record : c.getList()) selected = record;
    });
    GridPane editPane = new GridPane();
    int i = 0;
    for (Field<?> field : COUNTRIES.fields()) {
        Label label = new Label(field.getName());
        TextField textField = new TextField();
        textField.textProperty().addListener((o, oldV, newV) -> {
            selected.set((Field) field, newV);
        });
        table.getSelectionModel().getSelectedItems().addListener((Change<? extends CountriesRecord> c) -> {
            if (c.getList().isEmpty())
                textField.setText("");
            else
                for (CountriesRecord record : c.getList()) textField.setText(record.get(field, String.class));
        });
        editPane.addRow(i++, label, textField);
    }
    Button saveButton = new Button("Save");
    saveButton.setOnAction(event -> {
        try {
            if (selected.store() > 0)
                refresh.run();
        } catch (DataAccessException e) {
            e.printStackTrace();
            error.setText(e.sqlStateClass() + ": " + e.getCause().getMessage());
        }
    });
    Button deleteButton = new Button("Delete");
    deleteButton.setOnAction(event -> {
        try {
            if (selected.delete() > 0)
                refresh.run();
        } catch (DataAccessException e) {
            e.printStackTrace();
            error.setText(e.sqlStateClass() + ": " + e.getCause().getMessage());
        }
    });
    GridPane buttonPane = new GridPane();
    buttonPane.addRow(0, saveButton, deleteButton);
    editPane.addRow(i++, new Label(""), buttonPane);
    GridPane chartPane = new GridPane();
    chartPane.addColumn(0, chart1, chart2);
    grow(chartPane);
    GridPane display = new GridPane();
    display.addRow(0, chartPane, table, editPane);
    grow(display);
    GridPane displayAndStatus = new GridPane();
    displayAndStatus.addColumn(0, display, error);
    displayAndStatus.setGridLinesVisible(true);
    grow(displayAndStatus);
    Scene scene = new Scene(displayAndStatus, 1000, 800);
    stage.setScene(scene);
    stage.show();
}
Also used : Insets(javafx.geometry.Insets) GridPane(javafx.scene.layout.GridPane) CountriesRecord(org.jooq.generated.tables.records.CountriesRecord) Label(javafx.scene.control.Label) Change(javafx.collections.ListChangeListener.Change) Scene(javafx.scene.Scene) Button(javafx.scene.control.Button) TextField(javafx.scene.control.TextField) DataAccessException(org.jooq.exception.DataAccessException)

Aggregations

Change (javafx.collections.ListChangeListener.Change)1 Insets (javafx.geometry.Insets)1 Scene (javafx.scene.Scene)1 Button (javafx.scene.control.Button)1 Label (javafx.scene.control.Label)1 TextField (javafx.scene.control.TextField)1 GridPane (javafx.scene.layout.GridPane)1 DataAccessException (org.jooq.exception.DataAccessException)1 CountriesRecord (org.jooq.generated.tables.records.CountriesRecord)1