0

After what I learned from my previous question, I would like to use a texture to paint the text on an extended JButton while it's being pressed. The first step was setting up the button's ChangeListener and stateChanged method; I have these working and can set the foreground color in the method, so that the text will be one color while the button's pressed and another while not.

Building on this, I replaced the setForeground call with the drawString code I used for my toggleButtons. It works, but immediately after the text is drawn it's overwritten by the button being automatically repainted. I tried throwing the code in a "while (model.isPressed())" loop, but that had some pretty terrible results (system hang). How would I go about manually repainting my button, so that it's only redrawn during the stateChanged method?

Community
  • 1
  • 1
bendicott
  • 369
  • 2
  • 17
  • 1
    your questions are on hight theoretical level, please post code that demonstraded your issue with JButton, in the http://sscce.org/ form – mKorbel Nov 18 '11 at 20:58
  • My JButton's doing exactly what it's meant to do; I'm only asking for ideas, as I'm not sure how to approach the modification I'd like to make. – bendicott Nov 19 '11 at 03:26

1 Answers1

2

It seems to me that you are going the wrong way to change the look of your button. I think it would be easier for you to create a class that would handle the look and feel of your button, instead of manually handling the drawing parameters of your button inside the button's code. Blocking the repaint() calls isn't really the way to go I believe in your case.

I would personally create my own ButtonUI implementation that would handle all the paint rules (foreground color based on button state for instance), then I would call the setUI on the button, specifying an instance of this new ButtonUI as a parameter. If you don't want to handle all the drawing stuff, you can always use your new class as a proxy to the button's already existing UI handler (through JButton's getUI() method), and make changes only where you need them (I haven't tested it myself, but I'm pretty sure it would work just fine).

Of course, this represents more coding for you, but it would localize your look and feel handling in a single class, and it would fit in Swing's way of working. There are a few resources on the web to get you started (here, here and here).

Laf
  • 7,965
  • 4
  • 37
  • 52
  • +1 but `ButtonUI` would be `Laf` (excluding Windows Native OS) sensitive – mKorbel Nov 18 '11 at 21:02
  • @mKorbel Not if you use it only as a proxy, and define some default values in the `UIDefaults` class, or any other way of specifying your color scheme. But you would need to be careful indeed so that your application does not look silly in other look and feels (if it is a concern at all in your application). – Laf Nov 18 '11 at 21:05
  • Thanks for the advice; I've never delved into the ButtonUI before, but I'll take a look at the links and post back in a bit – bendicott Nov 19 '11 at 03:29
  • This was exactly what I was looking for; I went ahead and moved the drawing code for my toggle buttons into a custom UI as well so that I no longer need a subclass. Thanks! – bendicott Nov 20 '11 at 23:51