alex
alex

Reputation: 7471

Enclosing two JLabels (one on top of the other) in a JScrollPane

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?

Upvotes: 0

Views: 1089

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285430

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();
         }
      });
   }
}

Upvotes: 4

Related Questions