jMenu bar in Java Swing not enabling issue

242 views Asked by At

I have a jMenu bar and it is enabled by default. On selecting first item, I am disabling the jMenu

enter image description here

Here is code on first menu item. Basically ViewCustomerAccountsDetails is a jInterframe :-

       ViewCustomerAccountsDetails vca = new ViewCustomerAccountsDetails();
        this.jDesktopPane1.add(vca);
        vca.show();
        jMenu1.setEnabled(false);

Now, in class ViewCustomerAccountsDetails, I have a Button and on its click, i am hiding this JInternal frame and trying to enable the jMenu bar :-

    CustomerMainScreenLogin cmsl = new CustomerMainScreenLogin();
    cmsl.jMenu1.setEnabled(true); //jMenu is public
    this.dispose();

But it is not working. It is still disabled.

2

There are 2 answers

1
dic19 On BEST ANSWER

Here:

CustomerMainScreenLogin cmsl = new CustomerMainScreenLogin(); // A new instance? Why?
cmsl.jMenu1.setEnabled(true); //Here jMenu1 references the new instance menu 1, not the current one's.
this.dispose();

Why do you create a new instance of CustomerMainScreenLogin class? Most likely jMenu1 is enabled but in a new non visible CustomerMainScreenLogin object. To make it visible just call cms1.setVisible(true) and you'll see that.

So you need to reference the current instance of CustomerMainScreenLogin class instead of creating a new one. For instance by making jMenu1 static and calling jMenu1.setEnabled(true) in this way:

CustomerMainScreenLogin.jMenu1.setEnabled(true);
this.dispose();
0
Joop Eggen On

I am not entirely sure where, but an invokeLater is probably needed. This does the heavy work a bit later, and allows the event queue thread to handle all events, like the button click fast and keep responsive and reactive - as with menus being enabled.

    EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            jMenu1.setEnabled(false);
        }
    }
    vca.show();

    cmsl.jMenu1.setEnabled(true);
    EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            dispose();
        }
    }