9

Is there a way I can define my own font and color schemes for Text1 AND Text2 text within the setBorder method. New to java and cannot find it in the SUN tutorials.

My code

//Create Positions Table
 JPanel SpreadPanel = new JPanel();
 SpreadPanel.setBorder(BorderFactory.createTitledBorder(" Text 1    Text 2"));

Regards Simon

1

6 Answers 6

8
setBorder(BorderFactory.createTitledBorder(null, "text", TitledBorder.CENTER, TitledBorder.BOTTOM, new Font("times new roman",Font.PLAIN,12), Color.yellow));

the first parameter null or another border (for compound borders) 2nd param text that you're displaying 3rd and 4th param justification and location of the text from param 2

4th param and 5th param are the two to set font and color

3

If you want a different font and color for each of the strings (e.g. Text1 and Text2) in the same TitledBorder, you may be need to extend AbstractBorder and override paintBorder(). The existing implementation only has one font and one color for a single title.

0
1

Text Font:

((javax.swing.border.TitledBorder) panel_1.getBorder()).setTitleFont(new Font("Tahoma", Font.PLAIN, 20));

Text Color:

((javax.swing.border.TitledBorder)panel_1.getBorder()).setTitleColor(Color.WHITE);
0

The JavaDocs for doing this are somewhat overwhelming if you are new to Java and Swing. The JavaDocs for BorderFactory are here: http://download.oracle.com/javase/1.5.0/docs/api/javax/swing/BorderFactory.html

Here's an example of making the text red in a sans serif font:

import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.io.IOException;

public class ScratchSpace {

    public static void main(String[] args) throws IOException {
        Font myFont = new Font("SansSerif", Font.PLAIN, 10);
        Color myColor = Color.RED;
        TitledBorder titledBorder = BorderFactory.createTitledBorder(null, " Text 1    Text 2", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, myFont, myColor);
        JFrame frame = new JFrame();
        final JLabel label = new JLabel("Hello gruel world");
        label.setBorder(titledBorder);
        frame.getContentPane().add(label);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

}
0

I know it is an old question. Thought I would like to resurrect it as maybe someone knows how to solve this problem. I have only 'a partial solution'.

I have very quickly implemented the border which does what you want. I have reused what Java gives, i.e. interpretation of HTML in swing components.

All works sweet, the border is painted fine for a plain or HTML text, with exception for a situation where you are trying to have different font sizes for the texts.

I do not have idea how to solve this issue. But I am very much interested in a solution.

I know the procedure would be to sum up width of each string in its own font size when calculating the textLengthInPixels variable.

The problem is that I do not know how to get it, maybe from the View, but no idea how?



import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.Rectangle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.AbstractBorder;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;
import javax.swing.text.BadLocationException;
import javax.swing.text.View;

public class MultiColorTitleBorder extends AbstractBorder
{
    private static final long serialVersionUID = 1L;
    private JLabel label;
    private int thicknessTop = 10;
    private Border border;
    private int thicknessLeft = 0;
    private int thicknessRight = 0;
    private int thicknessBottom = 0;

    public MultiColorTitleBorder(String title)
    {
        this.label = new JLabel(title);
        thicknessTop = label.getPreferredSize().height;
    }

    public MultiColorTitleBorder(String title, Border border)
    {
        this(title);
        this.border = border;
        thicknessLeft = border.getBorderInsets(null).left;
        thicknessRight = border.getBorderInsets(null).right;
        thicknessBottom = border.getBorderInsets(null).bottom;
    }

    @Override
    public synchronized void paintBorder(Component c, Graphics g, int x, int y, int width, int height)
    {
        Graphics2D g2 = (Graphics2D) g;
        View view = (View) label.getClientProperty("html");
        String text = label.getText();
        FontMetrics fm = g2.getFontMetrics(label.getFont());
        int bY = y + fm.getAscent() - ((fm.getAscent() + fm.getDescent())) / 2;

        if(border != null)
        {
            Insets in = border.getBorderInsets(c);
            g2.setClip(x, y, thicknessLeft * 2, height);
            border.paintBorder(c, g, x, bY, width, height - bY);
            try
            {
                if(view != null)
                    text = view.getDocument().getText(0, view.getDocument().getLength());
            }catch(BadLocationException ex)
            {
                Logger.getLogger(MultiColorTitleBorder.class.getName()).log(Level.SEVERE, null, ex);
            }
            int textLengthInPixels = fm.stringWidth(text);
            System.out.println("textLengthInPixels=" + textLengthInPixels);
            g2.setClip(x +thicknessLeft * 2+ textLengthInPixels, y, width - thicknessLeft * 2 -textLengthInPixels, height);
            border.paintBorder(c, g, x, bY, width, height - bY);
            int bottomIn = in.bottom;
            g2.setClip(x, height - bottomIn, width, bottomIn);
            border.paintBorder(c, g, x, bY, width, height - bY);
            g2.setClip(x, y, width, height);
        }
        if(view != null)
            view.paint(g2, new Rectangle(x + thicknessLeft * 2, y, width - thicknessLeft * 2, height));
        else
        {
            Font prevFont = g2.getFont();
            g2.setFont(label.getFont());
            g2.drawString(text, x + thicknessLeft * 2, fm.getAscent());
            g2.setFont(prevFont);
        }
    }

    @Override
    public Insets getBorderInsets(Component c)
    {
        return new Insets(thicknessTop, thicknessLeft, thicknessBottom, thicknessRight);
    }

    @Override
    public Insets getBorderInsets(Component c, Insets insets)
    {
        insets.top = thicknessTop;
        insets.left = thicknessLeft;
        insets.right = thicknessRight;
        insets.bottom = thicknessBottom;
        return insets;
    }

    @Override
    public boolean isBorderOpaque()
    {
        return false;
    }

    public static void main(String[] args)
    {
        JPanel p = new JPanel();
        p.setPreferredSize(new Dimension(200, 200));
        String title = "<html><color=red> Text 1</font><font color=blue>      Text 2</font>";
        //title = "<html><font color=red font size=5> Text 1</font><font color=blue>      Text 2</font>";
        //title = "Text 1   Text 2";

        p.setBorder(new MultiColorTitleBorder(title, new LineBorder(Color.CYAN, 6)));
        p.setBackground(Color.YELLOW);
        p.add(new JTextField(5));
        JPanel contentPane = new JPanel();
        contentPane.add(p);
        JFrame f = new JFrame();
        f.setContentPane(contentPane);
        f.setSize(800, 600);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
    }
}

0

Try this:

.setBorder(UIManager.getBorder("TextField.border"));
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.