JLabel not displaying text

Hello i have a problem displaying properly my JLabel here is the code for my class extending JLabel :

publicclass PanneauScoreextends JLabel

{

privateint score = 0;

/** Creates a new instance of PanneauScore */

public PanneauScore()

{

this.setText("score = " + score);

}

publicvoid incrementerScore(int y)

{

score += y;

this.setText("score = " + score);

}

publicvoid paintComponent(Graphics g)

{

((Graphics2D)g).setRenderingHint(RenderingHints.KEY_ANTIALIASING,

RenderingHints.VALUE_ANTIALIAS_ON);

g.setColor(Color.MAGENTA);

g.drawRect(0, 0, 80, 20);

}

}

and here is where i use this class :

publicclass MyFrameextends JFrame

{

private JPanel backgroundPanel;

private PanneauScore unPanneauDeScore;

/** Creates a new instance of MyFrame */

public MyFrame()

{

Runnable runner =new Runnable()

{

publicvoid run()

{

backgroundPanel =new JPanel()

{

publicvoid paintComponent(Graphics g)

{

((Graphics2D)g).setRenderingHint(RenderingHints.KEY_ANTIALIASING,

RenderingHints.VALUE_ANTIALIAS_ON);

g.setColor(Color.YELLOW);

g.fillRect(360, 320, 30, 30);

g.setColor(Color.RED);

g.fillOval(360, 320, 30, 30);

g.setColor(Color.BLACK);

g.fillOval(0, 80, 30, 30);

g.setColor(Color.RED);

g.drawOval(2, 120, 26, 26);

g.drawOval(0, 118, 30, 30);

g.drawString("100", 2, 140);

g.drawOval(2, 160, 26, 26);

g.drawOval(0, 158, 30, 30);

g.drawString("200", 2, 180);

g.drawOval(0, 200, 30, 30);

g.drawString("100", 2, 220);

g.drawOval(0, 240, 30, 30);

g.drawString("200", 0, 260);

g.setColor(Color.GREEN);

g.fillRect(0, 280, 30, 5);

g.drawLine(0, 283, 30, 285);

g.drawLine(30, 285, 0, 287);

g.drawLine(0, 287, 30, 289);

g.drawLine(30, 289, 0, 291);

}

};

setLayout(new BorderLayout());

unPanneauDeScore =new PanneauScore();

getContentPane().add(unPanneauDeScore, BorderLayout.NORTH);

getContentPane().add(backgroundPanel, BorderLayout.CENTER);

addWindowListener(new WindowAdapter()

{

publicvoid windowClosing(WindowEvent e)

{

System.exit(0);

}

});

setSize(400, 400);

setVisible(true);

}

};

SwingUtilities.invokeLater(runner);

}

publicstaticvoid main(String[] args)

{

MyFrame frame =new MyFrame();

}

}

thank you for help

[4615 byte] By [JusteUneQuestiona] at [2007-11-27 6:15:22]
# 1
Because you're overriding the label's paintComponent, you should call super.paintComponent(g)
dwga at 2007-7-12 17:25:53 > top of Java-index,Desktop,Core GUI APIs...
# 2
Hello and thanks, so everytime i overide paintComponent i have to make a call to super.paintComponent ? If not in which case do i have to make a call to super and in which case i dont need to ?Thanks
JusteUneQuestiona at 2007-7-12 17:25:53 > top of Java-index,Desktop,Core GUI APIs...
# 3

> Hello and thanks, so everytime i overide

> paintComponent i have to make a call to

> super.paintComponent ? If not in which case do i

> have to make a call to super and in which case i dont

> need to ?

>

um,... any time you want super's paint functionality, you must call it, and anytime you don't, you don't call it.

In this case, since you are not recreating a label de-novo, without super.paintComponent(g), how will your app know how to draw the rudiments of a label?

petes1234a at 2007-7-12 17:25:53 > top of Java-index,Desktop,Core GUI APIs...
# 4
ok its logic, and if i had made a constructor with a string argument and made a call to super(label) would i still have to call super.paintComponent() ?It maybe basic questions but i need to make it clear, thanks.
JusteUneQuestiona at 2007-7-12 17:25:53 > top of Java-index,Desktop,Core GUI APIs...
# 5

> ok its logic, and if i had made a constructor with a

> string argument and made a call to super(label) would

> i still have to call super.paintComponent() ?

> It maybe basic questions but i need to make it clear,

> thanks.

I'm not sure what you are asking here, but if you are subclassing a graphical component and overriding its paintComponent method, and if you want the component's innate paint functionality, (I think -- I'm not a Swing guru) you must call super.paintComponent(g). It doesn't matter what constructor your class uses.

If I'm wrong, please someone correct this.

Message was edited by:

petes1234

petes1234a at 2007-7-12 17:25:53 > top of Java-index,Desktop,Core GUI APIs...
# 6
The real question is why are you overriding the paintComponent() method in the first place.If you are simply trying to paint a rectangle around the label then use a Border. How do you know what the size of the label should be. Hardcoding the width and height is not a good idea.
camickra at 2007-7-12 17:25:53 > top of Java-index,Desktop,Core GUI APIs...
# 7

> The real question is why are you overriding the

> paintComponent() method in the first place.

>

> If you are simply trying to paint a rectangle around

> the label then use a Border. How do you know what the

> size of the label should be. Hardcoding the width and

> height is not a good idea.

i used paintComponent caused it seems easier to create a border this way. I have my border in two lines of code. But i havent really looked at the Border class maybe it also requires 2 lines of code to do a border in any color i want.

JusteUneQuestiona at 2007-7-12 17:25:53 > top of Java-index,Desktop,Core GUI APIs...
# 8

> i used paintComponent caused it seems easier to create a border

> this way. I have my border in two lines of code.

Its more than two lines of code. First you have to override the paintComponent() method. Then you need to invoke the super.paintComponent(...) method. Not only that the JVM needs to create another object every time you subclass an object which is what you are doing by overriding the method.

> But i havent really looked at the Border class maybe it also requires 2

> lines of code to do a border in any color i want.

Why do you think they would go to all the trouble of create a Border API if it was easier to extend a component every time you wanted to add a Border to a component? Its one line of code.

camickra at 2007-7-12 17:25:53 > top of Java-index,Desktop,Core GUI APIs...
# 9
hm...much much simpler, thanks..
JusteUneQuestiona at 2007-7-12 17:25:53 > top of Java-index,Desktop,Core GUI APIs...