| Author |
Is there a way to paint swing components without subclassing?
|
Paul Adcock
Ranch Hand
Joined: Jan 22, 2011
Posts: 34
|
|
For instance, I could have the class
Is there a way that I could use some method or something with a regular JPanel for instance, it doesn't have to be a JPanel, though we could focus on JComponents for now, to paint it without having to subclass it?
Another case of interest would be this:
Is there a way to do that with the painting the icon on the background of the JMenuBar without having to subclass it and call the paintComponent() (or some other paint method)?
|
Hi all.
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 16483
|
|
|
You might be able to do it with something like byte-code manipulation, or some other equally obscure process. But since the subclassing idea works perfectly well, why are you looking for alternatives?
|
 |
Paul Adcock
Ranch Hand
Joined: Jan 22, 2011
Posts: 34
|
|
Because it can be burdensome to have all of these subclasses if I want to paint a lot of components.
However, I have no idea how to do byte-code manipulation.
I had thought of trying to find the paint method code in Component and creating an interface that had the paint method or something.
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 16483
|
|
|
Ah. Then just do the subclassing. You still have to write the code to do the drawing, which is the hard part, creating a subclass is just a few repetitive lines of code.
|
 |
Darryl Burke
Bartender
Joined: May 03, 2008
Posts: 4167
|
|
Two other approaches:
1) Use a JLabel and set an Icon which has your custom painting code in its paintIcon(...) method. If you want to fill the component or otherwise position your graphic regardless of where the labbel tries to position its Icon, you can ignore the [x, y] parameters to the method. If you want that the size of the label not be influenced by the graphic, return 0 as the Icon's height and width, but make sure the label is displayed in a layout that ignores its preferredHeight.
2) If on Java 7, use JLayer. That's what it's meant for. If you're not yet on 7, search for and incorporate JXLayer from the SwingX project.
|
luck, db
There are no new questions, but there may be new answers.
|
 |
Darryl Burke
Bartender
Joined: May 03, 2008
Posts: 4167
|
|
Paul Adcock wrote:Is there a way to do that with the painting the icon on the background of the JMenuBar without having to subclass it and call the paintComponent() (or some other paint method)?
Missed this in my first reading.
Yes, you can create and set a custom MenuBarUI extending the default UI delegate of your application's LaF. Unfortunately, this does mean that if you allow changing the LaF at runtime you're going to have to create a subclass for each LaF that you support. And even if you don't have LaF changes, you will need to explicitly set a LaF -- you can't rely on the default LaF (presently Metal) always remaining the same.
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
paintBorder is called after paintComponent (and documented to do so). Therefore, if you set a border you can let that border do the painting. To make sure that it also works if the component already has a border, use a compound border:
Now all you need to do is create the image border, but that shouldn't be too hard.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Paul Adcock
Ranch Hand
Joined: Jan 22, 2011
Posts: 34
|
|
Actually, I'm not familiar with how UI's work. I know it means user interface, but the specifics of what exactly it is, what the component looks I'm guessing, but again, not certain of what exactly it is or how to manipulate it.
|
 |
Darryl Burke
Bartender
Joined: May 03, 2008
Posts: 4167
|
|
Time to go through the tutorials then.
http://docs.oracle.com/javase/tutorial/uiswing/index.html
|
 |
 |
|
|
subject: Is there a way to paint swing components without subclassing?
|
|
|