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

Thread: How to put in a "new" image in ImageIcon JFrame ?

  1. #1
    Member
    Join Date
    Mar 2013
    Posts
    33
    My Mood
    Innocent
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default How to put in a "new" image in ImageIcon JFrame ?

    Hi there. If you are putting pictures in labels inside of a JFrame and there are 3 labels in the JFrame. You want the same picture for the first and second labels, but for the third label you want to change to a new picture.
    How would you make it display the new picture ?

    ie: I have done this :
    ImageIcon icon = new ImageIcon ("devil.gif")

    and entered this above label3 in the code to try to display angels.gif
    ImageIcon icon = new ImageIcon ("angels.gif")

    but I get the error:
    LabelDemoGo.java:27: error: variable icon is already defined in method main (String[])

    Heres the full code which works fine displaying all three labels with the same picture (But I want to make the third label have a different pictuere)
    //LabelDemoGo.java
    //Demonstrates the use of image icons in labels.
     
     
    import java.awt.*;
    import javax.swing.*;
     
    public class LabelDemoGo   {
     
    //creates and displays the primary application frame.
     
    public static void main (String[] args)   {
     
    JFrame frame = new JFrame ("Label Demo");
    frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
     
    ImageIcon icon = new ImageIcon ("devil.gif");
     
    JLabel label1, label2, label3;
     
    label1 = new JLabel ("TRY ", icon, SwingConstants.CENTER);
     
    label2 = new JLabel ("alittle ", icon, SwingConstants.CENTER);
    label2.setHorizontalTextPosition (SwingConstants.RIGHT);
    label2.setVerticalTextPosition (SwingConstants.CENTER);
     
    label3 = new JLabel ("HARDER -", icon, SwingConstants.CENTER);
    label3.setHorizontalTextPosition (SwingConstants.RIGHT);
    label3.setVerticalTextPosition (SwingConstants.CENTER);
     
    JPanel panel = new JPanel();
    panel.setBackground (Color.pink);
    panel.setPreferredSize (new Dimension (250, 400));
    panel.add (label1);
    panel.add (label2);
    panel.add (label3);
     
    frame.getContentPane().add(panel);
    frame.pack();
    frame.setVisible(true);
     
      }
    }

    thank you.


  2. #2
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: How to put in a "new" image in ImageIcon JFrame ?

    You can only have one variable with the name icon. Try changing the second one to "ImageIcon icon2 = new ImageIcon ("angels.gif")".
    Writing code is your job, helping you fix and understand it is mine.

    <-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

  3. The Following User Says Thank You to Chris.Brown.SPE For This Useful Post:

    craigjlner (March 29th, 2013)

  4. #3
    Member
    Join Date
    Mar 2013
    Posts
    33
    My Mood
    Innocent
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: How to put in a "new" image in ImageIcon JFrame ?

    Quote Originally Posted by Chris.Brown.SPE View Post
    You can only have one variable with the name icon. Try changing the second one to "ImageIcon icon2 = new ImageIcon ("angels.gif")".

    Whilst making the following amendments:
    ImageIcon icon = new ImageIcon ("devil.gif");
     
    JLabel label1, label2;
     
    label1 = new JLabel ("TRY ", icon, SwingConstants.CENTER);
     
    label2 = new JLabel ("alittle ", icon, SwingConstants.CENTER);
    label2.setHorizontalTextPosition (SwingConstants.RIGHT);
    label2.setVerticalTextPosition (SwingConstants.CENTER);
     
    ImageIcon icon2 = new ImageIcon ("angels.gif");
     
    JLabel label3;
    label3 = new JLabel ("HARDER -", icon, SwingConstants.CENTER);
    label3.setHorizontalTextPosition (SwingConstants.RIGHT);
    label3.setVerticalTextPosition (SwingConstants.CENTER);

    It compiles and runs but still with the devil.gif where angels.gif should be.....
    Line 7 is the code i inserted and I moved - label3; - down to below that.

    Maybe ImageIcon class isn't the way to achieve this ?

  5. #4
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: How to put in a "new" image in ImageIcon JFrame ?

    When you make your third label make sure you are referencing the right icon.

    label3 = new JLabel ("HARDER -", icon, SwingConstants.CENTER);
    Writing code is your job, helping you fix and understand it is mine.

    <-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

  6. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,321
    Thanks
    56
    Thanked 2,374 Times in 2,345 Posts

    Default Re: How to put in a "new" image in ImageIcon JFrame ?

    A suggestion: give names to the variables that show what the variable contains: angleIcon and devilIcon
    icon and icon2 do NOT say what the variables are
    If you don't understand my answer, don't ignore it, ask a question.

  7. #6
    Member
    Join Date
    Mar 2013
    Posts
    33
    My Mood
    Innocent
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: How to put in a "new" image in ImageIcon JFrame ?

    Quote Originally Posted by Chris.Brown.SPE View Post
    When you make your third label make sure you are referencing the right icon.

    label3 = new JLabel ("HARDER -", icon, SwingConstants.CENTER);
    Yes. I got it working! Thanks alot for your help

Similar Threads

  1. What's the difference between "import.javax.swing*" and JFrame inheritance
    By Johnny Bravo in forum Object Oriented Programming
    Replies: 5
    Last Post: August 14th, 2012, 10:28 AM
  2. Replies: 3
    Last Post: December 7th, 2011, 02:03 AM
  3. Replies: 7
    Last Post: August 13th, 2011, 02:22 AM
  4. Replies: 2
    Last Post: June 29th, 2009, 04:06 PM
  5. Replies: 4
    Last Post: June 18th, 2009, 10:23 AM