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
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.
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:
TitledBorder titled = BorderFactory.createTitledBorder("\u2615");
titled.setTitleFont(new Font(Font.Dialog, Font.PLAIN, 32));
titled.setTitleColor(Color.blue);
label.setBorder(titled);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With