1

I have a JFrame where I insert some informations, these informations I send to a object called "macro". When I hit a JButton "macro" is insert in a ArrayList, called "listaJFP". When I enter with the first informations like, name "Murilo", id "1", and hit the button, my ArrayList receives the correct information, but when I try to insert another name like "Joao", id "2", my ArrayList receives in the first index [0] Joao, 2, and second index[1] Joao, 2. Instead of [0]Murilo,1 and [1]Joao,2. I looked for this problem and I saw someone talking about the reference of the object, in other words, when I change the values of my object "macro" at the same time the values of my ArrayList are changed. Can someone help me, please ? Thanks for the attention !

This is in my class JFramePrincipal:

Macro macro = new Macro();

private List<Macro> listaJFP = new ArrayList<Macro>();

This is in my JButton actionPerformed:

listaJFP.add(macro);

JFrameTabela jfT = new JFrameTabela(listaJFP);

I will try to put more code:

public class JFramePrincipal extends javax.swing.JFrame {

private List<Macro> listaJFP = new ArrayList<Macro>();
Macro macro = new Macro();
String[] arrayNodeName;
String[] listaVelocidade = new String[]{"1024", "1984"};
String[] listaSlot = new String[]{"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13"};
String[] listaModule86x0 = new String[]{"0", "1"};
String[] listaModule8609 = new String[]{"3", "4"};
String[] listaPort = new String[]{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23"};
String[] listaPortFeGe = new String[]{"0", "1", "2", "3", "4", "5", "6", "7"};
String[] nodeType = new String[]{"8609", "8630", "8660"};

 private void jButtonGerarMacroActionPerformed(java.awt.event.ActionEvent evt) {                                                  
    try {
        if (jCheckBoxFSP.isSelected() == true) {
            macro.setVpnName(jFormattedTextFieldFSP.getValue().toString());
        } else if (jCheckBoxSP.isSelected() == true) {
            macro.setVpnName(jFormattedTextFieldSP.getValue().toString());
        }

        macro.velocidade = jComboBoxVelocidade.getSelectedItem().toString();

        if (jTextVLAN.isEnabled() == true) {
            int vlanInt;
            boolean ok = false;
            vlanInt = Integer.parseInt(jTextVLAN.getText());
            do {
                if (vlanInt >= 1 && vlanInt <= 4094) {
                    macro.vlan = jTextVLAN.getText();
                    gerar();
                    jButtonExecutarMacro.setEnabled(true);
                } else {
                    JOptionPane.showMessageDialog(null, "VLAN deve ser maior do que 0 e menor do que 4094", "Mensagem", JOptionPane.ERROR_MESSAGE);
                    jTextVLAN.grabFocus();
                    jButtonExecutarMacro.setEnabled(false);
                }
            } while (ok);
        } else {
            macro.vlan = null;
            gerar();
            jButtonExecutarMacro.setEnabled(true);
            jButtonGerarMacro.setEnabled(false);
        }


private void jButtonExibirResultadoActionPerformed(java.awt.event.ActionEvent evt) {                                                       
    if(jCheckBoxE1.isSelected() == true){
        listaJFP.add(macro);
        Macro macro = new Macro();
        JFrameTabela jfT = new JFrameTabela(listaJFP);

}

4
  • Seems like you are forgetting to copy the object at some point. But it is hard to tell where without the GUI code. If you want get answer faster please provide a SSCCE. Commented Oct 31, 2013 at 12:51
  • Did you make sure to create a new Macro for every input from GUI. It may be that you're adding the same macro everytime. Commented Oct 31, 2013 at 12:54
  • No. I am not creating a new Macro everytime. But my question is, where do I put this new Macro ? In the beginning of the class ? After listaJFP.add(macro); ? Where ? Commented Oct 31, 2013 at 12:56
  • Put it in your actionPerformed() Commented Oct 31, 2013 at 13:04

1 Answer 1

1

Did you make sure to create a new Macro for every input from GUI

You have to Create a new Macro like this

public void actionPerformed(ActionEvent e){
    Macro macro = new Macro();
    listaJFP.add(macro);
}
// so it create a totally new Macro object everytime

Edit: After OP edit with more code

You need to create to new Macro inside to the first ActionPerformed because that's where you're manipulating the data. And why do you have two different actionperformed for a similar task?

Sign up to request clarification or add additional context in comments.

20 Comments

When I put Macro macro = new Macro(); before the add, I insert in the list a null value.
Need to see more code. Hard to tell what you're trying to do.
If you put the add before the new Macro, where is the macro in add(macro) coming from. That's what makes no sense at all. A lot more relevant code is needed, in order for anyone to understand what's really going on. We can all guess but we may run into situations like this, where fixing one bug causes another bug.
You need to create to new Macro inside to the first ActionPerformed because that's where you're manipulating the data. And why do you have two different actionperformed for a similar task?
I do the Macro macro = new Macro(); at the beginning of the first actionPerformed ? or in the end ?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.