Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 197
  • Last Modified:

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.
0
mqhopewell
Asked:
mqhopewell
  • 8
  • 5
  • 5
  • +1
1 Solution
 
objectsCommented:
you could store your buttons in a map and use:

JButton b = (JButton) map.get(name);
b.setText(valString);
0
 
mqhopewellAuthor Commented:
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?
0
 
objectsCommented:
- 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.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
objectsCommented:
Reflection may be an option to you if your file format property names matched method names.
0
 
Daij-DjanCommented:
Acccess not per name if you want to rename them...
or assemble the JPanel and THEN the map
0
 
mqhopewellAuthor Commented:
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?



0
 
Daij-DjanCommented:
if( component.getClass().toString == "JButton" )
 btn = (JButton)...

or
try / catch
0
 
mqhopewellAuthor Commented:
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.
0
 
zzynxSoftware engineerCommented:
>> 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????
0
 
mqhopewellAuthor Commented:
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".
0
 
Daij-DjanCommented:
sorry, I dont get the question.
Maybe someone else does.
0
 
mqhopewellAuthor Commented:
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.
0
 
Daij-DjanCommented:
write a getComponentsByText method for JPanel :)
0
 
zzynxSoftware engineerCommented:
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);
...
0
 
mqhopewellAuthor Commented:
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);

0
 
zzynxSoftware engineerCommented:
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!");
        }
0
 
mqhopewellAuthor Commented:
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?
0
 
Daij-DjanCommented:
Find common attributes
0
 
zzynxSoftware engineerCommented:
>> 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?
0
 
objectsCommented:
> What about using reflection to check if the object has a function setText()?

yes i suggested that earlier.
0
 
mqhopewellAuthor Commented:
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.
0
 
objectsCommented:
> 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.
0
 
zzynxSoftware engineerCommented:
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.
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 8
  • 5
  • 5
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now