2

I'm trying to create a hangman game where dashes will display for the user and if they click on the correct button, a letter will replace the dashes. This worked when I was using the console to output all the data, but after I changed to GUI, it wouldn't work no matter what I tried. It seems that the JTextField won't accept a character array.

    private static JTextField txtDashes;
    static char [] dashes = {'-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-'};

            txtDashes = new JTextField();
            txtDashes.setBackground(Color.GRAY);
            txtDashes.setHorizontalAlignment(SwingConstants.CENTER);
            txtDashes.setBorder(javax.swing.BorderFactory.createEmptyBorder());
            txtDashes.setBounds(39, 207, 218, 46);
            gameScreen.add(txtDashes);
            txtDashes.setColumns(10);

            for (int i = 0; i < dashes.length; i++){
                   txtDashes.setText(dashes[i]); //Error occurs here
            } //End of for loop

I'm only experienced in about 5 months of java coding and can't seem to find a solution for this problem.

3
  • Can you elaborate on "it wouldn't work no matter what I tried"? Are you getting an error? The wrong output? Commented Jun 30, 2015 at 6:28
  • 1
    What kind of error your get? Commented Jun 30, 2015 at 6:28
  • 1
    Well, the answer is "covert it to a string first". Not a very useful question/title is it? Make the title mean something per the actual problem. Commented Jun 30, 2015 at 6:29

3 Answers 3

1

you should pass a String to setText() method dashes is a char array.you can't set a char to a textfield .you have to convert char to String first

use

txtDashes.setText(String.valueOf(dashes[i])); 

or

txtDashes.setText(dashes[i] + ""); 

edit


setText() replace exiting text and set new text.if you want to show all array chars, first append it to a string throw a loop and from outside of loop setText()

like this

String s="";
for (int i = 0; i < dashes.length; i++){
    s+=dashes[i];
} 
txtDashes.setText(s); 

note .string append inside a loop is not good you can use StringBuilder for that

Sign up to request clarification or add additional context in comments.

3 Comments

An output was created using your method, but it only 1 dash was displayed instead of the 12 that I needed to be displayed.
@SamerAlabi how do you want to display them .as ------ or - - - - or ??
I want them to display as ------
0

Your can use Fast Snail method or you can use String instead of char.

static String [] dashes = {"-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-"};

Comments

0

Remove for loop in your code. Instead use Arrays.toString to convert array to String:

txtDashes.setText(Arrays.toString(dashes));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.