2

I would like to place an image on an extended JFrame to set it as background, the extended jframe contains menu bars only. The problem is that, it doesn't display the picture, I don't know what I might be doing wrong. Any ideas are highly appreciated

public class VirtualViewGUI extends JFrame{

    public VirtualViewGUI()
      {
         super("Virtual View");

         JMenuBar jmenuBar = new JMenuBar();
         JMenu fileMenu = new JMenu("File");
         JMenu helpMenu = new JMenu("Help");
         JMenu feel = new JMenu("Look & Feel");

         JMenu layOutMenu = new JMenu("ConfigureCells");
         JMenuItem add_files = new JMenuItem("Select Directory.."); 
         JMenuItem minCellSize = new JMenuItem("height 260 X  width 260"); 
         JMenuItem moderateCellSize = new JMenuItem("height 320 X  width 320"); 
         JMenuItem maxCellSize = new JMenuItem("height 360 X  width 360"); 
         JMenuItem exit = new JMenuItem("Exit");
         JMenuItem help = new JMenuItem("Help Content");

         fileMenu.add(add_files);
         fileMenu.add(exit);
         layOutMenu.add(minCellSize);
         layOutMenu.add(moderateCellSize);
         layOutMenu.add(maxCellSize);
         helpMenu.add(help);

         jmenuBar.add(fileMenu);
         jmenuBar.add(layOutMenu);
         jmenuBar.add(helpMenu);

         ImageIcon myImage=new ImageIcon("grid_2.png");

         JLabel icon = new JLabel(myImage);
         icon.setIcon(myImage);
         setJMenuBar(jmenuBar); 

         add(icon);


         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      }
}
Jon7
  • 7,165
  • 2
  • 33
  • 39
Raymond Nakampe
  • 371
  • 7
  • 22
  • `"The problem is that, it doesn't display the picture, I don't know what I might be doing wrong."`: I don't see the problem in the code posted. Have you done any debugging? – Hovercraft Full Of Eels Sep 25 '12 at 23:51
  • Edit: and when I tested your code with images that I know to work, it works fine. So either the problem isn't in the code you're showing or you're not looking in the right place for your image. – Hovercraft Full Of Eels Sep 25 '12 at 23:57
  • I'd also make sure that `myImage` is actually loading an image. `ImageIcon` won't complain if it can't find the image, it just won't paint anything (very annoying). I'd test it with something like `File iconFile = new File("grid_2.png"); System.out.println(iconFile.exists();` – MadProgrammer Sep 26 '12 at 00:09
  • If the picture is contained within your application (ie it is a resource within your binaries), you should use `getClass().getResource("/path/to/resource/resource.name")`. This returns a `URL` which you can then use to create a `ImageIcon`. You can test the `URL` for `null` to see if the resource was loaded – MadProgrammer Sep 26 '12 at 00:12
  • Hi Hovercraft, I have the image in the same directory, I am using NetBeans, so it displays the picture in the project directory I am currently just like the other source files I am working on. Is it maybe because I am using Net beans because in JGrasp it works perfect ? – Raymond Nakampe Sep 26 '12 at 00:34
  • @ MadProgrammer, thanx for the advice I shall try it out, really appreciate it. – Raymond Nakampe Sep 26 '12 at 00:38
  • @RaymondNakampe : Your code is working fine, just appears to me as **Hovercraft Full of Eels** suggested your path to the image is not right. Have a look at this answer for more detail, [HOW TO ADD IMAGES TO YOUR NET BEANS PROJECT](http://stackoverflow.com/a/9866659/1057230). Since if I use `getClass().getResource("/pathToAnImage/imageFile.imageExtension")`, your code is giving the right result :-) For the above link, your path would be `getClass().getResource("/images/grid_2.png")` – nIcE cOw Sep 26 '12 at 05:19
  • It had to do with the way NetBeans reads images in a project, so I added "src\\\grid_2.png" for the files path and I got the results, @ MadProgrammer, that function for checking for existence of the file really worked wonders, to everyone, I thank you all of for your time and contribution in helping me out, much appreciated, thank you once again. – Raymond Nakampe Sep 26 '12 at 13:01

3 Answers3

3

No, nothing wrong at all (apart from maybe not using the right layout manager).

Rather the "adding" the icon to the frame, I would make the icon the frame's "content"...

setContentPane(icon);

This will mean when you add any additional components to the frame, the will be added on top of the image...

public VirtualViewGUI() {
    super("Virtual View");

    JMenuBar jmenuBar = new JMenuBar();
    JMenu fileMenu = new JMenu("File");
    JMenu helpMenu = new JMenu("Help");
    JMenu feel = new JMenu("Look & Feel");

    JMenu layOutMenu = new JMenu("ConfigureCells");
    JMenuItem add_files = new JMenuItem("Select Directory..");
    JMenuItem minCellSize = new JMenuItem("height 260 X  width 260");
    JMenuItem moderateCellSize = new JMenuItem("height 320 X  width 320");
    JMenuItem maxCellSize = new JMenuItem("height 360 X  width 360");
    JMenuItem exit = new JMenuItem("Exit");
    JMenuItem help = new JMenuItem("Help Content");

    fileMenu.add(add_files);
    fileMenu.add(exit);
    layOutMenu.add(minCellSize);
    layOutMenu.add(moderateCellSize);
    layOutMenu.add(maxCellSize);
    helpMenu.add(help);

    jmenuBar.add(fileMenu);
    jmenuBar.add(layOutMenu);
    jmenuBar.add(helpMenu);

    ImageIcon myImage = new ImageIcon("your picture here");

    JLabel icon = new JLabel(myImage);
    icon.setIcon(myImage);
    setJMenuBar(jmenuBar);

    // Don't add the icon to the content pane, make it the content pane, then when you add
    // anything to the frame, they will be added ontop... ;)
    setContentPane(icon);

    pack();
    setVisible(true);


    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

UPDATE with example

I added the following to code to the end of your constructor

setLayout(new GridBagLayout());
JLabel label = new JLabel("Look ma, no hands!");
label.setFont(label.getFont().deriveFont(Font.BOLD, 36f));
label.setForeground(Color.WHITE);
add(label);

And got this output

Capture the beer

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
3

Your code has got no flaw, since as I ran that on my computer it's working fine, if the path to the image is good. So if I use getClass().getResource("/pathToImageFolder/myImage.fileExtension"), it's giving me the desired outcome. Seems like you need to check if the path specified by you i.e. "grid_2.png", is actually the right path or not !! Have a look at this answer of mine regarding HOW TO ADD IMAGES TO YOUR RESOURCE FOLDER IN NETBEANS, hope this might can help you more. Here is one working example

import javax.swing.*;

public class VirtualViewGUI extends JFrame
{
    public VirtualViewGUI()
    {
        super("Virtual View");

        JMenuBar jmenuBar = new JMenuBar();
        JMenu fileMenu = new JMenu("File");
        JMenu helpMenu = new JMenu("Help");
        JMenu feel = new JMenu("Look & Feel");

        JMenu layOutMenu = new JMenu("ConfigureCells");
        JMenuItem add_files = new JMenuItem("Select Directory.."); 
        JMenuItem minCellSize = new JMenuItem("height 260 X  width 260"); 
        JMenuItem moderateCellSize = new JMenuItem("height 320 X  width 320"); 
        JMenuItem maxCellSize = new JMenuItem("height 360 X  width 360"); 
        JMenuItem exit = new JMenuItem("Exit");
        JMenuItem help = new JMenuItem("Help Content");

        fileMenu.add(add_files);
        fileMenu.add(exit);
        layOutMenu.add(minCellSize);
        layOutMenu.add(moderateCellSize);
        layOutMenu.add(maxCellSize);
        helpMenu.add(help);

        jmenuBar.add(fileMenu);
        jmenuBar.add(layOutMenu);
        jmenuBar.add(helpMenu);

        ImageIcon myImage = new ImageIcon(
            getClass().getResource(
                    "/image/cow-cartoon.jpg"));

        JLabel icon = new JLabel(myImage);
        icon.setIcon(myImage);
        setJMenuBar(jmenuBar); 

        add(icon);

        pack();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationByPlatform(true);    
        setVisible(true);
    }

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                new VirtualViewGUI();
            }
        });
    }
}
Community
  • 1
  • 1
nIcE cOw
  • 24,468
  • 7
  • 50
  • 143
0

I managed to do get it right, it was with NetBeans like I suspected initially as with other IDEs it was working fine, I needed to add "src\\grid_2.png" for the image path even if it was inside my work directory. Thanks for your time everyone, its really much appreciated.

Raymond Nakampe
  • 371
  • 7
  • 22
  • Since you working with Java, instead of using \\ (two backslash) use a single forward slash for defining path like "src/grid_2.png". For the rest You're MOST WELCOME and KEEP SMILING :-) – nIcE cOw Sep 26 '12 at 13:24