This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes JComponent Color Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "JComponent Color" Watch "JComponent Color" New topic

JComponent Color

KunkalaGuntala Samba Siva Rao

Joined: Nov 06, 2003
Posts: 20
I am trying to create a custom Label component
I am unable to find out
1. why this changes the default look and feel font color to black
2. though i set the color using setColor explicitly, it doesn't seem to change
thank you
import java.awt.*;
import java.awt.font.*;
import java.text.*;
import javax.swing.*;
public class JMultilineLabel extends JComponent
private String text;
private Color color;
private Insets margin = new Insets(0,0,0,0);
private int maxWidth = Integer.MAX_VALUE;
private boolean justify;
private final FontRenderContext frc = new FontRenderContext(null, false, false);
private void morph()
public String getText()
return text;
public void setText(String text)
String old = this.text;
this.text = text;
firePropertyChange("text", old, this.text);
if ((old == null) ? text!=null : !old.equals(text))
public void setColor(Color color) {
Color old = this.getForeground();
firePropertyChange("foreground", old, this.getForeground());
if (!color.equals(this.getForeground()))
public int getMaxWidth()
return maxWidth;
public void setMaxWidth(int maxWidth)
if (maxWidth <= 0)
throw new IllegalArgumentException();
int old = this.maxWidth;
this.maxWidth = maxWidth;
firePropertyChange("maxWidth", old, this.maxWidth);
if (old != this.maxWidth)
public boolean isJustified()
return justify;
public void setJustified(boolean justify)
boolean old = this.justify;
this.justify = justify;
firePropertyChange("justified", old, this.justify);
if (old != this.justify)
public Dimension getPreferredSize()
return paintOrGetSize(null, getMaxWidth());
public Dimension getMinimumSize()
return getPreferredSize();
protected void paintComponent(Graphics g)
paintOrGetSize((Graphics2D)g, getWidth());
private Dimension paintOrGetSize(Graphics2D g, int width)
Insets insets = getInsets();
width -= insets.left + insets.right + margin.left + margin.right;
float w = insets.left + insets.right + margin.left + margin.right;
float x = insets.left + margin.left, +;
if (width > 0 && text != null && text.length() > 0)
AttributedString as = new AttributedString(getText());
as.addAttribute(TextAttribute.FONT, getFont());
AttributedCharacterIterator aci = as.getIterator();
LineBreakMeasurer lbm = new LineBreakMeasurer(aci, frc);
float max = 0;
while (lbm.getPosition() < aci.getEndIndex())
TextLayout textLayout = lbm.nextLayout(width);
if (g != null && isJustified() && textLayout.getVisibleAdvance() > 0.80 * width) textLayout = textLayout.getJustifiedLayout(width); if (g != null) textLayout.
I agree. Here's the link:
subject: JComponent Color
Similar Threads
Text canvas vertical scroll
Printing Database Records to an external printer/paper?
LineBreakMeasurer and Fonts
JComponent Color
Creating a image file using TextLayout. Need to find right startYpoint