Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Putting images in GUI. Images in source file dont seem to be recognised.

  1. #1
    Junior Member
    Join Date
    May 2014
    Posts
    29
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default Putting images in GUI. Images in source file dont seem to be recognised.

    This is my class with the GUI:
    import java.awt.FlowLayout;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import javax.swing.JFrame;
    import javax.swing.JTextField;
    import javax.swing.JPasswordField;
    import javax.swing.JOptionPane;
    import javax.swing.JButton;
    import javax.swing.Icon;
    import javax.swing.ImageIcon;
    import javax.swing.JOptionPane;
     
     
    public class GUI extends JFrame{
     
    		private JButton reg;
    		private JButton custom;
     
    		public GUI(){
    			super("The Tittle");
    			setLayout(new FlowLayout());
     
    			reg = new JButton("reg Button");
    			add(reg);
     
    			Icon b = new ImageIcon(getClass().getResource("x.png"));
    			Icon c = new ImageIcon(getClass().getResource("y.png"));
     
    		custom = new JButton("Custom", b);
    		custom.setRolloverSelectedIcon(c);
    		add(custom);
     
    		HandlerClass handler = new HandlerClass();
    		reg.addActionListener(handler);
    		custom.addActionListener(handler);
     
     
    		}
     
    		private class HandlerClass implements ActionListener{
    			public void actionPerformed(ActionEvent event){
    				JOptionPane.showMessageDialog(null, String.format("%s", event.getActionCommand()));
    			}
    		}
    }

    then my main class:
    import javax.swing.JFrame;
     
    class Apples{
     
        public static void main (String args[]){
     
        	GUI go = new GUI();
        	go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        	go.setSize(300,200);
        	go.setVisible(true);
     
        }
      }

    Eclipse error message:
    Exception in thread "main" java.lang.NullPointerException
    	at javax.swing.ImageIcon.<init>(ImageIcon.java:205)
    	at GUI.<init>(GUI.java:26)
    	at Apples.main(Apples.java:7)

    i think the problem is to do with my images not being recognised. I put them in my source in User>...>workspace>src which is correct as far as i know. From what i know the images should show up if i look at my src file in eclipse but they dont. I tried changing the file type from .png to .jpg but it makes no difference. Help much appreciated!

    --- Update ---

    i solved this but im not sure how to delete the thread lol

    --- Update ---

    i solved this but im not sure how to delete the thread lol
    Last edited by nikolaiL; June 8th, 2014 at 03:46 PM.


  2. #2
    Junior Member
    Join Date
    May 2014
    Posts
    29
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default Re: Putting images in GUI. Images in source file dont seem to be recognised.

    So the program runs and i get know error but when i hover over the 'custom' button that has an image on it, another image should appear, but it doesnt. I need help with this.

    --- Update ---

    Scratch that, im answering my own questions, solved it!

Similar Threads

  1. Moving Images in a Jar File?
    By Gravity Games in forum Object Oriented Programming
    Replies: 0
    Last Post: November 2nd, 2012, 04:00 PM
  2. Images
    By Hoshi in forum Java Theory & Questions
    Replies: 1
    Last Post: September 6th, 2012, 11:54 PM
  3. switch between 2 images
    By mohamed_saleh2012 in forum Java Theory & Questions
    Replies: 1
    Last Post: March 12th, 2012, 06:22 AM
  4. [SOLVED] Help needed with Images.
    By Brt93yoda in forum What's Wrong With My Code?
    Replies: 19
    Last Post: August 16th, 2010, 05:46 PM
  5. What are the best way of placing images in GUI?
    By Ciwan in forum AWT / Java Swing
    Replies: 5
    Last Post: February 26th, 2009, 05:19 PM