Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Icon in titledBorder title

Tags:

java

swing

Hi is it possible to place an icon in the title of a titledBorder for example the following code:


import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;

public class TitledExample extends JPanel {

  public TitledExample() {
    super(true);

    this.setLayout(new GridLayout(1, 1, 5, 5));

    JLabel label = new JLabel("Titled Border");
    label.setHorizontalAlignment(JLabel.CENTER);

    TitledBorder titled = new TitledBorder("Image here ?? Title");
    label.setBorder(titled);

    add(label);
  }

Thanks , Cheers

like image 744
greenLizard Avatar asked Mar 25 '10 20:03

greenLizard


2 Answers

Try subclassing TitledBorder, and override the paintBorder method:

 @Override
 public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) 
 {
     super.paintBorder(c, g, x, y, width, height);

     // Now use the graphics context to draw whatever needed
     g.drawImage(img, xImageOffset, yImageOffset, imgWidth, imgHeight, observer);
 }

Not desperately sure that this is entirely right method call, but you get the idea; once you have access to the Graphics object, you can paint pretty much whatever you need.

like image 164
rhu Avatar answered Oct 19 '22 05:10

rhu


It's probably not what you want, but maybe a nice Unicode™ glyph or two would do.

Addendum: @rhu's approach is preferable, but I couldn't resist trying this:

enter image description here

TitledBorder titled = BorderFactory.createTitledBorder("\u2615");
titled.setTitleFont(new Font(Font.Dialog, Font.PLAIN, 32));
titled.setTitleColor(Color.blue);
label.setBorder(titled);
like image 34
trashgod Avatar answered Oct 19 '22 07:10

trashgod