java - How do I display a character array in a JTextField

Samer Alabi

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.

Madhawa Priyashantha

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I find a character followed by a character in a char array Java?

How to Display Message When i left JTextField in java

How do i display data inside array in java json

How do I display Label text character-by-character?

Java, Swing: how do I set the maximum width of a JTextField?

How do i convert a character array to a string?

How do I replace the " character in Java?

How do I make a string with a " character in it? (Java)

How do I display names starting with specific character in BQ

How do I display an array of encoded polylines?

How do I display an Array of Arrays in swiftUI

How do I display images from an array?

How do I display array object information?

How do I display the average of my array?

How do I make an array display properly?

How do I type a special character on top of a character in java(FX)

How do I read input character-by-character in Java?

How do I display a byte array as an array of hex bytes or unsigned decimal numbers in the Eclipse Java debugger?

How do I convert a byte array with null terminating character to a String in Java?

How do I display a google captcha in java?

How do I display a java ResultSet visually?

How do i get the value from JTextField in java that was created dynamically through a JLabel click

In C, how do I print out a character array then empty it after?

How do I convert multiple elements of a character array into an integer?

How do I split an array into smaller arrays using a specific character?

How do I sort an array by the first character in the string?

How do I change the character displayed in the 2D array?

How do I make Java display a display a quotient and a remainder?

How do I pass an array of character pointers as void *, then cast back to array of character pointers?