We help IT Professionals succeed at work.

Adding multiple similar items to the file menu (MRU file list)

galneweinhaw
galneweinhaw asked
on
349 Views
Last Modified: 2008-02-01
I am adding items to my menu using:

fileMenu.add(AppActions.LOAD_MRU_FILE1);
fileMenu.add(AppActions.LOAD_MRU_FILE2);

public class AppActions {
...
    public static final Action LOAD_MRU_FILE1 = new DefaultClientAction() { .... };

    public static final Action LOAD_MRU_FILE2 = new DefaultClientAction() { .... };
}


/* Is there a way I can do this with just one Action, instead of having a different action for each file, and how could I then call fileMenu.add using a loop?

Thanks!
Comment
Watch Question

Author

Commented:
fileMenu.add(AppActions.LOAD_MRU_FILE1);

should be

fileMenu.add(new JMenuItem(AppActions.LOAD_MRU_FILE1));
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
you could but then you'd be adding same action multiple times to the menu and I doubt thats what u want

what u could do is create *seperate actions* using a loop and add them to your menu.

Author

Commented:
"create *seperate actions* using a loop"

ooh, that sounds good!  How would I do that with the format of Action declarations used above?
Java Developer
CERTIFIED EXPERT
Top Expert 2010
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT

Commented:

Author

Commented:
"static {"

what does this mean, just static without a variable or method?
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
thats a static block. It will get executed once the first time a class is loaded

Author

Commented:
"Cannot refer to a non-final variable i inside an inner class defined in a different method"

is the error I get if I try to use i inside the { .... };

which I think is the only way to make the actions different?

    static {
          MRUCampaignManager mruManager = new MRUCampaignManager();
          
          for (int i = 0; i < LOAD_MRU_FILE.length; i++ ) {
                LOAD_MRU_FILE[i] = new DefaultClientAction() {
                
                      public void execute(ActionEvent ae) {
                            
                            LoadFile(i);  // <--- error at the i
                      }                 

                };
                
          } //for
    } // static
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
you can't do that. you'd need to store i as a member variable in your inner class
(you would have that same problem with the code posted in the question)

Author

Commented:
1. Am I going about the right way of doing this?

2. if not, can you set me straight? or, if this is ok, can you show me how to:
" store i as a member variable in your inner class"

=)
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Thanks so much for all your help... I'll will continue in a new question if I need to since this one has been answered and then some!

Author

Commented:
contactkarthi, thanks for the link!  it would have been a perfect answer for a previous question I had... but this one I needed to deal with the file loading Actions involved.  Thanks again!
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.