0

I am designing an application in Netbeans using CardLayout & I want to use the same background for all of my JPanel objects.

I have already tried to accomplish what I want using the method discussed in How to add an image to a JPanel?, however I haven't figured out how to use the same image for all of my panels.

Community
  • 1
  • 1
Ali Zakeri
  • 57
  • 10
  • 2
    Please [edit] to add meaningful code and a problem description here. Posting a [Minimal, Complete, Verifiable Example](http://stackoverflow.com/help/mcve) that demonstrates your problem would help you get better answers. Thanks! – Jaime Gómez Mar 07 '15 at 06:01

1 Answers1

4

How to set a single background for all panels in CardLayout..

Make them all transparent.

enter image description here

This image is a picture of a coal mine ..at midnight.

import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import javax.swing.*;

public class BackgroundImageForCardLayout {

    private JComponent ui = null;
    private Image image= new BufferedImage(400,300,BufferedImage.TYPE_INT_RGB);

    BackgroundImageForCardLayout() {
        initUI();
    }

    public void initUI() {
        if (ui!=null) return;

        ui = new JPanel() {
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.drawImage(image, 0, 0, getWidth(), getHeight(), this);
            }
        };

        final CardLayout cl = new CardLayout();
        ui.setLayout(cl);

        final String[] names = {"Card 1", "Card 2", "Card 3"};
        for (String name : names) {
            JPanel p = new JPanel(new GridLayout());
            p.setOpaque(false);
            p.add(getBigColoredLabel(name));
            p.setOpaque(false);
            ui.add(p, name);
        }
        ActionListener animationListener = new ActionListener() {

            int i=0;

            @Override
            public void actionPerformed(ActionEvent e) {
                String name = names[i%names.length];
                cl.show(ui, name);
                i++;
            }
        };
        Timer timer = new Timer(750, animationListener);
        timer.start();
    }

    private final JLabel getBigColoredLabel(String text) {
        JLabel l = new JLabel(text, SwingConstants.CENTER);
        l.setForeground(Color.RED);
        Font f = l.getFont();
        l.setFont(f.deriveFont(120f));
        return l;
    }

    public JComponent getUI() {
        return ui;
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(
                            UIManager.getSystemLookAndFeelClassName());
                } catch (Exception useDefault) {
                }
                BackgroundImageForCardLayout o = 
                        new BackgroundImageForCardLayout();

                JFrame f = new JFrame("Background Image for CardLayout");
                // Hack to terminate the Timer on close.
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                f.setLocationByPlatform(true);

                f.setContentPane(o.getUI());
                f.pack();
                f.setMinimumSize(f.getSize());

                f.setVisible(true);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Thank you for your answer, can you please guide me how I can replace the black background say with "myPicture.gif"? – Ali Zakeri Mar 08 '15 at 04:23
  • That's a topic for a separate question. But try something first and get back to us with a specific problem. – Andrew Thompson Mar 08 '15 at 04:27
  • Oh, and make sure you post an MCVE of your efforts, as you were advised to do here, and as I did in reply. One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). – Andrew Thompson Mar 08 '15 at 04:29
  • 1
    Cool! Thanks, I'll see what I can do & seek help if needed. Cheers – Ali Zakeri Mar 08 '15 at 04:38