I have little problem with inner methods in Java. In line:
dReservation[i].dispose();
I have an error:
Cannot refer to a non-final variable dReservation inside an inner class defined in a different method
I have read many threads in forum, but there are two solutions of that problem which didn't works:
Cannot refer to a non-final variable inside an inner class defined in a different method
Cannot refer to a non-final variable i inside an inner class defined in a different method
I have tried to set JDialog[] dReservation
as global
field for my class (GUIShowReservations
). Then my error disappears, but in the inner method (actionPerformed
) instead of dReservation[i]
is null
.
Just the same history is when I set JDialog[] dReservation
as final
field. It is null
.
bShowReservations = new JButton("Show Reservations");
bShowReservations.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
JDialog[] dReservation = new JDialog[10000];
for(Object o: reservations)
{
rez = (Reservations)o;
reservation.append(rez.getGroup());
dReservation[i] = new JDialog();
dReservation[i].setSize(400, 300);
dReservation[i].setLocationRelativeTo(null);
dReservation[i].setVisible(false);
dReservation[i].setLayout( null );
dReservation[i].setTitle("Edition");
bEditAccept = new JButton("Edit");
bEditAccept.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
rez.setTeacher(cEditTeacher.getSelectedItem().toString());
dao.update(rez);
dReservation[i].dispose();
}
});
bEditAccept.setSize(160, 24);
bEditAccept.setLocation(10, 200);
dReservation[i].add(bEditAccept);
}
}
});
Could you help me? I want to see a proper JDialog
in my inner method instead of null
.