What is the correct way to get the current background color of a component in Swing?

I am writing a custom Swing component with my own painting. I would like to ask if the current appearance components are opaque or not, and if so, what is their background color so that my component can use it. I find it hard to find this on Google. Somebody knows? Thank!

+3
source share
2 answers

It is pretty simple:

public class MyComponent extends JComponent {

    public void paintComponent(Graphics g) {

        if (this.isOpaque()) {
            // Paint background
            g.setColor(this.getBackground());
            g.fillRect(0,0,this.getWidth(), this.getHeight());
        }

        g.setColor(this.getForeground());
        // Continue painting
    }
}
+2
source

I do not understand this question. Each component can have a different background color, and what background color do you want to use your custom component?

, LAF JPanel, , , JPanel, .

backgroud , UIManager . . UIManager .

+1

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


All Articles