This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes AbstractAction with toggle state Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "AbstractAction with toggle state" Watch "AbstractAction with toggle state" New topic
Author

AbstractAction with toggle state

vladislav peichev
Greenhorn

Joined: Apr 28, 2011
Posts: 22
Does anyone know an easy way to extend custom AbstractAction extending class with toggle state - the custom implementation is added in a JToolbar and I need it to be shown toggled/untoggled.
Thanks in advance.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 4094
    
  18

You mean like a checkbox? Why don't you just add a JCheckBox to the JToolBar?

[edit]

If you actually want a JButton that has two states, you can simply add a JToggleButton to the JToolBar.


The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.
vladislav peichev
Greenhorn

Joined: Apr 28, 2011
Posts: 22
Not like JCheckBox but like JToggleButton. My application uses customized AbstractAction with some additional functionality e.g. enabling or disabling depending on condition so I cannot use the standard JToggleButton. I want my AbstractAction extension to be displayed toggled/untoggled.
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3719
    
  61

Another alternative is to use a JToggleButton (you can pass the Action instance to its constructor). I suppose JToggleButton would look better on a toolbar.
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3719
    
  61

vladislav peichev wrote:Not like JCheckBox but like JToggleButton. My application uses customized AbstractAction with some additional functionality e.g. enabling or disabling depending on condition so I cannot use the standard JToggleButton. I want my AbstractAction extension to be displayed toggled/untoggled.

Of course you can use JToggleButton. In your child of AbstractAction, you'll just check the action's Selected state. That is, don't mess with the selection state yourself, let the JToggleButton manage it. Just test the state in actionPerformed and enable/disable the other controls accordingly.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 4094
    
  18




[Thumbnail for lightning.png]

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: AbstractAction with toggle state
 
It's not a secret anymore!