Home > Back-end >  Bug error does not load the window with Java Swing
Bug error does not load the window with Java Swing

Time:12-07

First I put them in context, I'm working on a small project with Java Swing. Before the project was made with Java 8 and Ant and it worked fine. I decided to update the code to Java 17 and Maven, at this point the problems started (I'm with the Netbeans IDE)

The problem comes now, when you run the program it only shows a small window that doesn't even show one of the components that should appear

When running the program The view that should show is like this

introducir la descripción de la imagen aquí

In its place appears

introducir la descripción de la imagen aquí

When expanding the windows introducir la descripción de la imagen aquí

In the actual pom.xml file i have these dependencies

    <dependencies>
         <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.30</version>
        </dependency>

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-dbcp2</artifactId>
            <version>2.9.0</version>
        </dependency>
        
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-pool2</artifactId>
            <version>2.11.1</version>
        </dependency>
        <dependency>
            <groupId>commons-net</groupId>
            <artifactId>commons-net</artifactId>
            <version>3.8.0</version>
        </dependency>
        
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.12.0</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.24</version>
            <optional>true</optional>
        </dependency>

    </dependencies>

The project with Ant had these libraries

enter image description here

When the program starts, the layout constructor is executed, which calls these classes

public PrincipalLayout() {
        this.service = new ProductService();
        //La base de datos esta vacia
        this.model = new TableModel(service.selectAllProducts());
}

//constructor and parameters of ProductService
private final InMemoryProductRepository mysqlController;
private List<Product> inMemoryList;
public ProductService(){
        this.mysqlController = new InMemoryProductRepository();
        var optionalProducts = mysqlController.selectAll();
        
        if(optionalProducts.isPresent())
            optionalProducts.get().forEach(p-> this.inMemoryList.add( p));
        if(optionalProducts.isEmpty())
            this.inMemoryList.addAll(Collections.emptyList())
}

//the selectAll from the repository
@Override
public Optional<List<Product>> selectAll() {
        List<Product> products = Collections.emptyList();

        try {
            PreparedStatement preparedStatement = ConnectionJDBC
                    .getConnection().prepareStatement(SQL_SELECT.getCommand());
            ResultSet resultSet = preparedStatement.executeQuery();
     
            while (resultSet.next()) {
                var product = new Product(
                        resultSet.getInt("product_id"),
                        resultSet.getString("name"),
                        resultSet.getDouble("price")
                );
                products.add(product);
            }

            ConnectionJDBC.close(resultSet);
            ConnectionJDBC.close(preparedStatement);
            ConnectionJDBC.close(ConnectionJDBC.getConnection());
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, "Ocurrio un error al ejecutar SellectAll", "ERROR", 0);
        }
   
        return Optional.of(products);
}

I'm still editing the question to see if that way it's possible to find the place with the possible error, I'm still accepting suggestions in the code :)

CodePudding user response:

Well I finally solved the problem, what was happening was that the .form file generates a method called "initComponents" as you will see in the constructor above I was not calling that method... So adding it to the constructor solved it. it was nonsense.

The solution

public PrincipalLayout() {
        this.service = new ProductService();
        this.model = new TableModel(service.selectAllProducts());
        initComponents();
    }
  • Related