Problem dynamically filling a VBox with HBoxs in JavaFX

44 Views Asked by At

I’m developing an application in JavaFX and I’ve encountered a problem when trying to dynamically fill a VBox with HBoxs. In my FXML file, created with Scene Builder, I have a VBox named “vboxSP_contenedor_anuncios”. This VBox is inside a ScrollPane, which in turn is inside another VBox.

My goal is that, when the program starts (specifically in the initialize method), the VBox “vboxSP_contenedor_anuncios” is filled with HBoxs. Each HBox would represent an ad with specific information (title, name, and email of the advertiser) that I get from a database. At this moment, I’m not interested in loading the image.

It's worth noting that I created the VBox in Scene Builder and I'm trying to add the HBoxs with code in the initialize method.

I expected that, when running the program, each HBox (ad) would be correctly displayed in the VBox. However, when I run the program, nothing is displayed in the VBox “vboxSP_contenedor_anuncios”.

Here is the complete code I am using in the initialize method to create the HBoxs and add them to the VBox:

// Connect to the database
Connection connection = getConnection();
if (connection != null) {
    // Execute the query
    String query = "SELECT Anunciantes.nombre, Anunciantes.gmail, Anuncios.titulo FROM Anunciantes "
            + "JOIN Publica ON Anunciantes.idAnunciante = Publica.idAnunciante "
            + "JOIN Anuncios ON Publica.idAnuncio = Anuncios.idAnuncio;";
    Statement st;
    ResultSet rs;

    try {
        st = connection.createStatement();
        rs = st.executeQuery(query);

        // Process the results
        while (rs.next()) {
            // Create an ad
            HBox anuncio = new HBox();
            anuncio.setSpacing(10);
            anuncio.setPadding(new Insets(10));
            anuncio.setStyle("-fx-border-color: black;");

            VBox vboxImagen = new VBox();
            vboxImagen.setMinSize(100, 100); // Set the minimum width and height
            vboxImagen.setStyle("-fx-border-color: black; -fx-background-color: white;");
            ImageView imagen = new ImageView(); // Here you would load your image
            vboxImagen.getChildren().add(imagen);

            VBox vboxTexto = new VBox();
            vboxTexto.setMinWidth(200); // Adjust the minimum width according to your needs
            vboxTexto.setStyle("-fx-border-color: black; -fx-background-color: white;");

            // Add the ad title
            Label titulo = new Label(rs.getString("titulo"));
            titulo.setStyle("-fx-text-fill: black;");
            vboxTexto.getChildren().add(titulo);

            // Add the advertiser's name
            Label nombre = new Label(rs.getString("nombre"));
            nombre.setStyle("-fx-text-fill: black;");
            vboxTexto.getChildren().add(nombre);

            // Add the advertiser's gmail
            Label gmail = new Label(rs.getString("gmail"));
            gmail.setStyle("-fx-text-fill: black;");
            vboxTexto.getChildren().add(gmail);

            // Create the VBox for the buttons
            VBox vboxBotones = new VBox();
            vboxBotones.setSpacing(10);
            vboxBotones.setMinSize(100, 100); // Set the minimum width and height
            vboxBotones.setStyle("-fx-border-color: black; -fx-background-color: white;");
            Button ver = new Button("Ver");
            ver.setStyle("-fx-background-color: green; -fx-text-fill: white;");
            Button editar = new Button("Editar");
            editar.setStyle("-fx-background-color: red; -fx-text-fill: white;");
            vboxBotones.getChildren().addAll(ver, editar);

            // Add the VBoxes to the HBox
            anuncio.getChildren().addAll(vboxImagen, vboxTexto, vboxBotones);

            // Add the ad to the container
            this.vboxSP_contenedor_anuncios.getChildren().add(anuncio);
        }
    } catch (SQLException e) {
        System.out.println(e.getMessage());
    }
}

I want to clarify that I have no problems connecting to the database or creating the HBoxs. I have verified that the database query is executing correctly and that the HBoxs are being created as expected.

Can anyone help me understand why the HBoxs are not being displayed in the VBox and how I can solve this problem?

0

There are 0 best solutions below