开发者

Manipulating a JToggleButton's ImageIcon while pressed/selected

I (very miraculously) answered my own question while writing this question , but it was such a find I wanted to share with everyone. I understand these should be true "questions" but it was a monumental find for me. There is a "question" below that can be answered, however.

I created a custom class that extends a JToggleButton. In this class, I remove all default MouseListeners so it will only accept mouse actions when I deem the object ready. The problem I had was manipulating the JToggleButtons ImageIcon (set with setIcon()). If the button was not displayed on screen (not visible due to a JScrollPane) when I told the button to be selected, the ImageIcon would disappear. Similarly, if I modified the ImageIcon while the button was selected, the ImageIcon would also disappear.

It turns out that in my custom MouseListener, I was using getModel.setSelected(true) instead of getModel.setPressed(true). Apparently I don't have a clear understanding of the difference between selected and pressed, but suffice to say this fixed my issue. My "question" would therefore be开发者_Go百科 clarification on these two terms.

To summarize, don't use setSelected() on a button when you mean to use setPressed(). What a pain to track this one down! I sincerely hope this helps someone else. Email is valid but a spam-catcher.


Ok, I found my own answer again, so I thought I'd share and close this question. A button is "selected" when it has a checkbox/radio icon. A button is "pressed" when the button has been pushed. JToggleButtons appear to use both attributes in combination to properly "click and press" the button.

The reason the ImageIcons were disappearing was because I had inadvertently mixed setPressedIcon()/setPressed() and setSelectedIcon()/setSelected(). Because I had confused the terminology and didn't recognize the difference, I was inconsistent in my usage.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜