JLabel.setBackground(Color.red) not working

Hi, I am using the (JLabel) lblPrmf.setBackground(Color.RED), but the label is not colored. Here is my program:

import java.util.*;

import javax.swing.*; //JFrame and JPanel etc...

import java.awt.*;//BorderLayout and Container etc....

import java.awt.Color;

import java.awt.event.*;//ActionListener og WindowAdapter

public class AllVac extends JFrame

{

//Define JLabel

private JLabel lblDays;

private JLabel lblAbs;

private JLabel lblPrmf;

//Define JButton

private JButton month;

private JButton back;

private JButton forward;

//Define JPanel

private JPanel south;

private JPanel north;

public AllVac()

{

//JFrame jf = new JFrame("Vacation for the Stavanger Team ");

Container c = getContentPane();

//c.setTitle("Vacation for the Stavanger Team ");

c.setLayout(new BorderLayout());

c.add(new ShowNorth(), BorderLayout.NORTH);

c.add(new ShowSouth(), BorderLayout.SOUTH);

pack();

setVisible(true);

addWindowListener(new Close());

}

public static void main(String args[])

{

AllVac a = new AllVac();

}

public class ShowNorth extends JPanel

{

public ShowNorth()

{

north = new JPanel(new GridLayout(1, 3));

month = new JButton("August 2006");

back = new JButton("<<");

forward = new JButton(">>");

north.add(back);

north.add(month);

north.add(forward);

add(north);

}

}

public class ShowSouth extends JPanel

{

public ShowSouth()

{

int days = 31;

south = new JPanel(new GridLayout(16, days+1));

//Arrange the days

for ( int i = 1; i<(days+1); i++)

{

lblDays = new JLabel(" " + Integer.toString(i));

lblDays.setBorder(BorderFactory.createLineBorder(Color.red));

south.add(lblDays);

}

add(south, BorderLayout.LINE_START);

//Fill in the names and vacation days

for ( int i = 1; i<(days+1); i++)

{

lblAbs = new JLabel("");

south.add(lblAbs);

}

add(south, BorderLayout.LINE_START);

for (int j = 0; j<14; j++)

{

for ( int i = 1; i<days+1; i++)

{

String v = "v";

lblPrmf = new JLabel(" " + v + " ");

lblPrmf.setBackground(Color.RED);

lblPrmf.setBorder(BorderFactory.createLineBorder(Color.blue));

lblPrmf.setBackground(Color.RED);

lblPrmf.repaint();

south.add(lblPrmf);

}

add(south);

}

}

}

public class Close extends WindowAdapter

{

public void windowClosing(WindowEvent c)

{

System.exit(0);

}

}

}>

[2787 byte] By [Tjoge01a] at [2007-10-3 3:28:49]
# 1

When you post code, please use[code] and [/code] tags as described in [url=http://forum.java.sun.com/help.jspa?sec=formatting ]Formatting tips[/url] on the message entry page. It makes it much easier to read.

lblPrmf.setOpaque(true);

lblPrmf.setBackground(Color.RED);

PhHeina at 2007-7-14 21:22:27 > top of Java-index,Java Essentials,Java Programming...
# 2
Thank very much, PhHein I have added the code and I got what I wanted..
Tjoge01a at 2007-7-14 21:22:27 > top of Java-index,Java Essentials,Java Programming...
# 3
You're welcome.
PhHeina at 2007-7-14 21:22:27 > top of Java-index,Java Essentials,Java Programming...
# 4

Check out my reply in one of your other postings:

http://forum.java.sun.com/thread.jspa?threadID=757529&messageID=4327417

a) you don't listen to the advice given to you

b) you don't bother to take time to confirm whether the suggestions you get in your postings are helpfull or not.

So, I'm not going to waste time helping this time, (even though its a one line solution).

camickra at 2007-7-14 21:22:27 > top of Java-index,Java Essentials,Java Programming...
# 5

Hi Camickr,

Yes, Thanks for the answer from my previouse posting. It did help me.

Answers to your comments:

Your comment a: you don't listen to the advice given to you

My answer: I guess you are refering to the use of and

tags in my java examples? I forgot. Will try to improve here. I am a developer myself, but on Main Frame(programming in Assembler) and I know how difficult it ca be to understand what a customer wants. I will improve myself in this area.

Your comment b: you don't bother to take time to confirm whether the

suggestions you get in your postings are helpfull or not.

My answer: It did help me. Thanks...

So when it comes to my last posting, I search the net for the set.Background(Color.RED) and in non of the examples, the following statement was not included:

setOpaque(true);

Best regards

Tjoge01a at 2007-7-14 21:22:27 > top of Java-index,Java Essentials,Java Programming...
# 6
One question, was my last posting "swing" related or Java Programming related?
Tjoge01a at 2007-7-14 21:22:27 > top of Java-index,Java Essentials,Java Programming...
# 7
> One question, was my last posting "swing" related or Java Programming relatedYou are building a GUI with Swing components...
camickra at 2007-7-14 21:22:27 > top of Java-index,Java Essentials,Java Programming...
# 8
So, should it be posted under "Java programing" or "swing"? Your answer is a little bit unclear..
Tjoge01a at 2007-7-14 21:22:27 > top of Java-index,Java Essentials,Java Programming...
# 9

You are building a GUI with Swing components. Why would you ask the question in the general programming forum? People there may never have built a GUI or heard of Swing.

Concepts of creating GUI components adding then to a GUI, dealing with models, listening to events and responding to the events are all techniques related to GUI programming. Even Swing programming is different from AWT programming in some cases.

So when you topic title says. "I have a problem, with a Swing (JLabel) component", or "How do I change a JTable in a JScrollPane" where do you think the question should be asked. Maybe its just me but I would think the Swing forum is where you would find the experts, or at least people who think they know something about Swing.

Your previous posting on "display a button on a background image" was not very clear. You use an Applet and Panel which are an AWT components. But then you are also using JFrame and JButton which are Swing components. Since you posted the question in the general programming forum, I had no idea whether you where attempting to write an AWT or a Swing application so I ignored the posting.

camickra at 2007-7-14 21:22:27 > top of Java-index,Java Essentials,Java Programming...
# 10
Thanks for your reply. Next time I will use SWING forum .
Tjoge01a at 2007-7-14 21:22:27 > top of Java-index,Java Essentials,Java Programming...