Welcome to Dream.In.Code
Become a Java Expert!

Join 150,423 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,099 people online right now. Registration is fast and FREE... Join Now!




Strings and objects

 
Reply to this topicStart new topic

Strings and objects, Changing a String to a JButton object

theplaymaker
28 Mar, 2008 - 11:43 PM
Post #1

New D.I.C Head
*

Joined: 9 Sep, 2007
Posts: 9


My Contributions
Hi

I am trying to make a boardgame, but I am struggling to figure out how I can convert a String to a JButton object. The reason for this is that when the user clicks on "roll dice" a number from 1-6 will be generated. The program must make a string of the block name such as "btn7" depending on the places it moves e.g. if the user was on btn4 and rolls 3 it must move to btn7. After the String "btn7" is created I need to convert it to a JButton object to display an image.

The application that I am using is NetBeans 6.0.

Thanks
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Strings And Objects
29 Mar, 2008 - 09:07 AM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,660



Thanked: 314 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Well you got to realize that a button and string are radically two different things. The only thing you can really do is create a button that has a string for its text. To create such a thing you would use code similar to this...

java

String newbutton = "btn7";
JButton anewbutton = new JButton(newbutton);


This would create a new button control that has the text "btn7" as its text. I am not sure if this is what you are asking or not, but again the two objects you are playing with here are radically different. String is a series of characters and a button is a control class.

I hope this helps! smile.gif
User is offlineProfile CardPM
+Quote Post

theplaymaker
RE: Strings And Objects
29 Mar, 2008 - 09:32 AM
Post #3

New D.I.C Head
*

Joined: 9 Sep, 2007
Posts: 9


My Contributions
Thanks, I appreciate your help.

Maybe I can give you another example, but from your message it seems this cannot be done.

Here is an example (this will set btnDice to display the image pic):
java

private void btnDiceActionPerformed(Java.awt.event.ActionEvent evt)
{
ImageIcon pic = new ImageIcon ("bat.JPG");
javax.swing.JButton temp = (JButton) evt.getSource();
temp.setIcon(pic);
}



I have made buttons (these represent a block on the board) in the GUI such as btn1, btn2, btn3, etc.
If the number generated by the dice is a 4 and the user is on btn7, then I would like to make a String such as "btn" + (num + curr). Num is the number from the dice that will be converted to a String and curr is the current position the user is on. Therefore, the String should be btn11. I need to change temp so that it can be btn11.setIcon(pic); I want to use the String name for the object name.

Thanks
User is offlineProfile CardPM
+Quote Post

gl3thr0
RE: Strings And Objects
30 Mar, 2008 - 12:17 PM
Post #4

D.I.C Head
**

Joined: 27 Oct, 2007
Posts: 229



Thanked: 5 times
My Contributions
hey
um im not sure if this will work but try it

im guessing the reason you want set the name of the Object is so that you can use it in an action performed method yes??

if so then there is a simple maybe a little dangerous way bt that can be fixed, way of getting around the problem

java

public void actionPerformed(ActionEvent evt){
JButton src = (JButton) evt.getSource();//the dangerous line..
//this can be deadly because the evt might not becoming from a button..
//however this can be fixed reallly easily let me know if this is wt u want 2 do
//nd ill show you how 2 make this fool proof

System.out.println(src.getText());
//returns the name of the Button that you gave to the constructor
//this means that you can use it in a if statement eg:
if(src.getText().equals("btn11")){
//do shit here
}
}


what this means is that you can now use the button text instead of the object name. i think this should solve your problem???
but then again i dont understand what you want 2 do..
could you provide an image of the game and maybe an example play???
~~~~~~~~~~~~~~~~~~~

just as a side note... if the reason you dont want 2 use the Button text is because you dont want it to show up in the game then that is an easy fix aswell
we can extend the JButton class, add a constructor value(or just a new method) nd then a way 2 use this new value


This post has been edited by gl3thr0: 30 Mar, 2008 - 12:20 PM
User is offlineProfile CardPM
+Quote Post

theplaymaker
RE: Strings And Objects
5 Apr, 2008 - 11:10 AM
Post #5

New D.I.C Head
*

Joined: 9 Sep, 2007
Posts: 9


My Contributions
Howzat

Thanks for the help.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 08:38PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month