Java font settings / color in setBorder

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

My code

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

Relationship Simon

+7
source share
6 answers
 setBorder(BorderFactory.createTitledBorder(null, "text", TitledBorder.CENTER, TitledBorder.BOTTOM, new Font("times new roman",Font.PLAIN,12), Color.yellow)); 

first parameter is null or another border (for compound borders) The text of the second paragraph, which you show the 3rd and 4th value of the parameter and the location of the text from param 2

The 4th parameter and the 5th parameter are two for setting the font and color

+4
source

If you need a different font and color for each line (for example, Text1 and Text2 ) in the same TitledBorder , you may need the AbstractBorder extension and override paintBorder() . In the existing implementation, there is only one font and one color for one header.

+3
source

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); 
+1
source

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

Here is an example of how the text will be 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
source

I know this is an old question. I think I would like to resurrect it, because maybe someone knows how to solve this problem. I have only a "partial solution".

I very quickly implemented a border that does what you want. I reused what Java gives, i.e. Interpretation of HTML in swing components.

Everything works sweetly, the border is written perfectly for plain or HTML text, excluding the situation when you try to have different font sizes for texts.

I have no idea how to solve this problem. But I am very interested in the solution.

I know that the procedure will be to sum the width of each line in its own font size when calculating the variable textLengthInPixels.

The problem is that I don’t know how to get it, perhaps from a view, but I don’t know 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&gt"; //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
source

Try the following:

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

All Articles