Results 1 to 6 of 6
  1. #1
    Nilla is offline Member
    Join Date
    Jun 2012
    Posts
    13
    Rep Power
    0

    Default Images do not display

    In my code with JComboBox different images should display when I make a choice, but the don´t. What do I have to do with the code to get it work?

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    class ComBox2 extends JFrame implements ActionListener, ItemListener   {
    
    //    Data Members
        private static final int FRAME_WIDTH    = 400;
        private static final int FRAME_HEIGHT   = 400;
        private static final int FRAME_X_ORIGIN = 150;
        private static final int FRAME_Y_ORIGIN = 250;
        private JComboBox comboBox;
        private ImageIcon image;
        private JLabel label1;
        private JLabel label2;
        private JLabel label3;
        private JPanel comboPanel;
        private JPanel comboPane2;
        private JPanel comboPane3;
        private JPanel comboPane4;
        
    //      Main method
        public static void main(String[] args) {
            ComBox2 frame = new ComBox2();
            frame.setVisible(true);
        }
    //    Constructors
        public ComBox2() {
            Container   contentPane;
            JPanel      comboPanel, imagePanel;
    
            JButton     okButton;
            String[]    comboBoxItem = {"Surfa", "Måla", "Låda", "Ingen"};
    
            //set the frame properties
            setSize      (FRAME_WIDTH, FRAME_HEIGHT);
            setTitle     ("Demo av JComboBox");
            setLocation  (FRAME_X_ORIGIN, FRAME_Y_ORIGIN);
    
            contentPane = getContentPane( );
            contentPane.setBackground(Color.WHITE);
            contentPane.setLayout(new BorderLayout());
    
            //create and place a combo box
            comboPanel = new JPanel(new FlowLayout());
            comboPanel.setBorder(
                        BorderFactory.createTitledBorder("Välj en bild"));
    
            comboBox = new JComboBox(comboBoxItem);
            comboBox.addItemListener(this);
            comboPanel.add(comboBox);
           	comboBox.addActionListener(this);
           	contentPane.add(comboPanel, BorderLayout.NORTH);
           
           	// infoga bilderna
           	imagePanel = new JPanel(new FlowLayout());
      		image =new ImageIcon(getClass().getResource("surfa.png"));
     		label1 = new JLabel(image);
     		add(label1);
     		label1.setVisible(false);
     		
     		image =new ImageIcon(getClass().getResource("lada.png"));
     		label2 = new JLabel(image);
     		add(label2);
     		label2.setVisible(false);
     		
     		image =new ImageIcon(getClass().getResource("mala.png"));
     		label3 = new JLabel(image);
     		add(label3);
     		label3.setVisible(false);
     		comboBox.addActionListener(this);
     		comboBox.addItemListener(this);
     		contentPane.add(imagePanel, BorderLayout.CENTER);
     	 		 				
            //register 'Exit upon closing' as a default close operation
            setDefaultCloseOperation( EXIT_ON_CLOSE );
        }
    
        public void actionPerformed(ActionEvent event) {
            String favorite;
            int    loc;
            favorite = (String) comboBox.getSelectedItem();
            loc      = comboBox.getSelectedIndex();
        //   JOptionPane.showMessageDialog(this, "Den valda bilden '" +
        //                      favorite + "' är i position " + loc);
         }
    
        public void itemStateChanged(ItemEvent event) {
            if (event.getStateChange() == ItemEvent.SELECTED) {
                
            	if(comboBox.getSelectedIndex() == 0){
                   	label1.setVisible(true);
                   	label2.setVisible(false);
                   	label3.setVisible(false);
                }
                
                if(comboBox.getSelectedIndex() == 1){
                	label1.setVisible(false);
                   	label2.setVisible(true);
                   	label3.setVisible(false);
                }
                
                if(comboBox.getSelectedIndex() == 2){
                	label1.setVisible(false);
                   	label2.setVisible(false);
                   	label3.setVisible(true);
                }
            }
        }
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,874
    Rep Power
    25

    Default Re: Images do not display

    Are you having problems with the layout manager not placing the JLabels where you want them to go?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Ranjiz is offline Member
    Join Date
    Jun 2012
    Posts
    4
    Rep Power
    0

    Default Re: Images do not display

    It works fine.... U should do lyk diz...
    imagePanel.add(label1);imagePanel.add(label2);imag ePanel.add(label3); instead of add(label1);, add(label2);add(label3);

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,874
    Rep Power
    25

    Default Re: Images do not display

    By changing containers, you are changing the layout manager.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Nilla is offline Member
    Join Date
    Jun 2012
    Posts
    13
    Rep Power
    0

    Default Re: Images do not display

    Thank you very much. So simple I had forgotten imagePanel.add.

    Now it, works well besides one little problem and that is that I can´t get the first picture if choose this picture as the first choice. however, if I choose another picture to begin wit and then the first one, it works fine. How do I do if I want the first picture as the default one?

    /Nilla

    Hi again,

    I have solved this problem now. Thank you for valuable help when I was blind.

    /Nilla
    Last edited by Nilla; 06-25-2012 at 04:28 PM.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: Images do not display

    Quote Originally Posted by Ranjiz View Post
    It works fine.... U should do lyk diz...
    Please write well. This is a programming forum, not SMS chat.

    Also, go through BB Code List - Java Programming Forum

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. How To Display An Array Of Images?
    By dofod in forum New To Java
    Replies: 2
    Last Post: 04-01-2012, 08:23 PM
  2. How to display Images?
    By Rectal Exambot in forum New To Java
    Replies: 12
    Last Post: 10-11-2010, 05:36 AM
  3. JavaHelp how to display images
    By fossildoc in forum New To Java
    Replies: 0
    Last Post: 04-04-2010, 07:42 AM
  4. display images
    By prof.deedee in forum AWT / Swing
    Replies: 10
    Last Post: 11-12-2009, 10:08 PM
  5. Dynamic display of images
    By gixerino in forum NetBeans
    Replies: 7
    Last Post: 01-15-2009, 03:55 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
  •