Results 1 to 9 of 9
  1. #1
    javabarn is offline Member
    Join Date
    Jun 2010
    Posts
    12
    Rep Power
    0

    Question Changing menu status

    I am using java swing to create a menu


    public JMenuBar createMenuBar()
    {
    JMenuBar menuBar =new JMenuBar();
    menuBar.add(menu);
    JMenuItem menuItemtest = new JMenuItem("New");
    menu.add(menuItemtest);
    menuItemtest.addActionListener(this);
    JMenuItem menuItem = new JMenuItem("Load");
    menuItem.addActionListener(this);
    menu.add(menuItem);

    When "Load" is selected I want to dim "New"

    but I get an error message at

    if (myteststr=="Load")
    {pnl.setVisible (true);
    //read file here
    menuItemtest.setEnabled(false);


    of C:Myjava> javac Buttons.java
    Buttons.java:135: cannot find symbol
    symbol :variable menuItemtest
    location: class Buttons
    MenuItemtest.setEnabled(false);

    Can anyone help

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

    Default

    It's telling you that it can't find the variable MenuItemtest. Remember that capitalization matters, but likely your problem is one of scope. Your menuItemtest variable appears to be declared within a constructor or method and if so, it is only visible from within that constructor or method.

    Also, never compare Strings with the == operation, but instead use the equals(...) or equalsIgnoreCase(..) method.

    Also, welcome to the forum! And when you get a chance, please read my signature for information on code tags.

  3. #3
    javabarn is offline Member
    Join Date
    Jun 2010
    Posts
    12
    Rep Power
    0

    Default

    Thanks Fubarable,

    It was my transcription from the command screen where the capital issue came from. In code the capitalization was correct so must be scope as you suggest- how do I make it visible throughout the programme?

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

    Default

    It's all on where you declare the variable. If you declare it in the method:
    Java Code:
    public class Foo {
    
      public void myMethod() {
        int myVar = 3;
      }
     
    }


    Then the variable myVar is visible only within myMethod and no place else. If however I declare myVar in the class itself, then it's visible throughout the class:

    Java Code:
    public class Foo {
      int myVar = 0;  // declared in the class
    
      public void myMethod() {
        myVar = 3;  // it's visible here
      }
     
    }

  5. #5
    javabarn is offline Member
    Join Date
    Jun 2010
    Posts
    12
    Rep Power
    0

    Default

    Many thanks - that has solved it.

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

  7. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,386
    Blog Entries
    7
    Rep Power
    20

    Default

    There's another trick that can solve this scoping problem: define the local variable as final and use it in a local anonymous class, like so:

    Java Code:
    public JMenubar createBar() {
    
    final JMenuItem newItem= ...;
    final JMenuItem loadItem= ...;
    loadItem.addActionListener(new ActionListener() {
       public void actionPerformed(ActionEvent ae) {
          newItem.setSelected(false): // newItem can be used here
       }
    }
    It's the 'final' qualification that does the trick; the same trick that forbids good grown up closures in Java 7. But that is an entirely different story ;-)

    kind regards,

    Jos

  8. #8
    berkeleybross's Avatar
    berkeleybross is offline Senior Member
    Join Date
    Feb 2010
    Location
    England
    Posts
    187
    Rep Power
    5

    Default

    I know its a bit off topic, but JosAH could you explain why making the variable final works? I've never trusted it, I always make anonymous class' call methods in the class. I know it _does_ work but I dont get _why_.

    can you shed some light?

    Berkeleybross

  9. #9
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,386
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by berkeleybross View Post
    I know its a bit off topic, but JosAH could you explain why making the variable final works? I've never trusted it, I always make anonymous class' call methods in the class. I know it _does_ work but I dont get _why_.

    can you shed some light?

    Berkeleybross
    Final variables can be copied over to the scope of the other (inner) class; therefore they have to be final, i.e. if in the original scope that variable changes its value, the copy will still have the original value causing a lot of turmoil. At the same time this mechanism more or less forbids real closures of scopes. Something that is a breeze in other (functional) languages mostly but it stabs us in the back with Java. I think that decision was a mistake.

    kind regards,

    Jos

Similar Threads

  1. Status bar in a JFrame
    By smartsubroto in forum New To Java
    Replies: 1
    Last Post: 08-26-2010, 03:45 PM
  2. Replies: 0
    Last Post: 07-07-2008, 04:47 PM
  3. How to use SWT menu and menu event
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 07-07-2008, 04:46 PM
  4. React to menu action and checkbox menu
    By Java Tip in forum javax.swing
    Replies: 0
    Last Post: 06-27-2008, 07:50 PM
  5. Replies: 7
    Last Post: 05-09-2008, 07:54 AM

Tags for this Thread

Posting Permissions

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