I wrote a subclass of JLabel, and I override paintComponent(Graphics)
method to make a gradient background color.
This is the subclass of JLabel:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.BorderFactory;
import javax.swing.JLabel;
public class DLabel extends JLabel
{
Dimension size = new Dimension(70, 80);
public DLabel()
{
this.setPreferredSize(size);
this.setBorder(BorderFactory.createBevelBorder(TOP, Color.white, Color.black));
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
Color color1 = new Color(226, 218, 145);
Color color2 = color1.brighter();
int w = getWidth();
int h = getHeight();
GradientPaint gp = new GradientPaint(
0, 0, color1, 0, h, color2);
g2d.setPaint(gp);
g2d.fillRect(0, 0, w, h);
}
}
When I create an instance of this label it is displayed correctly, but when I use setText(String)
the text is not rendered anything.
DLabel label = new DLabel();
label.setText("I am"); //No text displayed.
I tried different compilations of these methods after setting the text:
label.setOpaque(true);
label.revalidate();
label.repaint();
But nothing happed
getText() : returns the text that the label will display. setText(String s) : sets the text that the label will display to string s.
By default, labels are vertically centered in their display area. Text-only labels are leading edge aligned, by default; image-only labels are horizontally centered, by default. You can also specify the position of the text relative to the image.
JLabel(String text, Icon icon, int horizontalAlignment) Creates a JLabel instance with the specified text, image, and horizontal alignment.
paintComponent ONLY receives a Graphics object. See this link for a short tutorial. If you need to access other objects in this method, make them variables of GraphicalBoard and pass them om during construction.
What if you call the super.paintComponent(g) at the end of the method, so that the label draws the text after it draws the gradiant:
public void paintComponent(Graphics g) {
// super.paintComponent(g); // *** commented
Graphics2D g2d = (Graphics2D) g;
Color color1 = new Color(226, 218, 145);
Color color2 = color1.brighter();
int w = getWidth();
int h = getHeight();
GradientPaint gp = new GradientPaint(0, 0, color1, 0, h, color2);
g2d.setPaint(gp);
g2d.fillRect(0, 0, w, h);
super.paintComponent(g); // *** added
}
Also, as an unrelated aside, I prefer to change this:
Dimension size = new Dimension(70, 80);
public DLabel()
{
this.setPreferredSize(size);
this.setBorder(BorderFactory.createBevelBorder(TOP, Color.white,
Color.black));
}
to this:
public static final Dimension PREF_SIZE = new Dimension(70, 80);
public DLabel()
{
this.setBorder(BorderFactory.createBevelBorder(TOP, Color.white,
Color.black));
}
@Override
public Dimension getPreferredSize() {
Dimension superDim = super.getPreferredSize();
int width = Math.max(superDim.getWidth(), PREF_SIZE.getWidth());
int height = Math.max(superDim.getHeight(), PREF_SIZE.getHeight());
return new Dimension(width, height);
}
JLabel
renders its text content within the paintComponent
method.
You are, correctly, calling super.paintComponent
, but are then promptly painting over it, using fillRect
Try moving the call to super.paintComponent
to the end of the method (after the fillRect
call) and leave the label as transparent
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