Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class BookViewController {
- private String dbUrl = "jdbc:postgresql://localhost:5432/BookingApp";
- private String dbUsername = "postgres";
- private String dbPassword = "secret";
- @FXML
- private TextField personnr;
- @FXML
- private TextField name;
- @FXML
- private TextField email;
- @FXML
- private TextField telnr;
- @FXML
- private Button addTraveler;
- @FXML
- private Button removeTraveler;
- @FXML
- private TableView<Traveler> travelers;
- @FXML
- private TableColumn<?, ?> personnrColumn;
- @FXML
- private TableColumn<?, ?> nameColumn;
- @FXML
- private TableColumn<?, ?> emailColumn;
- @FXML
- private TableColumn<?, ?> telnrColumn;
- @FXML
- private TableView<Trip> trips;
- @FXML
- private TableColumn<?, ?> originColumn;
- @FXML
- private TableColumn<?, ?> destinationColumn;
- @FXML
- private TableColumn<?, ?> departureColumn;
- @FXML
- private TableColumn<?, ?> arrivalColumn;
- @FXML
- private TableColumn<?, ?> driverColumn;
- @FXML
- private TableColumn<?, ?> priceAmountColumn;
- @FXML
- private TableColumn<?, ?> seatsColumn;
- @FXML
- private Button bookTrip;
- private Trip rowData;
- private ObservableList<Trip> tripData;
- protected void initialize(Trip rowData) {
- this.rowData = rowData;
- addTraveler.setDisable(true);
- removeTraveler.setDisable(true);
- populateTravelPlan();
- }
- @FXML
- private void populateTravelPlan() {
- originColumn.setCellValueFactory(new PropertyValueFactory<>("origin"));
- destinationColumn.setCellValueFactory(new PropertyValueFactory<>("destination"));
- departureColumn.setCellValueFactory(new PropertyValueFactory<>("departure"));
- arrivalColumn.setCellValueFactory(new PropertyValueFactory<>("arrival"));
- driverColumn.setCellValueFactory(new PropertyValueFactory<>("driver"));
- priceAmountColumn.setCellValueFactory(new PropertyValueFactory<>("priceAmount"));
- seatsColumn.setCellValueFactory(new PropertyValueFactory<>("seats"));
- try {
- Connection conn = DriverManager.getConnection(dbUrl, dbUsername, dbPassword);
- tripData = FXCollections.observableArrayList();
- ResultSet rs = null;
- if(rowData.getTrip1() != 0 && rowData.getTrip2() == 0 && rowData.getTrip3() == 0) {
- rs = conn.createStatement().executeQuery(
- "select trip.tripid, connexion.origin, connexion.destination, trip.departure, trip.arrival, trip.driverpnr, trip.priceamount, trip.seatsrn" +
- "from trip, connexionrn" +
- "where trip.tripid = " + rowData.getTrip1() + "rn" +
- "and trip.connexionid = connexion.connexionidrn" +
- "order by departure;");
- } else if(rowData.getTrip1() != 0 && rowData.getTrip2() != 0 && rowData.getTrip3() == 0) {
- rs = conn.createStatement().executeQuery(
- "select trip.tripid, connexion.origin, connexion.destination, trip.departure, trip.arrival, trip.driverpnr, trip.priceamount, trip.seatsrn" +
- "from trip, connexionrn" +
- "where trip.tripid = " + rowData.getTrip1() + "rn" +
- "and trip.connexionid = connexion.connexionidrn" +
- "unionrn" +
- "select trip.tripid, connexion.origin, connexion.destination, trip.departure, trip.arrival, trip.driverpnr, trip.priceamount, trip.seatsrn" +
- "from trip, connexionrn" +
- "where trip.tripid = " + rowData.getTrip2() + "rn" +
- "and trip.connexionid = connexion.connexionidrn" +
- "order by departure;");
- } else if(rowData.getTrip1() != 0 && rowData.getTrip2() != 0 && rowData.getTrip3() != 0) {
- rs = conn.createStatement().executeQuery(
- "select trip.tripid, connexion.origin, connexion.destination, trip.departure, trip.arrival, trip.driverpnr, trip.priceamount, trip.seatsrn" +
- "from trip, connexionrn" +
- "where trip.tripid = " + rowData.getTrip1() + "rn" +
- "and trip.connexionid = connexion.connexionidrn" +
- "unionrn" +
- "select trip.tripid, connexion.origin, connexion.destination, trip.departure, trip.arrival, trip.driverpnr, trip.priceamount, trip.seatsrn" +
- "from trip, connexionrn" +
- "where trip.tripid = " + rowData.getTrip2() + "rn" +
- "and trip.connexionid = connexion.connexionidrn" +
- "unionrn" +
- "select trip.tripid, connexion.origin, connexion.destination, trip.departure, trip.arrival, trip.driverpnr, trip.priceamount, trip.seatsrn" +
- "from trip, connexionrn" +
- "where trip.tripid = " + rowData.getTrip3() + "rn" +
- "and trip.connexionid = connexion.connexionidrn" +
- "order by departure;");
- }
- if(rs.next()) {
- tripData.add(new Trip(rs.getInt(1), rs.getString(2), rs.getString(3), ""+rs.getTimestamp(4), ""+rs.getTimestamp(5), rs.getString(6), ""+rs.getInt(7), ""+rs.getInt(8)));
- while(rs.next()) {
- tripData.add(new Trip(rs.getInt(1), rs.getString(2), rs.getString(3), ""+rs.getTimestamp(4), ""+rs.getTimestamp(5), rs.getString(6), ""+rs.getInt(7), ""+rs.getInt(8)));
- }
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- trips.setItems(tripData);
- }
- }
- driverColumn.setCellFactory(this::createDriverCell);
- private TableCell<Trip,String> createDriverCell(TableColumn<Trip, String> table) {
- return new TableCell<Trip, String>() {
- @Override
- protected void updateItem(String item, boolean empty) {
- super.updateItem(item, empty);
- if (empty) {
- // configure empty
- } else if (item == null) {
- // create no driver content
- } else {
- // create different content
- }
- }
- };
- }
- driverColumn.setCellFactory(param -> new TableCell<Trip,String>{
- public void updateItem(Trip t, boolean empty) {
- super.updateItem(t, empty);
- if(t.getDriver()!=null) {
- setText(t.getDriver());
- //revert graphic and contentDisplay, as cells may be reused
- setGraphic(null);
- setContentDisplay(ContentDisplay.TEXT_ONLY);
- } else {
- Button b = new Button("Choose Driver");
- //... hook up button actions, etc
- setGraphic(b);
- setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
- }
- }
- });
- @Override
- public void start(Stage primaryStage) {
- TableView<StringProperty> table = new TableView<>();
- // fill table
- for (int i = 0; i < 20; i++) {
- table.getItems().add(new SimpleStringProperty());
- }
- TableColumn<StringProperty, String> column = new TableColumn<>();
- column.setCellValueFactory(TableColumn.CellDataFeatures::getValue); // returns the row item itself
- column.setCellFactory(col -> new TableCell<StringProperty, String>() {
- private final Button button;
- {
- button = new Button("Set Value...");
- button.setOnAction(evt -> {
- TextInputDialog dialog = new TextInputDialog();
- Optional<String> opt = dialog.showAndWait();
- String result = opt.orElse(null);
- if (result != null) {
- ObservableValue<String> ov = getTableColumn().getCellObservableValue(getIndex());
- if (ov instanceof WritableValue) {
- ((WritableValue) ov).setValue(result);
- }
- }
- });
- }
- @Override
- protected void updateItem(String item, boolean empty) {
- super.updateItem(item, empty);
- if (empty) {
- // empty cell
- setText("");
- setGraphic(null);
- } else {
- if (item == null) {
- // editable cell
- setText("");
- setGraphic(button);
- } else {
- // uneditable cell
- setText(item);
- setGraphic(null);
- }
- }
- }
- });
- table.getColumns().add(column);
- Scene scene = new Scene(table);
- primaryStage.setScene(scene);
- primaryStage.show();
- }
Add Comment
Please, Sign In to add comment