JDK-4421165 : JLabel setBackground
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 1.3.0
  • Priority: P3
  • Status: Closed
  • Resolution: Not an Issue
  • OS: windows_nt
  • CPU: x86
  • Submitted: 2001-03-02
  • Updated: 2001-03-02
  • Resolved: 2001-03-02
Description
The setBackground method in JLabel does not seem to be working.

import java.awt.*;
import javax.swing.*;

public class LabelTest {

	public static void main(String args[]) {
		JFrame frame = new JFrame();
		Label label = new Label("This works!");
		JLabel jLabel = new JLabel("This does not!");

		Container contentPane = frame.getContentPane();
		FlowLayout flowLayout = new FlowLayout();
		contentPane.setLayout(flowLayout);

		label.setForeground(Color.white);
		jLabel.setForeground(Color.white);

		label.setBackground(Color.green);
		jLabel.setBackground(Color.red);
		contentPane.add(label);
		contentPane.add(jLabel);
		frame.setSize(350, 250);
		frame.setVisible(true);
	}
}

Comments
WORK AROUND setBackground works for Label
2004-06-11

EVALUATION JLabel is not opaque by default, which, among other things means that its background isn't painted. To have the background painted set the label to be opaque. scott.violet@eng 2001-03-02
2001-03-02