Results 1 to 12 of 12
Like Tree1Likes
  • 1 Post By JosAH

Thread: Why am I getting errors on this code?

  1. #1
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Why am I getting errors on this code?

    I am trying to display an image of the periodic table on a window. Why am I getting errors?
    Thanks, and here is the code:

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    public class DisplayImg{
    ImageIcon image = new ImageIcon("Desktop/PeriodicTable.jpg");
    public void createAndShowGUI(){
        JFrame frame = new JFrame("Show Image");
        frame.setPreferredSize(new Dimension(250,250));
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new JPanel();
        frame.add(panel);
        panel.add(image);
    }
    public static void main(String[] args) {
    DisplayImg runIt = new DisplayImg();
    runIt.createAndShowGUI();
      }
    }
    When I try to run it, I get:


    Java Code:
    Michaels-MacBook-Pro:javap MWaldman$ javac DisplayImg.java
    DisplayImg.java:12: error: no suitable method found for add(ImageIcon)
    panel.add(image);
         ^
        method Container.add(Component,Object,int) is not applicable
          (actual and formal argument lists differ in length)
        method Container.add(Component,Object) is not applicable
          (actual and formal argument lists differ in length)
        method Container.add(Component,int) is not applicable
          (actual and formal argument lists differ in length)
        method Container.add(String,Component) is not applicable
          (actual and formal argument lists differ in length)
        method Container.add(Component) is not applicable
          (actual argument ImageIcon cannot be converted to Component by method invocation conversion)
        method Component.add(PopupMenu) is not applicable
          (actual argument ImageIcon cannot be converted to PopupMenu by method invocation conversion)
    1 error
    Thanks!

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,344
    Blog Entries
    7
    Rep Power
    20

    Default Re: Why am I getting errors on this code?

    If you had read the API documentation for the JPanel class (instead of just guessing and hope for the best) you'd had read that you can't add an ImageIcon to a JPanel; you can add it to a JLabel and add that to your panel.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,383
    Rep Power
    5

    Default Re: Why am I getting errors on this code?

    It is showing all the possible add signatures and none of them apply to type Image. You don't add images to panels like you add components. You must write the image in the panel.

    Regards,
    Jim
    Last edited by jim829; 03-11-2013 at 02:35 PM. Reason: Nuts!!
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  4. #4
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Re: Why am I getting errors on this code?

    I tried it, but it still won't work.
    New code goes:

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    public class DisplayImg{
    ImageIcon image = new ImageIcon("Desktop/PeriodicTable.jpg");
    public void createAndShowGUI(){
        JFrame frame = new JFrame("Show Image");
        frame.setPreferredSize(new Dimension(250,250));
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new JPanel();
        frame.add(panel);
        JLabel label = new JLabel("Img",icon,JLabel.CENTER);
        label.add(image);
        panel.add(label);
    }
    public static void main(String[] args) {
    DisplayImg runIt = new DisplayImg();
    runIt.createAndShowGUI();
      }
    }
    I get errors:
    Java Code:
    avac DisplayImg.java
    DisplayImg.java:12: error: no suitable method found for add(ImageIcon)
    panel.add(image);
         ^
        method Container.add(Component,Object,int) is not applicable
          (actual and formal argument lists differ in length)
        method Container.add(Component,Object) is not applicable
          (actual and formal argument lists differ in length)
        method Container.add(Component,int) is not applicable
          (actual and formal argument lists differ in length)
        method Container.add(String,Component) is not applicable
          (actual and formal argument lists differ in length)
        method Container.add(Component) is not applicable
          (actual argument ImageIcon cannot be converted to Component by method invocation conversion)
        method Component.add(PopupMenu) is not applicable
          (actual argument ImageIcon cannot be converted to PopupMenu by method invocation conversion)
    1 error
    Michaels-MacBook-Pro:javap MWaldman$ javac DisplayImg.java
    DisplayImg.java:12: error: cannot find symbol
        JLabel label = new JLabel("Img",icon,JLabel.CENTER);
                                        ^
      symbol:   variable icon
      location: class DisplayImg
    DisplayImg.java:13: error: no suitable method found for add(ImageIcon)
        label.add(image);
             ^
        method Container.add(Component,Object,int) is not applicable
          (actual and formal argument lists differ in length)
        method Container.add(Component,Object) is not applicable
          (actual and formal argument lists differ in length)
        method Container.add(Component,int) is not applicable
          (actual and formal argument lists differ in length)
        method Container.add(String,Component) is not applicable
          (actual and formal argument lists differ in length)
        method Container.add(Component) is not applicable
          (actual argument ImageIcon cannot be converted to Component by method invocation conversion)
        method Component.add(PopupMenu) is not applicable
          (actual argument ImageIcon cannot be converted to PopupMenu by method invocation conversion)
    2 errors
    Thanks again!

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,383
    Rep Power
    5

    Default Re: Why am I getting errors on this code?

    Where are you defining icon? Your image is your icon.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,802
    Rep Power
    19

    Default Re: Why am I getting errors on this code?

    Java Code:
    JLabel label = new JLabel("Img",icon,JLabel.CENTER);
    Your variable is called 'image', not 'icon'.
    And I'm guessing you don't want to display the text "Img" as well as the icon itself?

    And JLabel has no add() method for ImageIcons.
    You just need the constructor.

    This is the JLabel API.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Re: Why am I getting errors on this code?

    I am still getting an error when I run it. Here is the new code..
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    public class DisplayImg{
    ImageIcon image = new ImageIcon("Desktop/PeriodicTable.jpg");
    public void createAndShowGUI(){
        JFrame frame = new JFrame("Show Image");
        frame.setPreferredSize(new Dimension(250,250));
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new JPanel();
        frame.add(panel);
        JLabel label = new JLabel("Img",icon,JLabel.CENTER);
        label.add(image);
        panel.add(label);
    }
    public static void main(String[] args) {
    DisplayImg runIt = new DisplayImg();
    runIt.createAndShowGUI();
      }
    }
    Here are the errors. Thanks!
    Java Code:
    avac DisplayImg.java
    DisplayImg.java:12: error: no suitable method found for add(ImageIcon)
    panel.add(image);
         ^
        method Container.add(Component,Object,int) is not applicable
          (actual and formal argument lists differ in length)
        method Container.add(Component,Object) is not applicable
          (actual and formal argument lists differ in length)
        method Container.add(Component,int) is not applicable
          (actual and formal argument lists differ in length)
        method Container.add(String,Component) is not applicable
          (actual and formal argument lists differ in length)
        method Container.add(Component) is not applicable
          (actual argument ImageIcon cannot be converted to Component by method invocation conversion)
        method Component.add(PopupMenu) is not applicable
          (actual argument ImageIcon cannot be converted to PopupMenu by method invocation conversion)
    1 error
    Michaels-MacBook-Pro:javap MWaldman$ javac DisplayImg.java
    DisplayImg.java:12: error: cannot find symbol
        JLabel label = new JLabel("Img",icon,JLabel.CENTER);
                                        ^
      symbol:   variable icon
      location: class DisplayImg
    DisplayImg.java:13: error: no suitable method found for add(ImageIcon)
        label.add(image);
             ^
        method Container.add(Component,Object,int) is not applicable
          (actual and formal argument lists differ in length)
        method Container.add(Component,Object) is not applicable
          (actual and formal argument lists differ in length)
        method Container.add(Component,int) is not applicable
          (actual and formal argument lists differ in length)
        method Container.add(String,Component) is not applicable
          (actual and formal argument lists differ in length)
        method Container.add(Component) is not applicable
          (actual argument ImageIcon cannot be converted to Component by method invocation conversion)
        method Component.add(PopupMenu) is not applicable
          (actual argument ImageIcon cannot be converted to PopupMenu by method invocation conversion)
    2 errors

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,344
    Blog Entries
    7
    Rep Power
    20

    Default Re: Why am I getting errors on this code?

    I know it's a totally wierd idea but how about reading the relevant API documentation? I promise it won't hurt.

    kind regards,

    Jos
    DarrylBurke likes this.
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,802
    Rep Power
    19

    Default Re: Why am I getting errors on this code?

    And the posts that actually point out where you are going wrong.
    Because the lines jim and I highlighted have not changed at all...
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  10. #10
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Re: Why am I getting errors on this code?

    Hi guys, I read the API documentation. However, I don't understand why my image still won't be displayed am I writing the location in an incorrect manner?



    Java Code:
        ImageIcon icon = new ImageIcon("Desktop/ray.png");
    that's the location
    The program will run, but img won't display.
    Last edited by MW130; 03-14-2013 at 01:27 PM.

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,802
    Rep Power
    19

    Default Re: Why am I getting errors on this code?

    So what does the rest of the code look like now?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  12. #12
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    317
    Rep Power
    4

    Default Re: Why am I getting errors on this code?

    Hi MW130,

    The exact path to the image file depends on your OS but on Windows 7 it would be "C:/Users/USER_NAME/Desktop/ray.png"

    NOTE: You need to replace USER_NAME with the name of the profile you are using.

    Regards.

Similar Threads

  1. Errors in code
    By MikeJ39 in forum New To Java
    Replies: 0
    Last Post: 10-23-2011, 10:57 PM
  2. Help with errors in code
    By XodoX in forum New To Java
    Replies: 10
    Last Post: 02-08-2011, 05:14 AM
  3. nasty errors in your code
    By j2me64 in forum Forum Lobby
    Replies: 0
    Last Post: 01-27-2011, 04:18 PM
  4. Errors in the code (?)
    By AndersBjörnör in forum New To Java
    Replies: 1
    Last Post: 10-09-2009, 03:06 PM
  5. HELP ON errors occuring in the code
    By jaiminparikh in forum Advanced Java
    Replies: 6
    Last Post: 02-12-2009, 02:02 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
  •