aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Need some advice with a JLabel issue. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Need some advice with a JLabel issue." Watch "Need some advice with a JLabel issue." New topic
Author

Need some advice with a JLabel issue.

Jordan Carl
Greenhorn

Joined: Nov 06, 2010
Posts: 4
// 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

Joined: Sep 08, 2007
Posts: 3054
    
  33

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[].

Mohamed Sanaulla | My Blog
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2090
    
    7
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

Joined: Nov 06, 2010
Posts: 4
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

Joined: Sep 08, 2007
Posts: 3054
    
  33

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

Joined: Nov 06, 2010
Posts: 4

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
Bartender

Joined: Oct 14, 2005
Posts: 18121
    
    8

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

Joined: Nov 06, 2010
Posts: 4

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!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need some advice with a JLabel issue.
 
Similar Threads
generics - incompatible types
Mouse shift event coding - to trigger a predetermined sequence of images to a GUI
Java Board Game
Problems using JTabbedPane In a GUI application
text box - GridBagLayout