#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    78
    Rep Power
    2

    Help with Hangman JPanel


    In the buttonListener(), how can I show the character with dashes ( - ) in the JLabel. When I do the System.out.print, it works the way I want, but not in the JLabel.

    For example,

    say the random word was "india"

    I want the JLabel to show i - - i - just like the System.out....

    Code:
    package hangman;
    
    import java.awt.Dimension;
    import java.awt.Font;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import java.util.Random;
    
    public class hangmanPanel extends JPanel{
        
        String[] wordsArray = new String[] {"lion", "cat", "india", "fat", "mouse", "cheetah", "airplane", "toy", "jump", "push", "elephant"};
        
        String[] alphabetArray = new String[] {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", 
                                               "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", };
        
        String[] dashArray = new String[] {" - "};
        
        ImageIcon[] imageArray = new ImageIcon[] {new ImageIcon("zero.png"), new ImageIcon("one.png"), new ImageIcon("two.png"), 
                                                  new ImageIcon("three.png"), new ImageIcon("four.png"), new ImageIcon("five.png"), 
                                                  new ImageIcon("six.png")};
        public JLabel imageLabel, dashLabel, alphabetLabel, label;
        public JTextField guess;
        public JButton play;
        public int selectWord;
        public String showWord, dashString, alphaString;
        public char getLetter, checkLetter;
        
        public hangmanPanel()
        {
            Random rand = new Random();
            selectWord = rand.nextInt(wordsArray.length) + 0;
            
            showWord = wordsArray[selectWord];
                   
            System.out.print(showWord + " , ");      
            
            dashLabel = new JLabel();
            alphabetLabel = new JLabel();
            guess = new JTextField(6);
            label = new JLabel("Enter a character.");
            play = new JButton("Try");       
            imageLabel = new JLabel(imageArray[0]);    
    
            alphaString = "";
            
            for(int a = 0; a < alphabetArray.length; a++)
            {
                  alphaString = alphaString + alphabetArray[a] + " ";
            }
            
            dashString = "";
                    
            for(int b = 0; b < showWord.length(); b++)
            {
                dashString = dashString + " - ";
                checkLetter = showWord.charAt(b);   
                
                System.out.print("\t" + checkLetter);
            }
            
            System.out.println();
            
            dashLabel.setFont(new Font("Helvetica", Font.PLAIN, 48));
            dashLabel.setText(dashString);
            alphabetLabel.setFont(new Font("Helvetica", Font.PLAIN, 32));
            alphabetLabel.setText(alphaString);
            
            add(alphabetLabel);
            add(dashLabel);
            add(label);
            add(guess);        
            add(play);
            play.addActionListener(new buttonListener());
            add(imageLabel);
                    
            setPreferredSize(new Dimension(650, 500));
        }
        
        public class buttonListener implements ActionListener
        {
            @Override
            public void actionPerformed(ActionEvent e) 
            {
                getLetter = guess.getText().charAt(0);
                
                for(int c = 0; c < showWord.length(); c++)
                {
                    if(getLetter == showWord.charAt(c))
                    {
                        dashString = getLetter + " ";
                        dashLabel.setText(dashString);
                        
                        System.out.print(getLetter + " ");
                        
                        // remove getLetter from alphabetArray
                    }  
                    else
                    {
                        dashString = dashString + " - ";
                        System.out.print(" - ");
                        
                        // show next image in imageArray
                    }     
    
                }
            }        
        }    
    }
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,713
    Rep Power
    348
    When I do the System.out.print, it works the way I want, but not in the JLabel.
    What does the code do now? What is put in the JLabel?
    Last edited by NormR; September 19th, 2013 at 07:05 AM.
  4. #3
  5. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,663
    Rep Power
    1958
    Code:
    dashString = getLetter + " ";
    You forgot to add combine it with dashString:
    Code:
    dashString = dashString + getLetter + " ";
    This loop:
    Code:
    for(int c = 0; c < showWord.length(); c++)
    should only create the dashString.

    After the loop, you can then display they string using JLabel and/or System.out.print.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    78
    Rep Power
    2
    I've tried that earlier, but its's not giving the correct output display.

    Thank you,

    Tony

    Originally Posted by MrFujin
    Code:
    dashString = getLetter + " ";
    You forgot to add combine it with dashString:
    Code:
    dashString = dashString + getLetter + " ";
    This loop:
    Code:
    for(int c = 0; c < showWord.length(); c++)
    should only create the dashString.

    After the loop, you can then display they string using JLabel and/or System.out.print.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    78
    Rep Power
    2
    Originally Posted by NormR
    What does the code do now? What is put in the JLabel?
    Let's the random word selected from the wordsArray[] was "cat".
    I've entered "c" into the JTextField, but the JLabel only shows

    "c", no remaining dashes. I want it to show

    c - -

    Even if the word contains more than 1 of the same character, "india", the JLabel only shows one "i", but the System.out shows
    i--i-.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    78
    Rep Power
    2
    I've changed the code to

    dashString = getLetter + dashString;

    but now if the word is "india"

    the JLabel shows ii-------

    Where does the extra -s come from? Everything works fine when the program is in System.out.print format, but the GUI is making it confusing.

    Code:
    package hangman;
    
    import java.awt.Dimension;
    import java.awt.Font;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import java.util.Random;
    
    public class hangmanPanel extends JPanel{
        
        String[] wordsArray = new String[] {"lion", "cat", "india", "fat", "mouse", "cheetah", "airplane", "toy", "jump", "push", "elephant"};
        
        String[] alphabetArray = new String[] {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", 
                                               "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", };
        
        String[] dashArray = new String[] {" - "};
        
        ImageIcon[] imageArray = new ImageIcon[] {new ImageIcon("zero.png"), new ImageIcon("one.png"), new ImageIcon("two.png"), 
                                                  new ImageIcon("three.png"), new ImageIcon("four.png"), new ImageIcon("five.png"), 
                                                  new ImageIcon("six.png")};
        public JLabel imageLabel, dashLabel, alphabetLabel, label;
        public JTextField guess;
        public JButton play;
        public int selectWord;
        public String showWord, dashString, alphaString;
        public char getLetter, checkLetter;
        
        public hangmanPanel()
        {
            Random rand = new Random();
            selectWord = rand.nextInt(wordsArray.length) + 0;
            
            showWord = wordsArray[selectWord];
                   
            System.out.print(showWord + " , ");      
            
            dashLabel = new JLabel();
            alphabetLabel = new JLabel();
            guess = new JTextField(6);
            label = new JLabel("Enter a character.");
            play = new JButton("Try");       
            imageLabel = new JLabel(imageArray[0]);    
    
            alphaString = "";
            
            for(int a = 0; a < alphabetArray.length; a++)
            {
                  alphaString = alphaString + alphabetArray[a] + " ";
            }
            
            dashString = "";
                    
            for(int b = 0; b < showWord.length(); b++)
            {
                dashString = dashString + " - ";
                checkLetter = showWord.charAt(b);   
                
                System.out.print("\t" + checkLetter);
            }
            
            System.out.println();
            
            dashLabel.setFont(new Font("Helvetica", Font.PLAIN, 32));
            dashLabel.setText(dashString);
            alphabetLabel.setFont(new Font("Helvetica", Font.PLAIN, 32));
            alphabetLabel.setText(alphaString);
            
            add(alphabetLabel);
            add(dashLabel);
            add(label);
            add(guess);        
            add(play);
            play.addActionListener(new buttonListener());
            add(imageLabel);
                    
            setPreferredSize(new Dimension(650, 500));
        }
        
        public class buttonListener implements ActionListener
        {
            @Override
            public void actionPerformed(ActionEvent e) 
            {
                getLetter = guess.getText().charAt(0);
                
                for(int c = 0; c < showWord.length(); c++)
                {
                    if(getLetter == showWord.charAt(c))
                    {
                        dashString = getLetter + dashString;
                        dashLabel.setText(dashString);
                        
                        System.out.print(getLetter + " ");
                        
                        // remove getLetter from alphabetArray
                    }  
                    else
                    {
                        dashString = dashString + " - ";
                        System.out.print(" - ");
                        
                        // show next image in imageArray
                    }     
    
                }
            }        
        }    
    }
  12. #7
  13. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,713
    Rep Power
    348
    To see what is happening, add a println() statement that prints out the value of dashString every time its value is changed. Make sure to have a unique id String with every println() so you know which println statement printed the line:
    Code:
      System.out.println("unique id here " + dashString);
    There should only be one call to the setText() method outside of the loop after the value of dashString has been built.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    78
    Rep Power
    2
    Originally Posted by NormR
    To see what is happening, add a println() statement that prints out the value of dashString every time its value is changed. Make sure to have a unique id String with every println() so you know which println statement printed the line:
    Code:
      System.out.println("unique id here " + dashString);
    There should only be one call to the setText() method outside of the loop after the value of dashString has been built.
    I did a
    Code:
      System.out.println("unique id here " + dashString);
    everytime the value of dashString was changed like you said. It's being changed when I call the
    Code:
    buttonListener();
    . It depends which character in the wordsArray I select. If the word was "cat", and I enter "c", the final value of dashString comes out to "c--", if I enter "a", the final value fo dashString comes out to "a-".
  16. #9
  17. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,713
    Rep Power
    348
    It looks like the code is adding the letter at the front of the String instead of at the end of the String as it is being built.
    Looking for the word CAT if the user enters A then the String as it is built should be
    -
    -A
    -A-

IMN logo majestic logo threadwatch logo seochat tools logo