Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Action objects won't enable

 
Ranch Hand
Posts: 173
2
Netbeans IDE PHP Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, world!

Im so bad at posting here please forgive me! I have encountered a somewhat fantastic problem concerning Action objects in jmenus,
I can't set them to be enabled again after having disabled them. My Action objects are attched to an ActionMap in a JFrame. I have a method
in which I call them by their key name like below



This is how a populate my ActionMap with Action objects



Can anyone see why this is not working? Am I missing something here? Is there something special with Action objects in jmenus that I have missed out?
I have also tried



But that one fails as well. Hence I am stuck here. Any kind of enlightment are greatly much appreciated in advance.

Very kind regards,
Robert!


 
Saloon Keeper
Posts: 12137
258
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why aren't you using the setEnabled method on the action?
 
Robert Ingmarsson
Ranch Hand
Posts: 173
2
Netbeans IDE PHP Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Why aren't you using the setEnabled method on the action?



Best, Mr van Hulst!

I have tried that also in the setActionEnabled(String name, Boolean isEnabled) - method, but it didn't worked.



Thanks for the reply! I have checked over my framework very thoroughly and I can't see why that method above shouldn't work.
I have an actionPerformed method within a JDialog in which i wan't to enable the Action when the user clicks the "insert"-button in the
dialog but the JMenu refuses to activate.

Very kind regards,
Robert!

 
Stephan van Hulst
Saloon Keeper
Posts: 12137
258
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you already debugged your application to ascertain that the action is not null after you retrieve it from your action map?

Please also show us the code where you populate the action map.
 
Rancher
Posts: 3191
29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using setEnabled(...) works for me.

See: How to Use Actions for a working example.
 
Robert Ingmarsson
Ranch Hand
Posts: 173
2
Netbeans IDE PHP Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Have you already debugged your application to ascertain that the action is not null after you retrieve it from your action map?

Please also show us the code where you populate the action map.



Best, Mr van Hulst!

I have tried to debug the application using Netbeans 8, but that one shows no error from my point of view that is. It's a small media manager application
that I have just started on and so far the code is very simple and embarasing small, but it's that Action error that is messing things up big. But here is the code for
the JMainFrame class that extends a JFrame class. I have populated the actionmap in the method installActions(); in the JMainFrame constructor.



Very kind regards,
Robert!


 
Rob Camick
Rancher
Posts: 3191
29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I can't set them to be enabled again after having disabled them.



Well I'm not sure why you posted over 300 lines of code:

1. That is waaaayyyy too much code for us to look at and attempt to understand what you are doing.
2. I did look at it, but I could not see anywhere in the code where you disable the Action or attempt to enable it.
3. We can't execute the code because we don't have access to your database or your ResourceBundle etc.

So, instead you should be post a SSCCE that demonstrates the problem. That is your stated problem is the ability to enable/disable Actions in a menu item.

To test this all you need is a JFrame with a JMenuBar and a JMenu and a JMenuItem with an Action. Then maybe you add a couple of buttons to enable or disable the Action

This entire code will be about 30 lines of code.

Then if you still have a problem we can help you.

Then once you understand the basic usage you can debug your own application to see what you are doing differently.
 
Stephan van Hulst
Saloon Keeper
Posts: 12137
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't need to catch an exception because your problem is likely not caused by an exception, but because the action you're trying to enable is not contained in the action map. We can't tell for sure though, because as Rob pointed out, there is no code that attempts to enable any particular action.
 
Robert Ingmarsson
Ranch Hand
Posts: 173
2
Netbeans IDE PHP Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, again!

Its working now I think, one should never say never though. Weird I had to use a SwingUtilities.inovkeLater method for the action to enable properly
in the calling actionPerformed method like below:



Now my setActionEnabled-method seems to work finally.

Oh, I am sorry for the code spam earlier in this post, mr Camick. I will do better in the future, but thanks for the advice and guidance.

Best regards,
Robert!



 
Stephan van Hulst
Saloon Keeper
Posts: 12137
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not weird if you're calling it from a background thread. Changes to the UI must happen on the event dispatch thread.

From where are you calling that code? There may be better ways to deal with this.
 
Robert Ingmarsson
Ranch Hand
Posts: 173
2
Netbeans IDE PHP Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:It's not weird if you're calling it from a background thread. Changes to the UI must happen on the event dispatch thread.

From where are you calling that code? There may be better ways to deal with this.



Best, Mr. Van Hulst!

I call the code in question from within a class that extends AbstractAction and it is executed in the actionPerformed-method.
Hope that was the right answer.

Best wishes,
Robert!
 
Stephan van Hulst
Saloon Keeper
Posts: 12137
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then the action that enables your other action is itself not executed on the event dispatch thread. Going by how your actions are publicly accessible, I'm assuming that you're calling them yourself, rather than having them called from your user interface.

Which action is responsible for enabling/disabling your MediaTitleAction? Where is it called from?

You'd do well to make all your class members private. You should also declare constants for your action map keys, instead of using string literals everywhere.
 
Bartender
Posts: 4001
156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Robert Ingmarsson wrote:I call the code in question from within a class that extends AbstractAction and it is executed in the actionPerformed-method.Robert!


But code from ActionListeners is executed on the EDT.

Here is an interesting topic about the EDT you might want to read. It is the reply from Sam Gooding.    EDT

But in the beginning of me using Swing, I ran my code from main, not knowing anything about the existence of an EDT, and I never faced problems, as far as I can remember. So I wrote this code to see if components can be enabled/disabled from outside the EDT. After 5 seconds the 'enabled'button is disabled from the constructor code. And further clicks on the left button are processed by an ActionListener, with the code executed from the EDT.

But global health warning: the code is only for demonstration purposes and is not meant for any serious work!

So, are you sure your problem was caused by not running from the EDT?
 
Clowns were never meant to be THAT big! We must destroy it with this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic