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: Showing an Image on screen

  1. #1
    Junior Member
    Join Date
    May 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Showing an Image on screen

    Hi all,

    I am trying to do a simple combobox which according to what is selected an image is shown. This is working however I have to minimize and maximize the window so that the image is shown. Moreover if 2 different choices are done on the same run both pics are being shown on the panel. Can you please help?

    Here is the code:

     import javax.swing.*;
     import java.awt.event.*;
     
     public class Images extends JFrame implements ActionListener {
         String[] formats = {"Green","Red"};
         JComboBox formatBox = new JComboBox();
          JPanel pane = new JPanel();
           JLabel formatLabel;
        public Images() {
            super("Door Choice");
            setSize(220, 300);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        
            formatLabel = new JLabel("Choose door type:");
            pane.add(formatLabel);
            for (int i = 0; i < formats.length; i++){
                 formatBox.addItem(formats[i]);
            }
     
     
            pane.add(formatBox);
            formatBox.addActionListener(this);
            add(pane);
            setVisible(true);
         }
           public void actionPerformed(ActionEvent ae)
        {
     
                     ImageIcon myDoor= new ImageIcon("C:\\Users\\Projector\\Desktop\\Doors\\"+formatBox.getSelectedItem()+".png");
                     JLabel myImage = new JLabel(); 
                     myImage.setIcon(myDoor);
                     pane.add(myImage);
     
            }
     
         public static void main(String[] args) {
            Images image = new Images();
     
        }
    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Showing an Image on screen

    You are creating a new JLabel each time, then adding it to the pane (which I'm surprised works without calling revalidate). Is that really what you want to do?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Moving an image around the screen using the arrow keys.
    By nemo in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 19th, 2013, 12:08 AM
  2. Replies: 2
    Last Post: February 14th, 2011, 05:36 PM
  3. Can't figure out why my image isn't showing up!!
    By thrashingboy in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 8th, 2010, 06:01 PM
  4. Showing a picture in a GUI
    By joachim89 in forum AWT / Java Swing
    Replies: 1
    Last Post: February 15th, 2010, 02:42 PM
  5. Pixel Alteration in an image/image rotation
    By bguy in forum Java Theory & Questions
    Replies: 3
    Last Post: November 1st, 2009, 10:50 AM