JLabel: After overriding paintComponent (). How to make setText () render string text?

I wrote a subclass of JLabel, and I override the paintComponent(Graphics) method to make the background color of the gradient.

This is a 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 shortcut, it displays correctly, but when I use setText(String) , the text does not display 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 happened

+4
source share
3 answers

What if you call super.paintComponent (g) at the end of the method so that the label draws text after drawing the gradient:

 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 irrelevant, 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)); } 

:

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

JLabel displays text content in the paintComponent method.

You, correctly, call super.paintComponent , but then quickly draw over it using fillRect

Try transferring the call to super.paintComponent to the end of the method (after calling fillRect ) and leave the label transparent

+4
source

When you made the paint, she drew the text. Take a look at the SwingX project. It has a JxLabel class that will do exactly what you want.

+4
source

All Articles