File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Is there a way to paint swing components without subclassing? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Reply Bookmark "Is there a way to paint swing components without subclassing?" Watch "Is there a way to paint swing components without subclassing?" New topic
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
    
    2

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
    
    2

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
    
    3

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
    
    3

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
    
    3

Time to go through the tutorials then.

http://docs.oracle.com/javase/tutorial/uiswing/index.html
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is there a way to paint swing components without subclassing?
 
Similar Threads
Swing doubt. not able to display pic on swing frame
Please check if the paint method used properly
JPanel
How to draw a line on a JPanel without extending it o overriding the paint method?
Radiobutton circle disappears when using icon