Solved

How to access object methods

Posted on 2004-04-25
25
174 Views
Last Modified: 2013-11-23
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
Comment
Question by:mqhopewell
  • 8
  • 5
  • 5
  • +1
25 Comments
 
LVL 92

Expert Comment

by:objects
ID: 10914638
you could store your buttons in a map and use:

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

Author Comment

by:mqhopewell
ID: 10914735
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
 
LVL 92

Expert Comment

by:objects
ID: 10914768
- 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
 
LVL 92

Accepted Solution

by:
objects earned 125 total points
ID: 10915544
Reflection may be an option to you if your file format property names matched method names.
0
 
LVL 5

Expert Comment

by:Daij-Djan
ID: 10915961
Acccess not per name if you want to rename them...
or assemble the JPanel and THEN the map
0
 

Author Comment

by:mqhopewell
ID: 10917098
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
 
LVL 5

Expert Comment

by:Daij-Djan
ID: 10917144
if( component.getClass().toString == "JButton" )
 btn = (JButton)...

or
try / catch
0
 

Author Comment

by:mqhopewell
ID: 10917170
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
 
LVL 37

Expert Comment

by:zzynx
ID: 10917666
>> 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
 

Author Comment

by:mqhopewell
ID: 10917709
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
 
LVL 5

Expert Comment

by:Daij-Djan
ID: 10917733
sorry, I dont get the question.
Maybe someone else does.
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Author Comment

by:mqhopewell
ID: 10917788
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
 
LVL 5

Expert Comment

by:Daij-Djan
ID: 10917874
write a getComponentsByText method for JPanel :)
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10917926
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
 

Author Comment

by:mqhopewell
ID: 10918017
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
 
LVL 37

Expert Comment

by:zzynx
ID: 10918206
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
 

Author Comment

by:mqhopewell
ID: 10919311
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
 
LVL 5

Expert Comment

by:Daij-Djan
ID: 10920180
Find common attributes
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10922302
>> 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
 
LVL 92

Expert Comment

by:objects
ID: 10922891
> What about using reflection to check if the object has a function setText()?

yes i suggested that earlier.
0
 

Author Comment

by:mqhopewell
ID: 10923551
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
 
LVL 92

Expert Comment

by:objects
ID: 10923611
> 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
 
LVL 37

Expert Comment

by:zzynx
ID: 10925777
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now