anonymous inner classes and final variables

V

VisionSet

I know the reasons why method variables must be final in order they can be
accessed by an annoymous inner class, but take a trivial menu set up:

JMenuItem menuItem;
for(int m=1;m<=5;m++) {
menuItem = new JMenuItem(Integer.toString(m));
menuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
holder.setInt(Integer.parseInt(((JMenuItem)e.getSource()).getText()));
holder.setInt(m); // would be nice!!
}
});
menu.add(menuItem);
}

Obviously the int parseing is a total hack, but what would you do?
And no I really don't feel the need to subclass JMenuItem and provide an int
attribute!
 
C

Chris Smith

VisionSet said:
I know the reasons why method variables must be final in order they can be
accessed by an annoymous inner class, but take a trivial menu set up:

JMenuItem menuItem;
for(int m=1;m<=5;m++) {
menuItem = new JMenuItem(Integer.toString(m));
menuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
holder.setInt(Integer.parseInt(((JMenuItem)e.getSource()).getText()));
holder.setInt(m); // would be nice!!
}
});
menu.add(menuItem);
}

JMenuItem menuItem;

for (int m = 1; m <= 5; m++)
{
menuItem = new JMenuUtem(Integer.toString(m));
final int mCopy = m;

menuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
holder.setInt(mCopy);
}
});
}

Yeah, I know it's sorta ugly, but it works!

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
B

Bent C Dalager

I know the reasons why method variables must be final in order they can be
accessed by an annoymous inner class, but take a trivial menu set up:

JMenuItem menuItem;
for(int m=1;m<=5;m++) {
menuItem = new JMenuItem(Integer.toString(m));
menuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
holder.setInt(Integer.parseInt(((JMenuItem)e.getSource()).getText()));
holder.setInt(m); // would be nice!!
}
});
menu.add(menuItem);
}

Obviously the int parseing is a total hack, but what would you do?
And no I really don't feel the need to subclass JMenuItem and provide an int
attribute!

Dropping the holder in favour of printing, the following code compiles
and should do what you want.

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

public class Final
{
public static void main(String args)
{
JMenuItem menuItem;
for (int m = 1; m <= 5; m++)
{
final int number = m;
menuItem = new JMenuItem(Integer.toString(m));
menuItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.out.println(number);
}
});
}
}
}
 
B

Bent C Dalager

Yeah, I know it's sorta ugly, but it works!

That's not ugly. _This_ is ugly:

final int[] result = new int[1];
try
{
SwingUtilities.invokeAndWait(new Runnable()
{
public void run()
{
result[0] = askForModalUserInput();
}
});
}
catch (InterruptedException ex) {}
System.out.println("You said " + result[0]);

Cheers
Bent D
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top