Reputation: 4553
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);
}
Upvotes: 1
Views: 202
Reputation: 209004
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?
Upvotes: 1