Results 1 to 5 of 5
  1. #1
    roadrash is offline Member
    Join Date
    Aug 2009
    Posts
    2
    Rep Power
    0

    Unhappy Can't display image icons on simple demo

    I'm new to Java and trying to run some basic demos from a class on my iMac. I should be able to create a new image icon using the ImageIcon object. No problems building. But when I run the program, the image(s) simply don't appear on the button (for this example). The image files are located in an image folder inside the working directory. This is too simple to be an issue, but there it is. Any suggestions?

    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.KeyEvent;

    import javax.swing.*;
    import javax.swing.ImageIcon.*;

    public class JButtons extends JFrame {
    public static void main(String[] args) {
    new JButtons();
    }

    public JButtons() {
    super("Creating Buttons");

    addWindowListener(new ExitListener());
    Container content = getContentPane();
    content.setBackground(Color.white);
    content.setLayout(new FlowLayout());


    // This is the statement of interest
    // the image can be anything in my
    ImageIcon splat = new ImageIcon("images/grapes.jpg");


    JButton button1 = new JButton("Button 1");
    content.add(button1);
    //JButton button2 = new JButton("Button 2 ",splat);
    JButton button2 = new JButton("Button 2");
    content.add(button2);
    //JButton button3 = new JButton("Button 3 ", splat);
    JButton button3 = new JButton("Button 3");
    button3.setHorizontalTextPosition(SwingConstants.L EFT);
    content.add(button3);
    //JButton button4 = new JButton(splat);
    JButton button4 = new JButton(splat);
    content.add(button4);

    pack();
    setVisible(true);
    }
    }

  2. #2
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    Location
    San Jose, CA, USA
    Posts
    320
    Rep Power
    5

    Default

    You might try this:

    Java Code:
    ImageIcon splat = new ImageIcon(this.getClass().getResource("images/grapes.jpg"));

  3. #3
    roadrash is offline Member
    Join Date
    Aug 2009
    Posts
    2
    Rep Power
    0

    Default

    It worked the first time:)

    After that I got the following popup:

    Cannot launch Java application
    Uncaught exception in main method:
    java.lang.NullPointerException

  4. #4
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    Location
    San Jose, CA, USA
    Posts
    320
    Rep Power
    5

    Default

    "After that I got the following popup:"

    I'm not exactly sure what you mean you got a popup...

    "Cannot launch Java application
    Uncaught exception in main method:
    java.lang.NullPointerException "

    Hopefully it displays the stacktrace of exactly where and what line in your code the NullPointerException happened. Null translates to "Nothing." It means you have an object somewhere that hasn't been created or doesn't exist yet but you are trying to use it.

  5. #5
    Melkor is offline Member
    Join Date
    Nov 2009
    Posts
    1
    Rep Power
    0

    Default Uncaught exception?

    Since
    Java Code:
    NullPointerException
    is a subclass of
    Java Code:
    RuntimeException
    , it shouldn't cause a compile error... are you using an IDE? if so, it might have a bug.

Similar Threads

  1. Simple Calculator Display Problem :(
    By jimbob in forum Java Applets
    Replies: 4
    Last Post: 07-18-2009, 04:13 AM
  2. Putting clickable icons on an image
    By szakee in forum AWT / Swing
    Replies: 3
    Last Post: 04-06-2009, 10:25 AM
  3. is there anyone how can help me with image icons please?
    By xbox_nutter in forum New To Java
    Replies: 4
    Last Post: 03-27-2009, 10:26 AM
  4. How do I display a JList with icons and text?
    By aneesahamedaa in forum AWT / Swing
    Replies: 2
    Last Post: 09-04-2008, 08:49 AM
  5. Replies: 0
    Last Post: 04-16-2008, 10:59 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •