wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes int to char 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 » Beginning Java
Bookmark "int to char" Watch "int to char" New topic
Author

int to char

Imtiaz Nizami
Greenhorn

Joined: May 19, 2007
Posts: 12
I have the following code which works fine:

cellPanel.add( new JButton(new CharIcon(new Character('1'))) );
cellPanel.add( new JButton(new CharIcon(new Character('2'))) );
cellPanel.add( new JButton(new CharIcon(new Character('3'))) );
cellPanel.add( new JButton(new CharIcon(new Character('4'))) );
cellPanel.add( new JButton(new CharIcon(new Character('5'))) );
cellPanel.add( new JButton(new CharIcon(new Character('6'))) );
cellPanel.add( new JButton(new CharIcon(new Character('7'))) );
cellPanel.add( new JButton(new CharIcon(new Character('8'))) );
cellPanel.add( new JButton(new CharIcon(new Character('9'))) );

I need to change it to something like:
for (int i=0; i<9; i++)
{
cellPanel.add( new JButton(new CharIcon(new Character(i))) );
}

except that there is no Character constructor that takes in an integer. Is there a quick solution? I want to avoid Strings and Character Arrays. Thanks.
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
In the for loop, the i isn't required by Java to be an int. (It could be a different data type, like the one you actually want it to be.)

Or...

When you create the new Character, you could cast the int i to a different data type.
[ June 10, 2007: Message edited by: Dirk Schreckmann ]

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Bupjae Lee
Ranch Hand

Joined: May 14, 2007
Posts: 107
Maybe you can use this code

Nicholas Jordan
Ranch Hand

Joined: Sep 17, 2006
Posts: 1282
Originally posted by Dirk Schreckmann:
In the for loop, the i isn't required by Java to be an int.


Correct. This compiles:



"The differential equations that describe dynamic interactions of power generators are similar to that of the gravitational interplay among celestial bodies, which is chaotic in nature."
Imtiaz Nizami
Greenhorn

Joined: May 19, 2007
Posts: 12
Thank you both.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: int to char
 
Similar Threads
help help
Not able to access an Array of buttons from another class
what layouts to use
Access an array, from different class. How?
Adding JButton Array to a applet issue