Results 1 to 6 of 6
Thread: Images do not display
- 06-25-2012, 10:58 AM #1
Member
- Join Date
- Jun 2012
- Posts
- 13
- Rep Power
- 0
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); } } } }
- 06-25-2012, 02:34 PM #2
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.
- 06-25-2012, 02:37 PM #3
Member
- Join Date
- Jun 2012
- Posts
- 4
- Rep Power
- 0
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);
- 06-25-2012, 02:47 PM #4
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.
- 06-25-2012, 03:21 PM #5
Member
- Join Date
- Jun 2012
- Posts
- 13
- Rep Power
- 0
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.
/NillaLast edited by Nilla; 06-25-2012 at 03:28 PM.
- 06-25-2012, 04:39 PM #6
Re: Images do not display
Please write well. This is a programming forum, not SMS chat.
Also, go through BB Code List - Java Programming Forum
dbWhy do they call it rush hour when nothing moves? - Robin Williams
Similar Threads
-
How To Display An Array Of Images?
By dofod in forum New To JavaReplies: 2Last Post: 04-01-2012, 07:23 PM -
How to display Images?
By Rectal Exambot in forum New To JavaReplies: 12Last Post: 10-11-2010, 04:36 AM -
JavaHelp how to display images
By fossildoc in forum New To JavaReplies: 0Last Post: 04-04-2010, 06:42 AM -
display images
By prof.deedee in forum AWT / SwingReplies: 10Last Post: 11-12-2009, 09:08 PM -
Dynamic display of images
By gixerino in forum NetBeansReplies: 7Last Post: 01-15-2009, 02:55 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks