File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes Help! Function returns bad value when inside .addActionListener Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Help! Function returns bad value when inside .addActionListener" Watch "Help! Function returns bad value when inside .addActionListener" New topic

Help! Function returns bad value when inside .addActionListener

Matt Senecal
Ranch Hand

Joined: Dec 01, 2000
Posts: 255
This is a piece of code that dynamically creates JButtons (jb) while reading data from a prefs file. The prefs file is opened, and the data objects (ButtonData is a custom class that contains information associated with each button) are read in. For each one (two total, in this test case) a JButton is dynamically created.
The problem is that just before jb.addActionListener(), bd.getObjectArray() returns two unique object arrays (as it should), one for each iteration. However, inside the .addActionListener(), bd.getObjectArray() returns the same object array for both iterations!
Does anyone know what might be causing this? I have a feeling the solution is simple, but I can't see it.

There's nothing more dangerous than a resourceful idiot. ---Dilbert
I agree. Here's the link:
subject: Help! Function returns bad value when inside .addActionListener
Similar Threads
help me!!
Blank JFrame caused by creation in an Inner Class
Empty Rows in a table
Custom Button Problem
JTextArea and saving to a file