themorfeus
themorfeus

Reputation: 287

Custom JLabel class breaks positioning of JLabel

I've created a class extending JLabel, as normally it is not antialiased. But, when i use it, whole positioning of JLabel is broken. How is that possible? Here's the code for the class:

private class AntialiasedJLabel extends JLabel{

    public AntialiasedJLabel(){}

    public AntialiasedJLabel(String name){
        this.setText(name);
    }


    public void paintComponent(Graphics g){
        ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
                RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        g.drawString(getText(),getX(),getY());
    }
}

Here's the result using normal JLabel: enter image description here

And here using AntialiasedJLabel: enter image description here

What could be the problem here?

Upvotes: 1

Views: 87

Answers (1)

camickr
camickr

Reputation: 324108

Don't do the custom painting yourself. Instead do:

Graphics g2 = g.create();
g2.setRenderingHint(...);

super.paintComponent(g2);

g2.dispose();

Upvotes: 3

Related Questions