user2262230
user2262230

Reputation: 199

Label inside Circle

I want to put text ( must be formatted with HTML, so I can't use drawString in PaintComponent) inside Circle. Problem is that "paintComponent" is called after drawing label, so it covers my text.

How to draw oval at the beginning and then draw my String?

class Circle extends JLabel 
{

         public Circle(String string) {         super(string);  }
     @Override
    public void paintComponent( Graphics g )
    {
        super.paintComponent(g);
        g.setColor(Color.yellow);
        g.fillOval(0,0, 70, 70);
        g.setColor(Color.blue);
       g.drawOval(0,0, 70, 70); 
    }
 }

Upvotes: 0

Views: 1867

Answers (3)

Andrew Thompson
Andrew Thompson

Reputation: 168825

Consider putting the component inside a custom border. See TextBubbleBorder for ideas.

Upvotes: 1

david a.
david a.

Reputation: 5291

Probably the quickest solution is to change your paintComponent to

public void paintComponent( Graphics g )
{
  g.setColor(Color.yellow);
  g.fillOval(0,0, 70, 70);
  g.setColor(Color.blue);
  g.drawOval(0,0, 70, 70); 
  super.paintComponent(g);
}

I'd however also consider composition rather than inheritance in this case. Perhaps define another component class composed of the label and a panel with the circle.

Upvotes: 1

gavlaaaaaaaa
gavlaaaaaaaa

Reputation: 652

I would try using setComponentZOrder() to set the labels order to be higher up than the circle.

Upvotes: 0

Related Questions