1

I wrote a simple drawing program, and for to create a menu, I used this source, http://download.oracle.com/javase/tutorial/displayCode.html?code=http://download.oracle.com/javase/tutorial/uiswing/examples/components/MenuLookDemoProject/src/components/MenuLookDemo.java

Therefore in my program to show the menus, I only added these lines:

    MenuDemo demo = new MenuDemo();
    frame.setJMenuBar(demo.createMenuBar());

When I started the program, menu successfully works, but on the other hand, for example, when I click sth on menu, in method "actionPerformed" I want to change my program's boolean variable. But "actionPerformed" is exist in "MenuLookDemo.java", therefore I cannot reach the variables.

Can you suggest a solution please ?

Thanks

2
  • You should really start reading about Object-Oriented design. This is a OO-101-level question. Commented Oct 3, 2011 at 14:41
  • 2
    Is it not OK to ask OO-101 level questions here? I thought it was perfectly OK to ask this kind of stuff. Commented Oct 3, 2011 at 14:49

2 Answers 2

1

Maybe you can rewrite the class MenuDemo and pass your Object to MenuDemo to access your variable.

class MenuDemo{
  YourType obj;
  MenuDemo(YourType obj){
    this.obj = obj;
  }
  // Now you can access elements of obj
}
Sign up to request clarification or add additional context in comments.

3 Comments

Pikaurd is right -- MenuDemo needs some way to send information to your program (really -- your object, represented in his sample as 'obj').
I tried this one, but the main problem is, I want some variables to be effected when "actionPerformed" event flagged. I added arguments to the MenuDemo, but I cant send this object to "actionPerformed" method, therefore I cant reach that variable
Where is your actionPerformed? Can your paste your project structure here? I thought actionPerformed in MemoDemo, so the menu element could handle actions its received.
0

Building off of what Pikaurd has above, do this:

public class MyType {
  int x;
  public void doTheNeedful() {
    x = 5;
  }
}

Then make sure MenuDemo contains a field obj of class MyType. Inside actionPerformed(), call obj.doTheNeedful().

I'm deliberately not just giving you the code on this; the sentence above should be enough for you to figure it out.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.