Can you disable a JButton without graying out the button itself?
When you use setEnbaled(false), the button disables and turns to gray. Is it possible to disable the button but make the appearance of the button still the same?
You could ignore the button press in your ActionListener if a flag is set; however, you should not disable the button without showing it as disabled: it will only confuse the users of your application.
I suggest setting a ButtonModel with overridden setArmed and setPressed. Exactly how you override it depends on exactly what you want it to do - should it appear to be pressed when pressed, for instance? Only set the ButtonModel once for a JButton. Add or repurpose state on the ButtonModel to indicate how you want it o behave at any given moment.
Clearly, GUIs with non-standard behaviour are likely to confuse users.
Related
I made a JButton and I am trying to remove the mouse events that is the "borders and colors" that comes with it by default. I tried the following:
setUndecorated(true);
But it wasn't the right code to do that.
Is there a way to remove the MouseListener that the JButton brings and set the JButton to how you want it to be decorated?
How to set a JButton to be undecorated...
If you want to play with buttons decoration then take a look to AbstractButton API (class which JButton extends from). Particularly these methods:
setContentAreaFilled(boolean flag): Sets the contentAreaFilled property. If true the button will paint the content area. If you wish to have a transparent button, such as an icon only button, for example, then you should set this to false.
setBorderPainted(boolean flag): Sets the borderPainted property. If true and the button has a border, the border is painted.
setRolloverEnabled(boolean flag):
Sets the rolloverEnabled property, which must be true for rollover
effects to occur.
JComponent.setBorder(null): since some look and feels might not support the borderPainted property, in which case they ignore this, you might want to set the buttons border to null instead.
...and remove the MouseListener
Don't understand what do you mean but I suspect you want to get rid of the animation that takes place when the button is pressed or perhaps roll-over efects. If this is the case then playing with methods mentioned above should be enough.
I want to extend the JButton class and manually paint the button's icon, though I need to get the correct icon according to the state the button is in, how do i do that?
Method getIcon() returns the default icon only, regardless of what state the button is in...
Instead, implement the Icon interface. ColorIcon, illustrated here, is a simple example used by a JButton subclass. Try instantiating ColorIcon for each of several colors . Use the Icon instances as the the button's pressed or rollover icon to see the effect. See also this related example.
getIcon() will return the default icon, but getDisabledIcon() will return the disabled icon.
Also
getDisabledSelectedIcon() returns the icon used by the button when it's disabled and selected. If no disabled selection icon has been set, this will forward the call to the LookAndFeel to construct an appropriate disabled Icon from the selection icon if it has been set and to getDisabledIcon() otherwise.
Some look and feels might not render the disabled selected Icon, in which case they will ignore this.
Then there is getPressedIcon(), getRollOverIcon() and getRolloverSelecedIcon().
Check the AbstractButton manual page.
As you can see there are many options available to you. On the other hand, you probably are the one who has set the icons, so if you may keep them in an Icon array, you could get them straight from there.
I'm working on a minesweeper in Java with Swing and I figured it'd be a fast way to get "rid" of a button that was clicked by using
JButton.setEnabled(false); (with a proper icon too, of course).
But do I have to remove all the listeners connected to this button later or is it enough and I can just forget about the said button then?
You have 2 different questions, one in your title, and one in your description.
Is removing actionListener necessary when you disable the button?
As stated in the previous comments, no.
But do I have to remove all the listeners connected to this button later...
Yes, if you have other kinds of listeners. For example, a MouseListener will still fire if the button is disabled. Usually, there is no need for a MouseListener on JButton, but there may be in some corner cases. I'm not sure about the other types of listeners that can be added to a JButton.
Just wanted to clarify.
I would like to check whether a certain javax.swing.JButton (regular push-button) is pressed down (before it was released). Is there any option at all to check whether a button is down?
The most trivial solution is to add a MouseListener that will respond to the mouse click and release events. But, this does not cover the case where the button was activated by the Enter key, or any other way. I don't want to disable activating the mouse by keyboards or other ways - I just want to know when is it pressed down without restricting it's behaviour.
I tried listening to all the different events, and the only two that do respond to button press are the ActionPreformed (ActionEvent) and the StateChanged (ChangedEvent) events. ActionPreformed is executed once per click, meaning only after the button was pressed and released, so it's not good. StateChanged is indeed invoked several times when I click a button, and several times when I release it. But, the event object only includes information about the source widget (the button) and no information about the state change itself. This prevents from distiguishing which of the events we want to catch.
Thanks in advance!
ButtonModel can do that, more here or here or maybe off-topic JMenuItem & ChangeListener by #kleopatra
I read that a JButton implements ItemSelectable and into documentation it has the method addItemListener so I can argue that it can generate an ItemEvent... but when I register with a JButton (but also for a JMenuItem) that interface the event is not rised?
Why?
I understand that if into docs is reported that a component has an add....Listener it means that it support that event... but for experience isn't often so...
What's the truth?
There is a difference between a button being "pressed" (which fires an ActionEvent) and a button being "selected" (which fires the ItemEvent). By default a JButton is backed by a javax.swing.DefaultButtonModel. If you look at the setPressed and setSelected methods in default button model you'll see the code that fires the different events.
So if you programmically call JButton.setSelected, your item listener will be fired. If you click the button, you'll only get the action event.
Note also that with a JButton (unlike, say, a JToggleButton) you probably won't see much visually when it is selected.