• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Need some advice with a JLabel issue.

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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[].
 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
reply
    Bookmark Topic Watch Topic
  • New Topic