• Post Reply Bookmark Topic Watch Topic
  • New Topic

Is there a way to paint swing components without subclassing?  RSS feed

 
Paul Adcock
Ranch Hand
Posts: 48
Firefox Browser Java Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)?

 
Paul Clapham
Sheriff
Posts: 22527
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48
Firefox Browser Java Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 22527
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21095
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Paul Adcock
Ranch Hand
Posts: 48
Firefox Browser Java Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Time to go through the tutorials then.

http://docs.oracle.com/javase/tutorial/uiswing/index.html
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!