Results 1 to 8 of 8
  1. #1
    LianaN is offline Member
    Join Date
    Aug 2010
    Posts
    40
    Rep Power
    0

    Default "Make final" problem

    Hi. I want to assign ActionListener to the buttons in JToolBar: toolBarMenu using FOR statement. However, I've got the error message that "j" must be final. If "j" is final, it cannot be changed... So, how could I change "j" for "createBaseRight(j);"

    PHP Code:
            int j = 1;
            for (int i=0; i<=toolBarMenu.getComponentCount(); i++) {
              toolBarMenu.getButton(i).addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    createBaseRight(j); // "make final" message
                    j++;
                }
              });
            }
    PHP Code:
    private void createBaseRight(int chooser) {
    ...
    }
    Thanks!

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    edit: ignore this reply.

    kind regards,

    Jos

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

  4. #4
    LianaN is offline Member
    Join Date
    Aug 2010
    Posts
    40
    Rep Power
    0

    Default

    Ok, thanks a lot! It works.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    On second thought, if that's the only place you'd use that index variable, then making the variable a class field may unnecessarily clutter your code. Another solution is to use a local final variable:

    Java Code:
            //int j = 1;
            for (int i=0; i<=toolBarMenu.getComponentCount(); i++) {
              final int jIndex = i + 1;
              toolBarMenu.getButton(i).addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    createBaseRight(jIndex); // "make final" message
                    //j++;
                }
              });
            }

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default

    Isn't this the same as ActionListener for JToolBar components ?

    db

  7. #7
    LianaN is offline Member
    Join Date
    Aug 2010
    Posts
    40
    Rep Power
    0

    Default

    Well, it is not completely the same. In that time I didn't know how to add ActionListener. Now I was asking about making my code more elegant and short by means of using FOR statement. But sorry if it is still a crossposting.

  8. #8
    LianaN is offline Member
    Join Date
    Aug 2010
    Posts
    40
    Rep Power
    0

    Default

    Wow, it is exactly what I need, because, you are right, I had a problem with using class variables.

Similar Threads

  1. Replies: 4
    Last Post: 01-04-2010, 02:23 PM
  2. problem with argument list and precedence "(" and ")"
    By helpisontheway in forum Advanced Java
    Replies: 6
    Last Post: 12-24-2009, 08:50 AM
  3. Replies: 0
    Last Post: 04-02-2009, 08:02 PM
  4. Replies: 2
    Last Post: 01-24-2009, 07:56 PM
  5. Replies: 1
    Last Post: 10-20-2008, 08:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •