Make Labels Opaque and Explore Custom Colors

Learn how to make your label opaque and create your own custom color by mixing RGB colours.

Components may be either opaque or transparent. Opaque components cover up components positioned behind them. A user cannot see through an opaque object, but transparent components allow underlying components to be seen. A component can be changed from opaque to transparent, or from transparent to opaque.

Making a label opaque

Why is the color of titleLabel still gray? A JLabel is transparent, so its own background color cannot be seen. Instead, the background color of the container behind a JLabel is seen. To see the background color of a JLabel, make the label opaque.

  1. Set titleLabel to opaque. Use JLabel's setOpaque() method.
...
private void initGUI() {
    ...
    titleLabel.setBackground(Color.BLACK); 
    titleLabel.setForeground(Color.WHITE); 
    titleLabel.setOpaque(_________); 
    add(titleLabel, BorderLayout.PAGE_START);
    ...

Get hands-on with 1200+ tech skills courses.