Link to home
Start Free TrialLog in
Avatar of mqhopewell
mqhopewell

asked on

How to access object methods

Hi people,

I have a JPanel, called "workspace". On workspace I have added 5 JButtons, called "btn1" to "btn5".
I'm reading in a file so perhaps one line would be:

[OBJECT: btn3]->(TEXT)->[STRING:"I am button 3"]

Therefore I can extract the name "btn3" and the string "I am button 3".

However, using these two strings how do I access access JButton btn3 on the workspace and set its text to "I am button3" on the work?

Note: Because the objects could be named and be any JSwing object I DO NOT want to be using a format as such:

IF (objString.equals("btn3") ) {
   btn3.setText(valString);
}

I just want to be able to reference the current object on the workspace and set its property using the two strings.

Any help is appreciated.
Avatar of Mick Barry
Mick Barry
Flag of Australia image

you could store your buttons in a map and use:

JButton b = (JButton) map.get(name);
b.setText(valString);
Avatar of mqhopewell
mqhopewell

ASKER

Hi,

Thanks for your reply. However, this would not work because this would set the text before I add it to the JPanel workspace. I want to be able to set the text after the button has been added to the workspace. Furthermore, can I please stress that the object could be anything at all so typecasting using (JButton) will not be suitable for this. For example, I wouldn't want to use (JButton) if I the designer of the cg file can chose JLabels, JButtons, JScrollPane (the designer can choose whatever they like).

In Summary:
- reference the object and set its text AFTER it has been added to the JPanel
- do not use explicit typecasting.

Thanks for you help.. any other ideas?
- reference the object and set its text AFTER it has been added to the JPanel

Using a map would allow that

- do not use explicit typecasting.

You cannot avoid typecasting as not all classes have a setText() method.
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Acccess not per name if you want to rename them...
or assemble the JPanel and THEN the map
OK, I cannot avoid using typecasting the JSwing object. But is there a way in which I can typecast by using an additional string? This is because I could use reflection to find the object's class.

eg, using String myObject = "JButton"; perhaps use myObject in some way to typecast an object? Could this be done?



if( component.getClass().toString == "JButton" )
 btn = (JButton)...

or
try / catch
Hi,

I want to be able to typecast using the string. I do not want to be using IF comparisons... there are just too many JSwing GUI Objects so lots of IF statements would be required!

Cheers anyway.
>> I want to be able to typecast using the string
You mean the string "I am button 3" must give you enough info to know to what object to cast to????
Sorry, bad example. Let me provide another example:

[OBJECT: btn3]->(TEXT)->[STRING:"Object's text has been changed"]

Now I want to set the object "btn3" (we don't know if it is a JButton, JLabel etc) text to "Object's text has been changed". How do I reference object btn3 already placed on the workspace. Member objects provided me with an example, but provided explicit typecasting. I could in fact get the object instantiating class eg JButton, but how would I typecast it using a string - it could be "JButton", "JLabel" or anything so the next query is how to typecast using a string parameter ge "JButton" which will then be able to let me access the workspace object by its string name "btn3".
sorry, I dont get the question.
Maybe someone else does.
Current objects technique to reference the GUI object name using description

[OBJECT: btn3]->(TEXT)->[STRING:"Object's text has been changed"]

Code below:

JButton b = (JButton) map.get(name);
b.setText(valString);

Ok so we can do that. However, note (JButton) - we do not know it is JButton though by [Object: btn3]. But presume we could get the class and find it is a JLabel or what about JScollPane? Then the code above will not work. If we could typecast using a string then we can use something like the above, but I haven't found anything that can typecast using a string parameter - that's the next target. Any ideas?

Hope that's ok.
write a getComponentsByText method for JPanel :)
Not clear yet.
>> If we could typecast using a string...
And where does that string come from?  Also from the description file?

[OBJECT: btn3;JButton]->(TEXT)->[STRING:"Object's text has been changed"]
                         ^
                   Like this?

What about:

if ( map.get(name) instanceof JButton )
     ((JButton)map.get(name)).setText(valString);
else if ( map.get(name) instanceof JCheckBox )
     ((JCheckBox)map.get(name)).setText(valString);
...
Using the below file input:

[BUTTON: jbtnOne]-
  (TEXT)->[STRING:"Object's text has been changed]

So I create a new class BUTTON extends JButton so essentially create new JButton. I have a BUTTON method called BUTTON.setRelationConcept(String attr, String val). So I can set the values and properties of this new class which is fair enough.

Yes, I can instantiate it say "jbtnOne" and yes it can be added to the workspace JPanel. I can also derive its superclass which is JButton.

Now how do I reference "jbtnOne" on the workspace JPanel? That's the main target. I do not want to be providing lots of IF statements, since there are hundreds of objects making my file very big! I just want to reference the object using a string. Member objects provided one, but too much typecasting. The java idea is shown below:

String objectType = "JButton"
String workspaceObj = "btn3"
String valString = "Object's text has been changed"

objectType b = (objectType) map.get(workspaceObj ); <<< but we don't know how to typecast using objectType string as yet!
b.setText(valString);

What about using reflection to check if the object has a function setText()?

     try {
                Class[] classArgs = new Class[1];
                classArgs[0] = shadow.getClass();
                Method method = map.get(name).getClass().getMethod("setText", classArgs);

                // Set the tet
                Object[] methodArgs = new Object[1];
                methodArgs[0] = valString;
                method.invoke(map.get(name), methodArgs);
        }
        catch (InvocationTargetException ex) {
            System.out.println("no setText() function available!");
        }
And hwo would I cater for the other hundreds of methods that all the JSwing objects have? For example, the above code settles for one method "setText" for one JSwing object "JButton". Imagine if we need to cater for all the other methods of "JButton" and imagine we need to cater for all other JSwing objects... there are lots and lots and lots and lots to cater for :S.

Difficult isn't it?
Find common attributes
>> the above code settles for one method "setText" for one JSwing object "JButton"
Correction: the above code looks for and triggers one method "setText" *FOR ALL OBJECTS*
[e.g. If
             map.get(name)
 returns a JCheckBox it will also work]
I thought you wanted to set the text of Swing objects, no?
> What about using reflection to check if the object has a function setText()?

yes i suggested that earlier.
Yes I wanted to set the text, correct. However, I wanted to set other methods. Setting the methods is fine. All I want to do is reference the GUI object on the workspace without having to typecast. However, I will need to typecast but can I typecast using a string variable and WITHOUT having to use IF comparisons for each object method.
> can I typecast using a string variable and WITHOUT having to use IF comparisons for each object method.

no and no.

the only way I can think of to avoid typecasting is to use reflection.
I agree: no you can't.

>> All I want to do is reference the GUI object on the workspace without having to typecast.
With my comment about reflection you *don't* typecast.
You trigger the setText() function for *ALL possible objects* that support setText().
>> I wanted to set other methods
All right. Then you'll have to try them all.
You can also use reflection to get all available methods an object has.