0

I'm using Netbeans 8.0 and I'm trying to create a graphical interface for a Java program. I have a large background image (a .png, 2000x1500 px, for instance) that needs a scrollbar, and I want to put position multiple text labels on top of the background image.

To put it differently, what I am attempting to do is position a JLabel (text) on top of another JLabel (image) and enclose the entire thing in a JScrollPane, like this.

I've tried many different layouts (including null layout and free design) with no success. Netbeans is only allowing me to enclose one JLabel in a JScrollPane, but not both. Am I using the correct approach?

alex
  • 6,818
  • 9
  • 52
  • 103
  • 1
    For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete and Verifiable Example). – Andrew Thompson Mar 28 '14 at 21:03
  • As HovercraftFullOfEeels said. Set the layout of the background jlabel to something _other_ than (its default) null layout. _Then_ you'll be able to add (drag and drop) other components onto the background label. You can see how to set the layout with the design tool in [this post](http://stackoverflow.com/q/22214202/2587435), which also has an alternative to using a label, which is using a panel for the background. – Paul Samsotha Mar 28 '14 at 21:12

1 Answers1

4

If you want the one label stuck on the other label, then give the background label a layout and add the overlay JLabel onto it.

For example:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.*;

public class OverlayingLabels extends JPanel {
   private static final String IMAGE_PATH = "https://duke.kenai.com/italian/BouncingDuke.gif";

   public OverlayingLabels() {
      setLayout(new BorderLayout());
      try {
         URL imageUrl = new URL(IMAGE_PATH);
         BufferedImage image = ImageIO.read(imageUrl);
         ImageIcon icon = new ImageIcon(image);

         JLabel bigLabel = new JLabel(icon);
         bigLabel.setLayout(new FlowLayout());

         JLabel littleLabel = new JLabel("Little Label");
         littleLabel.setFont(littleLabel.getFont().deriveFont(Font.BOLD, 36));
         littleLabel.setForeground(Color.YELLOW);

         // ****** here we add the little label to the big label ******
         bigLabel.add(littleLabel);

         JScrollPane scrollPane = new JScrollPane(bigLabel);
         scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
         int width = bigLabel.getPreferredSize().width;
         int height = bigLabel.getPreferredSize().height / 2;
         scrollPane.getViewport().setPreferredSize(new Dimension(width, height));
         add(scrollPane);

      } catch (IOException e) {
         e.printStackTrace();
      }
   }

   private static void createAndShowGui() {
      OverlayingLabels mainPanel = new OverlayingLabels();

      JFrame frame = new JFrame("OverlayingLabels");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • Hey, thanks for the quick response, but my problem is with enclosing both of them in a JScrollPane. Netbeans only allows me to enclose one of them in a JScrollPane, but not both. – alex Mar 28 '14 at 21:05
  • @user3474146: **Again**, if you add one to the other, and then add the background JLabel to the JScrollPane's viewport, you ***are*** adding both of them to the JScrollPane. I think that you're missing the point of adding the one JLabel to the other. – Hovercraft Full Of Eels Mar 28 '14 at 21:06
  • Yes, I am unclear on the point. I stuck one JLabel on top of the other in free design layout, and I can't enclose both of them in the JScrollPane: http://i.imgur.com/nJPk7sr.png If you could elaborate a little bit on what you mean by "adding one JLabel to the other", or the appropriate layout to use, I would be greatly appreciative. – alex Mar 28 '14 at 21:26
  • @user3474146: you use whatever layout works best for you. For example in the code I've posted above, I use a simple FlowLayout. – Hovercraft Full Of Eels Mar 28 '14 at 21:37