Stretch a Jlabel Text

Stretch a JLabel text

In the approach shown below, the desired text is imaged using TextLayout using a suitably large Font size and scaled to fill the component. There's a related example here.

Sample Image

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.font.FontRenderContext;
import java.awt.font.TextLayout;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
import javax.swing.JLabel;

/** @see https://stackoverflow.com/questions/8281886 */
public class LayoutTest extends JLabel {

private static final int SIZE = 256;
private BufferedImage image;

private LayoutTest(String string) {
super(string);
image = createImage(super.getText());
}

@Override
public void setText(String text) {
super.setText(text);
image = createImage(super.getText());
repaint();
}

@Override
public Dimension getPreferredSize() {
return new Dimension(image.getWidth() / 2, image.getHeight() / 2);
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
}

private BufferedImage createImage(String label) {
Font font = new Font(Font.SERIF, Font.PLAIN, SIZE);
FontRenderContext frc = new FontRenderContext(null, true, true);
TextLayout layout = new TextLayout(label, font, frc);
Rectangle r = layout.getPixelBounds(null, 0, 0);
System.out.println(r);
BufferedImage bi = new BufferedImage(
r.width + 1, r.height + 1, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = (Graphics2D) bi.getGraphics();
g2d.setRenderingHint(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setColor(getBackground());
g2d.fillRect(0, 0, bi.getWidth(), bi.getHeight());
g2d.setColor(getForeground());
layout.draw(g2d, 0, -r.y);
g2d.dispose();
return bi;
}

private static void display() {
JFrame f = new JFrame("LayoutTest");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new LayoutTest("Sample"));
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {

@Override
public void run() {
display();
}
});
}
}

make a JLabel wrap it's text by setting a max width

No.

You can use HTML in the label, but then you must hard code the break tag.

A better approach is to use a JTextArea and turn wrapping on. You can change the background,foreground, font etc. of the text are to make it look like a label.

Note, this answer is outdated as of at least Java 7.

As per @darren's answer, you simply need to wrap the string with <html> and </html> tags:

myLabel.setText("<html>"+ myString +"</html>");

You do not need to hard-code any break tags. The text wraps as the component resizes.

Creating a class that extends Jlabel to draw a string to a panel. Nothing is currently being displayed

I am learning and experimenting…

It's not clear if you want to experiment with JLabel or custom painting. You might want to start with a working JLabel#setText() example or TextLayout#draw() example.

Java Swing - JLabel width changed when icon or text added?

Your component expands because it allocates the necessary space for its Icon.


public class JLabelDemo {
private static BufferedImage bi;

public static void main(String[] args) throws IOException{
loadImage();

SwingUtilities.invokeLater(new Runnable(){
@Override
public void run() {
createAndShowGUI();
}
});
}

private static void loadImage() throws IOException{
bi = ImageIO.read(JLabelDemo.class.getResource("../resource/forever-alone.jpg"));
}

private static void createAndShowGUI(){
final JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

final JPanel panel = new JPanel();
panel.setBackground(Color.YELLOW);
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
final JLabel emptyLabel = new JLabel();
final JLabel textLabel = new JLabel("This label has text only");
final JLabel textAndImageLabel = new JLabel("This label has text and image");
textAndImageLabel.setIcon(new ImageIcon(bi));

panel.add(emptyLabel);
panel.add(textLabel);
panel.add(textAndImageLabel);
frame.add(panel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
System.out.println("Empty label dimensions - " + emptyLabel.getSize());
System.out.println("Text only label dimensions - " + textLabel.getSize());
System.out.println("Image width: " + bi.getWidth() + ", Image height: " + bi.getHeight());
System.out.println("Text and image label dimensions - " +textAndImageLabel.getSize());
}
}

Sample Image


The following is outputted to console:

Empty label dimensions - java.awt.Dimension[width=0,height=0]
Text only label dimensions - java.awt.Dimension[width=129,height=16]
Image width: 194, Image height: 180
Text and image label dimensions - java.awt.Dimension[width=363,height=180]

How to change JLabel font size to fill JPanel free space while resizing?

ok, here is answer.

Let's take the code from here: How to change the size of the font of a JLabel to take the maximum size, answer from coobird

int componentWidth = label.getWidth();

We need to get width from the JFrame component, NOT from JLabel, because the code will no let opportunity to change size for the JLabel.

this will fix it:

int componentWidth = this.getWidth()-20; // '20' - according width of Jlabel to JFrame

Swing JLabel - strike through HTML text

As a continue of @Gaël answer, using css you can achive your desired result with the follwing code:

new JLabel("<html><body><span style='text-decoration: line-through;'>Text Here</span></body></html>");

Resize a picture to fit a JLabel

Outline

Here are the steps to follow.

  • Read the picture as a BufferedImage.
  • Resize the BufferedImage to another BufferedImage that's the size of the JLabel.
  • Create an ImageIcon from the resized BufferedImage.

You do not have to set the preferred size of the JLabel. Once you've scaled the image to the size you want, the JLabel will take the size of the ImageIcon.

Read the picture as a BufferedImage

BufferedImage img = null;
try {
img = ImageIO.read(new File("strawberry.jpg"));
} catch (IOException e) {
e.printStackTrace();
}

Resize the BufferedImage

Image dimg = img.getScaledInstance(label.getWidth(), label.getHeight(),
Image.SCALE_SMOOTH);

Make sure that the label width and height are the same proportions as the original image width and height. In other words, if the picture is 600 x 900 pixels, scale to 100 X 150. Otherwise, your picture will be distorted.

Create an ImageIcon

ImageIcon imageIcon = new ImageIcon(dimg);


Related Topics



Leave a reply



Submit