Swing component default gradient background

Is there a way to change the default background image for all instances of the swing component (e.g. JPanel) to paint using a gradient background? Or do I need to create a JPanel extension that will look with a gradient, and then use this instead of JPanel in my application?

+6
source share
2 answers

IMHO, it would be easier to just subclass the Swing component and override its paintComponent method to make a gradient picture. And then, as you said, use this custom component throughout the application.

This can be difficult with the user interface properties, as they can be consistent across all LaFs.

+3
source

It is not clear how much volume you plan to use. Did you mean by class (therefore, all instances of JPanel correspond to the new drawing scheme), or do you mean all components in the container (for example, everything in the frame)?

There are opportunities to do this depending on the component class, the place where you can connect is Look and Feel and based on the component instance, either the paintComponent () method, or if you need to replace the standard appearance of an existing component, where you cannot overwrite this method because you have no control over it by providing your own user interface (see Component.setUI) after the component has been created.

With the exception of rewriting paintComponent, the approach is not easy to implement. For most applications, a simple approach is the best :)

0
source

Source: https://habr.com/ru/post/910351/


All Articles