Enable/Disable JMenu in JFrame on click of button in JInternalFrame

Hello there. Could anybody help me out of the situation?

I am trying to enable/disable JMenu of JFrame on click of a button which is in JInternalFrame.

I want to set JMenu(this is in JFrame).setEnable(true) in ActionPerformed() of JInternalFrame, but JFrame and JInternalFrame are the different classes and I do not know how I can access JMenu in main frame.

How should I write something like mainframe.menu.setEnabled(true) from internal frame?

For JInternalFrame window action, there is JInternalFrameListener, but this is not relevant for my situation because I am trying to control on click of abutton.

Can anybody suggest a solution?

[684 byte] By [muppet77a] at [2007-10-3 2:54:12]
# 1

why do not you keep a refference of your main JFrame in your internalFrame, and in your JFrame class create a method with parameter boolean and make that public, depending upon that boolean flag enable/disbale the JMenu in JFrame, now you can access your JFrame menu from your internalFrame.

:)

Aniruddha-Herea at 2007-7-14 20:43:16 > top of Java-index,Desktop,Core GUI APIs...
# 2

Thank you for the reply.

Ok, I am very new to Java and do not manage to implement your advice in my actual coding.

Will you give me further help using the actual code? Following are the extract of 2 classes.

// Main Frame

public class MainFrame extends JFrame{

// Constructor which adds the menu

public void activateMenu(){

menuBar.setEnabled(true);

}

public static void main(String args[]) {

MainFrame mainframe = new MainFrame();

}

}

// Internal Frame

public class InternalFrame extends JInternalFrame{

//Constructor which adds a JButton.

public void actionPerformed(ActionEvent ae) {

// How can I keep a reference to the MainFrame?

mainframe.activateMenu();

}

}

I am sorry to trouble you. I just need more experience.

muppet77a at 2007-7-14 20:43:16 > top of Java-index,Desktop,Core GUI APIs...
# 3

If I understand your question correctly, I think I recently did something like this.

I used the Observer/Observable design pattern to have the receive notifications about when to change state.

In your case you could make the button observable (or whichever object holds the state that is to be conveyed to the menu) and the JFrame holding the menu would then observe the button.

Hope it helps!

ivakra at 2007-7-14 20:43:16 > top of Java-index,Desktop,Core GUI APIs...
# 4

// Main Frame

public class MainFrame extends JFrame

{

public MainFrame()

{

InternalFrame l_int = new InternalFrame(this);

//try to initiate internal frame like this

}

public void activateMenu()

{

menuBar.setEnabled(true);

}

public static void main(String args[])

{

MainFrame mainframe = new MainFrame();

}

}

// Internal Frame

public class InternalFrame extends JInternalFrame

{

private MainFrame m_mainFrm = null;

public InternalFrame(MainFrame a_objMainFrame)

{

//your own code

m_mainFrm = a_objMainFrame;

}

public void actionPerformed(ActionEvent a_objevent)

{

if(m_mainFrm != null)

m_mainFrm.activateMenu();

}

}

try this.. hope this will help you

:)

Aniruddha-Herea at 2007-7-14 20:43:16 > top of Java-index,Desktop,Core GUI APIs...
# 5
grrr... I just had to pass mainframe itself as an argument to the internal frame. I got it now.Thank you very much, Aniruddha-Here.And thank you too, ivakr for your attention.Its just nice talking to people here and getting assist. Eased my irritation so quick.
muppet77a at 2007-7-14 20:43:16 > top of Java-index,Desktop,Core GUI APIs...