Sathish Robert
Sathish Robert

Reputation: 187

set background image in Jlabel (Label) box with dynamic text

How to set background image in label box with dynamic text.

for example:

JLabel label = new JLabel();
label.setIcon("/image/image.png");
label.setText("Text1");

This code not working, because The text is not display in in center of image.

Upvotes: 2

Views: 31828

Answers (4)

Vishakha Pol
Vishakha Pol

Reputation: 80

Use this code

label.setIcon(new ImageIcon("background.png"));
label.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
label.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);

Using this icon align center as background to JLabel

Upvotes: 0

camickr
camickr

Reputation: 324128

You haven't defined how the text should be painted in relation to the image. Here are 4 different approaches that don't involve custom painting:

import java.awt.*;
import javax.swing.*;
import javax.swing.text.*;

public class LabelImageText extends JPanel
{
    public LabelImageText()
    {
        JLabel label1 = new JLabel( new ColorIcon(Color.ORANGE, 100, 100) );
        label1.setText( "Easy Way" );
        label1.setHorizontalTextPosition(JLabel.CENTER);
        label1.setVerticalTextPosition(JLabel.CENTER);
        add( label1 );

        //

        JLabel label2 = new JLabel( new ColorIcon(Color.YELLOW, 200, 150) );
        label2.setLayout( new BoxLayout(label2, BoxLayout.Y_AXIS) );
        add( label2 );

        JLabel text = new JLabel( "More Control" );
        text.setAlignmentX(JLabel.CENTER_ALIGNMENT);
        label2.add( Box.createVerticalGlue() );
        label2.add( text );
        label2.add( Box.createVerticalStrut(10) );

        //

        JLabel label3 = new JLabel( new ColorIcon(Color.GREEN, 200, 150) );
        add( label3 );

        JLabel text3 = new JLabel();
        text3.setText("<html><center>Text<br>over<br>Image<center></html>");
        text3.setLocation(20, 20);
        text3.setSize(text3.getPreferredSize());
        label3.add( text3 );

        //

        JLabel label4 = new JLabel( new ColorIcon(Color.CYAN, 200, 150) );
        add( label4 );

        JTextPane textPane = new JTextPane();
        textPane.setText("Add some text that will wrap at your preferred width");
        textPane.setEditable( false );
        textPane.setOpaque(false);
        SimpleAttributeSet center = new SimpleAttributeSet();
        StyleConstants.setAlignment(center, StyleConstants.ALIGN_CENTER);
        StyledDocument doc = textPane.getStyledDocument();
        doc.setParagraphAttributes(0, doc.getLength(), center, false);
        textPane.setBounds(20, 20, 75, 100);
        label4.add( textPane );
    }

    public static class ColorIcon implements Icon
    {
        private Color color;
        private int width;
        private int height;

        public ColorIcon(Color color, int width, int height)
        {
            this.color = color;
            this.width = width;
            this.height = height;
        }

        public int getIconWidth()
        {
            return width;
        }

        public int getIconHeight()
        {
            return height;
        }

        public void paintIcon(Component c, Graphics g, int x, int y)
        {
            g.setColor(color);
            g.fillRect(x, y, width, height);
        }
    }

    private static void createAndShowUI()
    {
        JFrame frame = new JFrame("LabelImageText");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add( new LabelImageText() );
        frame.pack();
        frame.setLocationRelativeTo( null );
        frame.setVisible( true );
    }

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

Upvotes: 4

Stephan
Stephan

Reputation: 4443

Thats very easy.

import javax.swing.*;


public class YourFrame extends JFrame {

  private ImageIcon icon;
  private JLabel label;


  public YourFrame(){

    icon = new ImageIcon("/image/image.png");

    label= new JLabel() {
      public void paintComponent(Graphics g) {
        g.drawImage(icon.getImage(), 0, 0, null);
        super.paintComponent(g);
      }
    };

    label.setOpaque(false);
    getContentPane().add( label );

    label.setText("Text1");

  }

  public static void main(String[] args) {
    YourFrame frame = new YourFrame();

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
  }
} 

Upvotes: 4

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

There are several options available. For instance, you can place the JLabel on top of another JLabel (though give the container JLabel a viable layout manager first), or you can draw the image directly in the JLabel's paintComponent method. If you do the latter, be sure the JLabel is set to be opaque.

Upvotes: 2

Related Questions