0

I have a Java Swing application. I want to insert a JLabel with png image as background and I want insert another JLabel at the center of this image. So I have build this code:

ImageIcon icon = new ImageIcon(getClass().getResource("/resources/cornometro_white.png"));

labelSfondoTimer = new JLabel(icon);
labelTempoGara = new JLabel();
labelTempoGara.text("pippo");
GridBagConstraints GBC2 = new GridBagConstraints();
Container CR2 = new Container();
GridBagLayout GBL2 = new GridBagLayout();
CR2.setComponentOrientation(ComponentOrientation.UNKNOWN);
CR2.setLayout(GBL2);     
labelSfondoTimer.add(CR2);

GBC2 = new GridBagConstraints();
CR2.add(labelTempoGara);
GBC2.gridx=2;
GBC2.gridy=0;
GBC2.anchor= GridBagConstraints.CENTER;
GBL2.setConstraints(labelTempoGara,GBC2);

With this code, I can see the image on the screen but I can't see the secondo Label with text "pippo"

I have try also to insert this code:

labelSfondoTimer.setObaque(true); but not works.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
bircastri
  • 2,169
  • 13
  • 50
  • 119
  • 1
    JLabel hasn't any LayoutManager in API, JLabel has method for centering, JLabel is transparent by default, GBC without any constraints centering JComponent by default – mKorbel Mar 03 '16 at 10:21
  • So, how can i fixed my error? – bircastri Mar 03 '16 at 10:28
  • @bircastri See if this helps: [Image which overlaps](http://stackoverflow.com/questions/35711891/how-to-create-a-background-and-foreground-image-which-overlaps/35711894#35711894) – user3437460 Mar 03 '16 at 11:00

2 Answers2

5

So, how can i fixed my error? - so, for example printscreens (based on my comment in your question - JLabel hasn't any LayoutManager in API, JLabel has method for centering, JLabel is transparent by default, GBC without any constraints centering JComponent by default)

enter image description here . enter image description here . enter image description here

from code in SSCCE / MCVE form

import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.UIManager;

public class JLabelInJLabel {

    private JFrame frame = new JFrame();
    private Icon errorIcon = UIManager.getIcon("OptionPane.errorIcon");
    private Icon infoIcon = UIManager.getIcon("OptionPane.informationIcon");
    private Icon warnIcon = UIManager.getIcon("OptionPane.warningIcon");
    private JLabel parentJLabel = new JLabel();
    private JLabel childJLabel = new JLabel(infoIcon, SwingConstants.CENTER);
    private JLabel imageInJLabel = new JLabel(errorIcon, SwingConstants.CENTER);
    private JLabel simpleJLabel = new JLabel(warnIcon);

    public JLabelInJLabel() {
        parentJLabel.setLayout(new GridBagLayout());
        parentJLabel.add(childJLabel);
        parentJLabel.setBorder(BorderFactory.createLineBorder(java.awt.Color.RED));        
        imageInJLabel.setBorder(BorderFactory.createLineBorder(java.awt.Color.BLUE));        
        simpleJLabel.setBorder(BorderFactory.createLineBorder(java.awt.Color.ORANGE));        
        frame.setLayout(new GridLayout(0, 1));
        frame.add(parentJLabel);
        frame.add(imageInJLabel);
        frame.add(simpleJLabel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocation(100, 100);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(() -> new JLabelInJLabel());
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • ok but my answer is, how can I insert one PNG image and display a dynamic text ? – bircastri Mar 03 '16 at 13:08
  • (more than I'm sure) that `dynamic text is` posted here a few times in posts by @Hovercraft Full Of Eels, then there is not some reason to reinvent the wheel in this case and in this thread – mKorbel Mar 03 '16 at 13:43
3

I want to insert a JLabel with png image as background and I want insert another JLabel at the center of this image

You can have text and an Icon on the same label:

JLabel label1 = new JLabel( new ImageIcon(...) );
label1.setText( "Centered Text" );
label1.setHorizontalTextPosition(JLabel.CENTER);
label1.setVerticalTextPosition(JLabel.CENTER);

The text will be centered over the image.

You can also check out this posting: JButton settext specific position, which contain other approaches that give more flexibility with the text location.

Community
  • 1
  • 1
camickr
  • 321,443
  • 19
  • 166
  • 288