Tuesday, April 14, 2015

Method to return object's human readable name?


Dear Experts,


I've been doing internet searches and experimenting for

a good two hours now.


I'd like the code that would return an object's human readable name.


ie. myObject.getObjectsName();


Which would return the name of the object.

That is, the name of the object that I use as I code.


In the code below, I'm trying to find the correct code to return the string,

btnTTT_01


The results of the testing follow.

NO luck so far.


Is this possible in Java?

If so, what is the secret?


Thanks a lot!


-----


public static void main(String []args) {


tictactoe game ;

game = new tictactoe();

game.setVisible(true);


System.out.println ("main") ;

System.out.println ( "game.getName() is: " + game.getName() ) ;


System.out.println ( " " );


System.out.println ( "game.btnTTT_01.toString() is: " + game.btnTTT_01.toString() ) ;


System.out.println ( "game.btnTTT_01.getName() is: " + game.btnTTT_01.getName () ) ;


System.out.println ( "game.btnTTT_01.getClass().getSimpleName() is: "

+ game.btnTTT_01.getClass().getSimpleName() ) ;


System.out.println ( "game.btnTTT_01.getClass().getCanonicalName() is: "

+ game.btnTTT_01.getClass().getCanonicalName() ) ;


System.out.println ( "game.btnTTT_01.getClass().getFields() is: "

+ game.btnTTT_01.getClass().getFields() );


} // end main


main

game.getName() is: frame0


game.btnTTT_01.toString() is: javax.swing.JButton[,1,1,130x103,alignmentX=0.0,alignmentY=0.5,border= javax.swing.plaf.BorderUIResource$CompoundBorderUI Resource@40055f9f,flags=296,maximumSize=,minimumSi ze=,preferredSize=,defaultIcon=,disabledIcon=,disa bledSelectedIcon=,margin=javax.swing.plaf.InsetsUI Resource[top=2,left=14,bottom=2,right=14],paintBorder=true,paintFocus=true,pressedIcon=,rol loverEnabled=true,rolloverIcon=,rolloverSelectedIc on=,selectedIcon=,text=,defaultCapable=true]

game.btnTTT_01.getName() is: null

game.btnTTT_01.getClass().getSimpleName() is: JButton

game.btnTTT_01.getClass().getCanonicalName() is: javax.swing.JButton

game.btnTTT_01.getClass().getFields() is: [Ljava.lang.reflect.Field;@6744719c



No comments:

Post a Comment