United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: JDK-4421165 JLabel setBackground
JDK-4421165 : JLabel setBackground

Details
Type:
Bug
Submit Date:
2001-03-02
Status:
Closed
Updated Date:
2001-03-02
Project Name:
JDK
Resolved Date:
2001-03-02
Component:
client-libs
OS:
windows_nt
Sub-Component:
javax.swing
CPU:
x86
Priority:
P3
Resolution:
Not an Issue
Affected Versions:
1.3.0
Fixed Versions:

Related Reports

Sub Tasks

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
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
WORK AROUND

setBackground works for Label
                                     
2004-06-11



Hardware and Software, Engineered to Work Together