Results 1 to 10 of 10
Like Tree2Likes
  • 1 Post By Tolls
  • 1 Post By gimbal2

Thread: Using a Combobox, Array, and ImageIcon

  1. #1
    itkremit is offline Member
    Join Date
    Dec 2013
    Posts
    9
    Rep Power
    0

    Default Using a Combobox, Array, and ImageIcon

    I've looked at the book examples and I just don't get it.

    When I selected bert in the combobox, I want bert's image to load then display. Please advise

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class ImageViewer extends JFrame{
    private JPanel characterBoxPanel;
    private JPanel characterImagePanel;
    private JComboBox characterBox;
    private JButton randomButton;
    private JLabel charaterLabel;
    private final int WINDOW_WIDTH = 320;
    private final int WINDOW_HEIGHT = 240;
    
    private String[] character = { "Elmo", "Kermit", "Grover", "Bert", "Ernie"};
    
    //GUI setup
    public ImageViewer(){
    setTitle("Image Viewer");
    setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
    setLayout(new BorderLayout());
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    buildCharacterImagePanel();
    add(characterImagePanel, BorderLayout.CENTER);
    add(randomButton, BorderLayout.SOUTH);
    pack();
    setVisible(true);}
    
    //Combo box Method
    private void buildCharacterBoxPanel(){
    characterBoxPanel = new JPanel();
    characterBox = new JComboBox(character);
    characterBox.addActionListener(new ComboBoxListener());
    characterBoxPanel.add(characterBox);
    
    characterImagePanel = new JPanel();
    //charaterLabel = new JLabel();
    characterImagePanel.add(charaterLabel);
    
    
    }
    
    //event when item is selected in combo box
    private class ComboBoxListener implements ActionListener{
    public void actionPerformed(ActionEvent e){
    String selection = (String) characterBox.getSelectedItem();
    
    if(character.equals("Elmo")){
    ImageIcon elmoImage = new ImageIcon("Elmo.jpg");
    characterLabel.setIcon(elmoImage);}
    else if(character.equals("Kermit")){
    ImageIcon kermitImage = new ImageIcon("Kermit.jpg");
    characterLabel.setIcon(kermitImage);}
    else if(character.equals("Grover")){
    ImageIcon groverImage = new ImageIcon("Grover.jpg");
    characterLabel.setIcon(groverImage);}
    else if(character.equals("Bert")){
    ImageIcon bertImage = new ImageIcon("Bert.jpg");
    characterLabel.setIcon(bertImage);}
    else{
    ImageIcon ernieImage = new ImageIcon("Ernie.jpg");
    haracterLabel.setIcon(ernieImage);}
    
    
    pack();
    }}
    
    
    
    public static void main(String[] args){
    new ImageViewer();
    }}
    Last edited by itkremit; 12-03-2013 at 07:42 AM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Using a Combobox, Array, and ImageIcon

    Java Code:
    if(character.equals("Elmo")){
    I think you want to be testing "selection", not the "character" array.
    I'm guessing at the moment it keeps showing Ernie.
    gimbal2 likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    itkremit is offline Member
    Join Date
    Dec 2013
    Posts
    9
    Rep Power
    0

    Default Re: Using a Combobox, Array, and ImageIcon

    Quote Originally Posted by Tolls View Post
    Java Code:
    if(character.equals("Elmo")){
    I think you want to be testing "selection", not the "character" array.
    I'm guessing at the moment it keeps showing Ernie.
    Java Code:
    if(selection.equals("Elmo")){
    ImageIcon elmoImage = new ImageIcon("Elmo.jpg");
    characterLabel.setIcon(elmoImage);}
    ImageViewer.java:61: error: cannot find symbol
    characterLabel.setIcon(elmoImage);}
    ^
    symbol: variable characterLabel
    location: class ImageViewer.ComboBoxListener
    ImageViewer.java:64: error: cannot find symbol

    Thank you Tolls. I have this strange feeling my code is much more mixed up then this.

  4. #4
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,368
    Rep Power
    6

    Default Re: Using a Combobox, Array, and ImageIcon

    Oh it contains a simple typo. Errors are there to help you, not to intimidate you into becoming scared of the code. Read your code, the problems you are having are very simple if you but take the time to patiently go through it and reason about it.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    itkremit is offline Member
    Join Date
    Dec 2013
    Posts
    9
    Rep Power
    0

    Default Re: Using a Combobox, Array, and ImageIcon

    Wow... I've received more help here then my college and internet searches could offer! Thanks guys! Here is the running code:

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class ImageViewer extends JFrame{
    private JPanel characterBoxPanel;
    private JPanel characterImagePanel;
    private JComboBox characterBox;
    private JButton randomButton;
    private JLabel characterLabel;
    private JLabel selectedCharacter;
    private final int WINDOW_WIDTH = 320;
    private final int WINDOW_HEIGHT = 240;
    
    private String[] character = { "Elmo", "Kermit", "Grover", "Bert", "Ernie"};
    
    //GUI setup
    public ImageViewer(){
    setTitle("Image Viewer");
    setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
    setLayout(new BorderLayout());
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    buildCharacterImagePanel();
    buildCharacterBoxPanel();
    add(characterLabel, BorderLayout.CENTER);
    add(characterBox, BorderLayout.SOUTH);
    pack();
    setVisible(true);}
    
    private void buildCharacterImagePanel(){
    characterImagePanel = new JPanel();
    characterLabel = new JLabel();
    characterImagePanel.add(characterLabel);}
    
    //Combo box Method
    private void buildCharacterBoxPanel(){
    characterBoxPanel = new JPanel();
    characterBox = new JComboBox(character);
    characterBox.addActionListener(new ComboBoxListener());
    characterBoxPanel.add(characterBox);}
    
    //event when item is selected in combo box
    private class ComboBoxListener implements ActionListener{
    public void actionPerformed(ActionEvent e){
    String selection = (String) characterBox.getSelectedItem(); // get selected character
    
    
    if(selection.equals("Elmo")){
    ImageIcon elmoImage = new ImageIcon("Elmo.jpg");
    characterLabel.setIcon(elmoImage);}
    else if(selection.equals("Kermit")){
    ImageIcon kermitImage = new ImageIcon("Kermit.jpg");
    characterLabel.setIcon(kermitImage);}
    else if(selection.equals("Grover")){
    ImageIcon groverImage = new ImageIcon("Grover.jpg");
    characterLabel.setIcon(groverImage);}
    else if(selection.equals("Bert")){
    ImageIcon bertImage = new ImageIcon("Bert.jpg");
    characterLabel.setIcon(bertImage);}
    else{
    ImageIcon ernieImage = new ImageIcon("Ernie.jpg");
    characterLabel.setIcon(ernieImage);}
    
    
    pack();
    }}
    
    
    
    public static void main(String[] args){
    new ImageViewer();
    }}
    I am no longer depressed. Thank you so much guys.
    Last edited by itkremit; 12-03-2013 at 01:34 PM.

  6. #6
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,368
    Rep Power
    6

    Default Re: Using a Combobox, Array, and ImageIcon

    No need to ever get depressed - take it from me that being stuck is always a temporary thing.

    How long you're stuck depends on how you deal with it mentally. You have now found out that you might find support in this forum and that is true, but I honestly beg you to do everything in your power to solve problems yourself first before coming here; otherwise it will not be productive to your learning process. Keep in mind that it is more valuable to learn how to solve problems than getting problems you have solved.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  7. #7
    itkremit is offline Member
    Join Date
    Dec 2013
    Posts
    9
    Rep Power
    0

    Default Re: Using a Combobox, Array, and ImageIcon

    I know what you mean, but it took you guys pointing out the obvious for me to see what was wrong with it. I don't know how many times i rewrote the application thinking it was a process problem in my head. I have a problem finding food in the refrigerator in front of my nose. lol
    Last edited by itkremit; 12-03-2013 at 02:13 PM.

  8. #8
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,368
    Rep Power
    6

    Default Re: Using a Combobox, Array, and ImageIcon

    Quote Originally Posted by itkremit View Post
    I don't know how many times i rewrote the application thinking it was a process problem in my head.
    You are admitting here that in stead of thinking about the problem, you were trying to make it go away in stead. Learning moment: that doesn't work, don't do that anymore. Fix the problem you have, don't work around it. That's the only way you'll learn not to make the mistake again.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  9. #9
    itkremit is offline Member
    Join Date
    Dec 2013
    Posts
    9
    Rep Power
    0

    Default Re: Using a Combobox, Array, and ImageIcon

    Quote Originally Posted by gimbal2 View Post
    You are admitting here that in stead of thinking about the problem, you were trying to make it go away in stead. Learning moment: that doesn't work, don't do that anymore. Fix the problem you have, don't work around it. That's the only way you'll learn not to make the mistake again.
    But wouldn't that kind of thing come with experience?

  10. #10
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,368
    Rep Power
    6

    Default Re: Using a Combobox, Array, and ImageIcon

    No, you need to always think about it, you don't only do that when you have the experience. To quote the almighty JosAH: programming is not done by bashing your head on the keyboard.

    Believe me: working around a problem is a flight response. You need to fight.
    itkremit likes this.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Problem: Fill a comboBox from another combobox
    By Ridha in forum AWT / Swing
    Replies: 2
    Last Post: 03-08-2013, 03:34 PM
  2. ArrayStoreException with Object[][] array and ImageIcon
    By X75TIGER75X in forum AWT / Swing
    Replies: 10
    Last Post: 05-03-2012, 07:46 PM
  3. Need help with ImageIcon and int
    By proceeded in forum New To Java
    Replies: 9
    Last Post: 01-23-2011, 11:10 PM
  4. String array from file to ComboBox
    By cselic in forum AWT / Swing
    Replies: 3
    Last Post: 05-06-2010, 06:29 PM
  5. Replies: 0
    Last Post: 03-22-2010, 07:45 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •