Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need some advice with a JLabel issue.

 
Jordan Carl
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
// please ignore the request for resolved, was a mis-click. //

Hi, I am trying to set the text of a JLabel as a String array.



However, it gives me the error (setText(java.lang.String) in javax.swing.JLabel cannot be applied to (java.lang.String[]))

So I know that I must not be able to directly set it as such.
Can anyone lend a hand in what I can do to accomplish my goal?
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The compiler has clearly mentioned that it cannot take a String array(String[]). You can have a look at the API for JLabel and you can see that the label should be a String. One way to approach would be to flatten the String[] into String- by looping through and concatenating the elements of the String[].
 
Rob Camick
Ranch Hand
Posts: 2612
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone lend a hand in what I can do to accomplish my goal?


What exactly is your goal?

Are you trying to display multiple lines of text in a single JLabel?

Are you trying to convert multiple lines of text into a single line in a JLabel?

Something else?
 
Jordan Carl
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Camick wrote:
Can anyone lend a hand in what I can do to accomplish my goal?


What exactly is your goal?

Are you trying to display multiple lines of text in a single JLabel?

Are you trying to convert multiple lines of text into a single line in a JLabel?

Something else?


Almost, I'm making a lightweight hangman game;
and the array I want it to display would be the remaining spaces in whichever word you're guessing.

i.e. "_ _ _ _"

Then I'm going to update the label every time the player guesses a letter correctly.

"a _ _ _"

But I'm not sure how to do this with JLabel.

 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mohamed sanaullah wrote:One way to approach would be to flatten the String[] into String- by looping through and concatenating the elements of the String[].

You could try this.
 
Jordan Carl
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

You could try this.


I'm not trying to make one long word, I want to be able to change each index of it.
I'm quite a novice, so is that possible with just a straight String?
 
Paul Clapham
Sheriff
Posts: 21318
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jordan Carl wrote:I'm not trying to make one long word, I want to be able to change each index of it.
I'm quite a novice, so is that possible with just a straight String?


You seem to be confused between the "view" and the "model" of your program. The JLabel is part of the view. You give it a String as the text it should display. But there is nothing at all which says you must also use that same String in your "model" -- the part of your program which does the work. You could perfectly well have an array of chars in your model, and whenever that changes you produce a String and pass that to the JLabel's setText() method.
 
Jordan Carl
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

You seem to be confused between the "view" and the "model" of your program. The JLabel is part of the view. You give it a String as the text it should display. But there is nothing at all which says you must also use that same String in your "model" -- the part of your program which does the work. You could perfectly well have an array of chars in your model, and whenever that changes you produce a String and pass that to the JLabel's setText() method.


Thank you, that makes a lot of sense.

I will use both you and Mohamed's advice!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic