Making this paintbrush program I decided I want to clear the panel container (panel2
in the linked code). I used PanelName.setForeground(null)
and it worked. What I'd like to know is if there are known drawbacks of this approach and other ways of removing graphics objects from the container.
Asked
Active
Viewed 1,174 times
3
2 Answers
5
If you override paintComponent
, you can clear its Graphics
using Graphics#clearRect
. But overall, I prefer your approach because mine would require you to either subclass JPanel
, or anonymously declare one.

mre
- 43,520
- 33
- 120
- 170
3
The converse of @mre's answer is that "It is up to the look and feel to honor this property, some may choose to ignore it." I don't know of a PanelUI
that ignores the foreground color, but it's something to be aware of when testing.
Addendum: As @kleopatra comments, the JComponent@setForeground()
API links to the Component#getForeground()
API, which notes the following: "if this component does not have a foreground color, the foreground color of its parent is returned."

trashgod
- 203,806
- 29
- 246
- 1,045