0

I want to set a background image in a simple JTabbedPane. I used a Jlabel to set my background image. But i haven't been able to do so as i get a null pointer exception upon running. Can anyone help with some more ideas?

Here is my code:

import javax.swing.ImageIcon;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;

import java.awt.*;
import java.awt.event.*;

class ImageTest 
{
 JTabbedPane tp;
 JLabel lab1
 JPanel  welcome;
 JFrame frame;
 ImageIcon image2;
 public void createUI()
 {
    frame=new JFrame("JTabbedPane");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);



    welcome= new JPanel(null);
    welcome.setPreferredSize(new Dimension(1366,786));
    image2 = new ImageIcon("icloud.jpg");
    tp=new JTabbedPane();
            Container pane = frame.getContentPane();
    pane.add(tp);
    tp.addTab("Welcome", welcome);

    lab1.setIcon(image2);
    lab1.setBounds(0,0,1500,700);

    frame.setSize(500,500);
    frame.setVisible(true);
    frame.setTitle("I-Cloud");

}   
public static void main(String[] args) 
{

    try {
        for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (Exception e) {

    }
    ImageTest  tbp = new ImageTest ();
    tbp.createUI();     
}
   }

EDIT:

 import javax.swing.ImageIcon;
 import javax.swing.JTabbedPane;
 import javax.swing.JTextField;
 import javax.swing.JLabel;
 import javax.swing.JPanel;
 import javax.swing.JButton;
 import javax.swing.JFrame;
 import javax.swing.SwingUtilities;
 import javax.swing.UIManager;
 import javax.swing.UnsupportedLookAndFeelException;
 import javax.swing.UIManager.LookAndFeelInfo;

 import java.awt.*;
 import java.awt.event.*;

 class ImageTest 
  {
 JTabbedPane tp;
 JLabel lab1;
 JPanel  welcome,w;
 JFrame frame;
 ImageIcon image2;
 JButton b1;

public void createUI()
{
    frame=new JFrame("JTabbedPane Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    b1=new JButton();
    welcome= new JPanel(new GridLayout(1,1,15,15));
    w=new JPanel (new GridLayout(1, 1, 15, 15));
    welcome.setPreferredSize(new Dimension(1366,786));

    ImageIcon icon = new ImageIcon(ImageTest.class.getResource("icloud.jpg"));

    tp=new JTabbedPane();
            Container pane = frame.getContentPane();
    pane.add(tp);


    lab1=new JLabel();
    lab1.setIcon(icon);
    w.setOpaque(false);
    w.add(b1);


    b1.setVisible(true);
            welcome.add(lab1);
            welcome.add(w);
            tp.addTab("Welcome", welcome);

    frame.setSize(500,500);
    frame.pack();
    frame.setVisible(true);
    frame.setTitle("I-Cloud");

}   
public static void main(String[] args) 
{
    try {
        for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (Exception e) {

    }

        ImageTest w = new ImageTest();
        w.createUI();   

    }
  }
user3320152
  • 113
  • 2
  • 7

2 Answers2

2

You may just want to paint the image onto the panel you use for the JTabbedPane (as seen below). But as MadProgammer pointed out. It's most likely you are getting a null from the file location of your image file.

Note: when passing a String to the ImageIcon you are telling it to look for image in the file system. though this may work in a development environment from your IDE, it will not work at time of deployment Instead of reading the image file from the file system, you want to load it from the class path, as should be don with embedded resources, using a URL, which can be obtained by using MyClass.class.getResource(path)

So the way you should loading your image is like this

ImageIcon icon = new ImageIcon(ImageTest.class.getResource("icloud.jpg"));

And your image file would be in the same package as ImageIcon.java. You don't have to put the image in the same package, as you can specify a different path. You can find more info from the embedded resource tag wiki link above.

But back to the option of painting the background, see the example below. Instead of using a URL web link for the image though, you would read in your image file path as specified above.

enter image description here

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TabBackground {

    private BufferedImage bg;

    public TabBackground() {
        try {
            bg = ImageIO.read(new URL("http://2.bp.blogspot.com/-wWANHD-Dr00/TtSmeY57ZXI/AAAAAAAABB8/t-fpXmQZ0-Y/s1600/Vector_by_Karpiu23.png"));
        } catch (IOException ex) {
            Logger.getLogger(TabBackground.class.getName()).log(Level.SEVERE, null, ex);
        }

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

            @Override
            public Dimension getPreferredSize() {
                return new Dimension(400, 300);
            }
        };
        JPanel buttons = new JPanel(new GridLayout(4, 1, 15, 15));
        buttons.setOpaque(false);
        for (int i = 0; i < 4; i++) {
            buttons.add(new JButton("Button"));
        }
        tabPanel.add(buttons);

        JTabbedPane tabPane = new JTabbedPane();
        tabPane.add("Panel with Bachground", tabPanel);

        JFrame frame = new JFrame("Tabbed Pane with Background");
        frame.setContentPane(tabPane);
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    Logger.getLogger(TabBackground.class.getName()).log(Level.SEVERE, null, ex);
                }
                new TabBackground();
            }
        });
    }
}
Community
  • 1
  • 1
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • @peeskillet- I am still getting exception in the line ImageIcon icon = new ImageIcon(ImageTest.class.getResource("icloud.jpg")); – user3320152 Feb 23 '14 at 17:55
  • Put your image in the same package as the `ImageTest.java` – Paul Samsotha Feb 23 '14 at 17:56
  • What's the exception? – Paul Samsotha Feb 23 '14 at 18:06
  • Does it has to do anything with image resolution? – user3320152 Feb 23 '14 at 18:06
  • It has a NPE exception. – user3320152 Feb 23 '14 at 18:06
  • If it's that line the NPE is pointing to. The _only_ thing it could be is that your path is wrong. If the image is in the same package as the class the you are just using `"icloud.jpg"` and the image is actually named `icloud.jpg`, you shouldnt get an NPE on that line – Paul Samsotha Feb 23 '14 at 18:09
  • But even if the image isn't present at all it shouldn't generate a NPE. – user3320152 Feb 23 '14 at 18:14
  • I knew it. The IMmageIcon code you provided will _never_ throw an NPE. that wasnt even the line that was throwing the NPE. it the next line in the code from your post where you try and set the icon for your label. You never inititialized your label. Thats where the NPE is coming from. a null label. – Paul Samsotha Feb 23 '14 at 18:19
  • You can't do anything with a component that hasn't been initialized – Paul Samsotha Feb 23 '14 at 18:20
  • `lab1 = new JLabel(); lab1.setIcon(image2);` – Paul Samsotha Feb 23 '14 at 18:25
  • user3313050 has already made this one work and has posted this with a picture. I don't know what's wrong in mine. – user3320152 Feb 23 '14 at 18:35
  • You never added the label to the panel. Do follow my answer, You will have better luck with adding components on top of the image. Don't expect that just because you add the label, that will be the background. Doesn't work like that. If you paint the image on the panel, you can still add components on top of the image. That's why I suggested it. If there's something you dont understand about my code, just ask – Paul Samsotha Feb 23 '14 at 18:41
  • Another thing you can do is just add the label to the tab, without the panel, set the layout manager for the label, then add component to the label. – Paul Samsotha Feb 23 '14 at 18:46
  • Hello! Finally i got my image set. But i can't set the location and size of the button like the example you had shown. i have edited my code. plz help. – user3320152 Feb 27 '14 at 09:59
2

You can add an image like this on Jframe:

tp.addTab("Welcome",new JLabel(new ImageIcon("bksqla_xlargecover.jpg")));

enter image description here

andrewsi
  • 10,807
  • 132
  • 35
  • 51
Benjamin
  • 2,257
  • 1
  • 15
  • 24