Home > Enterprise >  I can't add the value of an int of a JFrame class with the value of an int of another class &qu
I can't add the value of an int of a JFrame class with the value of an int of another class &qu

Time:05-09

here is what I have in the JFrame class

 private void btnLLamada1ActionPerformed(java.awt.event.ActionEvent evt) {                                            
    empresa = new Empresa();
    minutos = Integer.parseInt(JOptionPane.showInputDialog(null, "numero de minutos"));

    Object[] Lista = {"Local", "Internacional", "Celular"};
    String s = (String) JOptionPane.showInputDialog(
            null, "Seleccione tipo de llamada", "Linea 1", JOptionPane.PLAIN_MESSAGE, null, Lista, "Local");
    System.out.println(s);
    switch (s) {
        case "Local":
            empresa.agregarLlamadaLocalLinea1(minutos);
            break;
        case "Internacional":
            empresa.agregarLlamadaInternacionalLinea1(minutos);
            break;
        case "Celular":
            empresa.agregarLlamadaCelularLinea1(minutos);
            break;

    }
}

and here is what I have in the other class "Empresa" where I am trying to add the int

void agregarLlamadaLocalLinea1(int minutos) {
    minT = minutos   minT;
    System.out.println(minT); // doesn't add the int
}

so when I run the program, it shows in the console the value of "minutos", but it doesn't add it with "minT"

the value of minutos is assigned by the user in a JOptionPane, and the value of minT is initially 0, so the idea of ​​"minT" is that it is the total of the "minutos" value that will be added each time the value is entered with the JOptionPane

CodePudding user response:

Regarding,

"I only call new Empresa() once"

No, you call new Empresa() whenever the ActionListener code is called, and so each time you try to add minutos to the minT field, you're doing this on a new instance, and leaving the old previously created instance unchanged.

Again, you should create an Empresa instance once and only once, likely in a constructor, and not in a listener. If you want a more detailed answer, again, please create and post a valid mre.


For example, in the code below, which is derived from your code, I simplify the code by always trying to add 10 to the Empresa int property. I print out the Empresa instance's hashCode in the Empresa method invoked by the listener:

import java.awt.*;
import javax.swing.*;

@SuppressWarnings("serial")
public class TestEmpresa extends JPanel {
    private Empresa empresa;
    
    public TestEmpresa() {
        setPreferredSize(new Dimension(400, 300));
        
        JButton btnLLamada1 = new JButton("Llamada 1");
        btnLLamada1.addActionListener(e -> btnLLamada1ActionPerformed(e));
        
        add(btnLLamada1);
    }

    private void btnLLamada1ActionPerformed(java.awt.event.ActionEvent evt) {
        empresa = new Empresa();
        
        empresa.agregarLlamadaLocalLinea1(10);      
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            TestEmpresa mainPanel = new TestEmpresa();

            JFrame frame = new JFrame("GUI");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(mainPanel);
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        });
    }

}
class Empresa {
    private int minT = 0;

    void agregarLlamadaLocalLinea1(int minutos) {
        minT = minutos   minT;
        System.out.println(minT);
        
        System.out.println("Current Empresa instance's hashCode: "   this.hashCode());
    }
}

You can see by the output shown on the command-line that minT value printed out remains 10 and the displayed hashCode is different each time the action listener is called, meaning the Empresa instances are different each time:

10
Current Empresa instance's hashCode: 912404094
10
Current Empresa instance's hashCode: 1076883823
10
Current Empresa instance's hashCode: 36667117
10
Current Empresa instance's hashCode: 757584339

Instead, create one Empresa instance and update it:

import java.awt.*;
import javax.swing.*;

@SuppressWarnings("serial")
public class TestEmpresa extends JPanel {
    private Empresa empresa = new Empresa();  // !! DO THIS ****
    
    public TestEmpresa() {
        setPreferredSize(new Dimension(400, 300));
        
        JButton btnLLamada1 = new JButton("Llamada 1");
        btnLLamada1.addActionListener(e -> btnLLamada1ActionPerformed(e));
        
        add(btnLLamada1);
    }

    private void btnLLamada1ActionPerformed(java.awt.event.ActionEvent evt) {
        // empresa = new Empresa(); // !! NO don't do this ****
        
        empresa.agregarLlamadaLocalLinea1(10);      
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            TestEmpresa mainPanel = new TestEmpresa();

            JFrame frame = new JFrame("GUI");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(mainPanel);
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        });
    }

}

The output shows an increasing value of minT and the same hashCode each time, meaning that we are updating the same Empresa instance each time:

10
Current Empresa instance's hashCode: 619496914
20
Current Empresa instance's hashCode: 619496914
30
Current Empresa instance's hashCode: 619496914
40
Current Empresa instance's hashCode: 619496914
50
Current Empresa instance's hashCode: 619496914

  • Note also the use of minimal example code in this answer to help simplify the code and the problem. In the future, consider creating and posting with your question.

CodePudding user response:

What in particular is not working? The following code prints the minutes two times. The only issue is that after leaving the method the instance of Empresa is not accessible anymore.

import javax.swing.*;

public class Main {
    public static void main(String[] args) {                                            
        Empresa empresa = new Empresa();
        int minutos = Integer.parseInt(JOptionPane.showInputDialog(null, "numero de minutos"));

        Object[] Lista = {"Local", "Internacional", "Celular"};
        String s = (String) JOptionPane.showInputDialog(
                null, "Seleccione tipo de llamada", "Linea 1", JOptionPane.PLAIN_MESSAGE, null, Lista, "Local");
        System.out.println(s);
        switch (s) {
            case "Local":
                empresa.agregarLlamadaLocalLinea1(minutos);
                break;
            case "Internacional":
                // empresa.agregarLlamadaInternacionalLinea1(minutos);
                break;
            case "Celular":
                // empresa.agregarLlamadaCelularLinea1(minutos);
                break;

        }
        System.out.println(empresa.minT);
    }
}

class Empresa {
    public int minT = 0;
    void agregarLlamadaLocalLinea1(int minutos) {
        minT = minutos   minT;
        System.out.println(minT); // doesn't add the int
    }
}
  •  Tags:  
  • java
  • Related