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 10 of 10

Thread: JComboBox

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Posts
    16
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Post JComboBox

    Hello,
    I am trying to create a JComboBox that would change pictures. My problem is that my method always returns null and it doesn`t return my image which is located in my package. Now I would like to know if java.net.URL is used only for getting images from web page or can I use it to get image from a local folder?

     
            public static ImageIcon createImageIcon(String path){
     
    	URL imgURL = Bikes.class.getResource(path);
     
            if (imgURL != null) {
                return new ImageIcon(imgURL);
            } else {
                System.out.println("Couldn't find file: " + path);
                System.out.println(path);
                return null;
            }
        }


  2. #2
    Member
    Join Date
    Jul 2012
    Posts
    119
    Thanks
    0
    Thanked 19 Times in 19 Posts

    Default Re: JComboBox

    icocijan
    Sure, you can access either from the web OR from a folder or from a JAR file. The code you wrote is good for a) web, b) Jar file you can circumvent the problem as following (access URL/JAR/local folder
    if (!(new File(path)).exists()) return new ImageIcon(getClass().getResource(path)); // web or Jar
    else return new ImageIcon(path); // local folder
    the getClass() is probably your Bikes.class

  3. The Following User Says Thank You to Voodoo For This Useful Post:

    ikocijan (July 16th, 2012)

  4. #3
    Junior Member
    Join Date
    Jun 2012
    Posts
    16
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default Re: JComboBox

    OK, thanks for your answer. Tried that but it still won`t load the picture. Can you please tell me in which folder should I put the pictures? package folder or something else?

    edit: Could there be an error in this method which calls createImageIcon method?

      public void updateLabel(String name) {
    		ImageIcon icon = createImageIcon(name);
                     picture.setIcon(icon);
                     picture.setToolTipText(name.toLowerCase()); //sets text when mouse is over the picture (works!!!! but there is no picture only text what picture should be there !)
     
                if (icon != null) {
                picture.setText(null);
            } else { //ako nema slike postavlja natpis "Image not found"
                picture.setText("Image not found");
            }
        }
    Last edited by ikocijan; July 16th, 2012 at 04:52 AM.

  5. #4
    Member
    Join Date
    Jul 2012
    Posts
    119
    Thanks
    0
    Thanked 19 Times in 19 Posts

    Default Re: JComboBox

    ...tell me how the path looks like. Remember that java is CASE SENSITIVE and window isn't.

  6. The Following User Says Thank You to Voodoo For This Useful Post:

    ikocijan (July 16th, 2012)

  7. #5
    Junior Member
    Join Date
    Jun 2012
    Posts
    16
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default Re: JComboBox

    When I print it on console path is Bike 5.

    When I try your code it always throws java.lang.NullPointerException. I think that is becase it never creates new File. I changed IF statement so that it runs when F exists and it always goes straight to else statement.

      File F = new File (path);
     
    		if ( F.exists())
    			{
    			 System.out.println("If statement");	
    			return new ImageIcon(Bikes.class.getResource(path)); // web or Jar
     
    			}
    		else
    		{
                             System.out.println(path);
    			 System.out.println("Else statement");	
    			 return new ImageIcon(path); // local folder
    		}

  8. #6
    Member
    Join Date
    Jul 2012
    Posts
    119
    Thanks
    0
    Thanked 19 Times in 19 Posts

    Default Re: JComboBox

    your path is invalid hence F is NULL and you end up with NullPointerException at the statement if (F.exists())

  9. The Following User Says Thank You to Voodoo For This Useful Post:

    ikocijan (July 16th, 2012)

  10. #7
    Junior Member
    Join Date
    Jun 2012
    Posts
    16
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default Re: JComboBox

    I know, I read that in Java tutorial NullPointerException - If the pathname argument is null. I would like to know if I need to put the pictures in a specific folder or add then to my project (tried all of the things I know).
    Last edited by ikocijan; July 16th, 2012 at 06:32 AM.

  11. #8
    Member
    Join Date
    Jul 2012
    Posts
    119
    Thanks
    0
    Thanked 19 Times in 19 Posts

    Default Re: JComboBox

    File fi = new File(path);
    if (fi == null || !fi.exists()) return new ImageIcon(Bikes.class.getResource(path)); // web or Jar
    else return new ImageIcon(path); // local folder

  12. The Following User Says Thank You to Voodoo For This Useful Post:

    ikocijan (July 16th, 2012)

  13. #9
    Junior Member
    Join Date
    Jun 2012
    Posts
    16
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default Re: JComboBox

    Quote Originally Posted by Voodoo View Post
    File fi = new File(path);
    if (fi == null || !fi.exists()) return new ImageIcon(Bikes.class.getResource(path)); // web or Jar
    else return new ImageIcon(path); // local folder
    SOLVED my problem. The whole time I gave the program Bike 5 path insted of Bike5.jpg. Didn`t realize that extensions must be added! Sorry for wasting your time and thanks for help!

  14. #10
    Member
    Join Date
    Jul 2012
    Posts
    119
    Thanks
    0
    Thanked 19 Times in 19 Posts

    Default Re: JComboBox

    you're welcome

  15. The Following User Says Thank You to Voodoo For This Useful Post:

    ikocijan (July 16th, 2012)

Similar Threads

  1. JComboBox help again but different help 75+ items in it
    By derekxec in forum AWT / Java Swing
    Replies: 7
    Last Post: August 27th, 2011, 06:53 PM
  2. JComboBox suggestions/help how can i do this?
    By derekxec in forum AWT / Java Swing
    Replies: 2
    Last Post: August 24th, 2011, 02:58 PM
  3. JComboBox
    By SV25 in forum Java Theory & Questions
    Replies: 2
    Last Post: April 20th, 2011, 08:47 AM
  4. [SOLVED] JCombobox help
    By sman36 in forum AWT / Java Swing
    Replies: 10
    Last Post: August 11th, 2010, 04:11 AM
  5. JComboBox
    By nasi in forum AWT / Java Swing
    Replies: 1
    Last Post: April 29th, 2010, 08:40 AM