Results 1 to 16 of 16
- 08-24-2011, 02:01 PM #1
Member
- Join Date
- Apr 2011
- Posts
- 69
- Rep Power
- 0
using string representation of component to access its methods
Hello everyone
Everytime i make an application, i always create a database structure to hold all the text in 2 languages. The way i accomplish that is by holding all the component names in one table and another holding the langagues. I then match the IDs in a relation table with the corresponding text.
However when Java gets involved all i have is a string representation of the component name i'm trying to access. What i need to do is basically iterate through the component list i retrieve and compare such as :
Of course if i have hundreds of components that method becomes enormous and gets me wondering if there is a simpler way to do this.Java Code:if (retrievedComponentString.compareTo("mnuFile")) { mnuFile.setText(getLanguageText(rertievedComponentString, retrievedComponentLanguage)) }
So the question is : Is there a way to access a components methods if i only have a String representation of its name?!
- 08-24-2011, 02:08 PM #2
Member
- Join Date
- Apr 2011
- Posts
- 69
- Rep Power
- 0
BTW i googled this for a long time but did not come up with anything. It's hard to think of proper keywords to look for too...
- 08-24-2011, 02:31 PM #3
Member
- Join Date
- Apr 2011
- Posts
- 69
- Rep Power
- 0
Do i have to iterate through all the content pane's component and do something like :
Java Code:if (component.getName().compareTo("retrievedComponentString") == 0) { ... } or is there a better way?
- 08-24-2011, 02:38 PM #4
Why don't you just add them to a Map?
Also, you don't have to do that == 0 part. Just use equals() or equalsIgnoreCase().How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 08-24-2011, 03:11 PM #5
Member
- Join Date
- Apr 2011
- Posts
- 69
- Rep Power
- 0
- 08-24-2011, 03:23 PM #6
What I meant was, when you initialize the components, add them to the HashMap. Or even if you did iterate through the content pane, at least you'd only have to do it once instead of at the beginning.
But if you add them to the HashMap at initialization time, that also helps in case you're adding/removing components on the fly- it doesn't sound like you are, but you might want to add that in the future, and your current idea wouldn't support that.How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 08-24-2011, 03:27 PM #7
Actually, you don't even need them in a Map. Just add them to a List, and when you change the language, iterate over the List, setting the text of each component based on its name. You wouldn't even need any if statements.
Wait.. you wouldn't need any if statements at all, would you? Why do you use them?How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 08-24-2011, 10:25 PM #8
Member
- Join Date
- Apr 2011
- Posts
- 69
- Rep Power
- 0
the reason i needed the conditions was because i didn't think of adding them to a map! Very good idea, that way i can create a map with the component and it's name as the key, iterate through my table and match with the corresponding value in the map!
- 08-25-2011, 07:10 AM #9
Sounds to me like reinventing java.util.ResourceBundlea database structure to hold all the text in 2 languages
db
- 08-25-2011, 01:13 PM #10
Member
- Join Date
- Apr 2011
- Posts
- 69
- Rep Power
- 0
are you telling me there's a built in feature to do what i'm trying to do?! Oh well my way is more fun but thx Darryl i'll look it up.
- 09-16-2011, 05:15 AM #11
Member
- Join Date
- Dec 2008
- Posts
- 64
- Rep Power
- 0
Re: using string representation of component to access its methods
The short answer to your original question:
"So the question is : Is there a way to access a components methods if i only have a String representation of its name?!"
Maybe, you need to use reflection, if your gui components are declared class level or globally you can, if they are declared within a method then you have to use a more custom solution. To start if you use reflection you will have to get an instance of the class object that represents your active object, then look up the declared fields on that object, change their access level so you can access them whether or not they are privately declared and then call your methods on these objects. It sounds more complicated than it is.
If your gui objects are created at the method level then you will have to take an approach of setting the swing components name to the same name as the variable name, then you can recursively traverse the root container's components looking for the object with the given name to find the specific object you need.
Are either of these solutions ideal...probably not for what you are doing, there is probably a better design that will meet your goals but depending on your situation and circumstances one of the solutions above will work.
- 09-18-2011, 05:15 PM #12
Member
- Join Date
- Apr 2011
- Posts
- 69
- Rep Power
- 0
Re: using string representation of component to access its methods
Hey you've got a point! I was able to list all the fields of the class (including the components!) using :
How would i then call setText() on any of those components since they are just string representations at this point? I managed to make the concept work by adding the components to a map <String, Component> as previously suggested but your idea seems much more elegant!Java Code:private void listFields() { Field[] fields = this.getClass().getDeclaredFields(); for (Field f: fields) { System.out.println(f.getName()); } }
Thanks!
- 09-18-2011, 05:34 PM #13
Member
- Join Date
- Dec 2008
- Posts
- 64
- Rep Power
- 0
Re: using string representation of component to access its methods
Here is an example of how...
PHP Code:import java.lang.reflect.Field; import javax.swing.JTextField; public class Test { private JTextField myTextField = new JTextField("This is some text"); /******************************* * STATIC METHODS *******************************/ public static void main(String[] args) throws Exception { //get the field by name Field field = Test.class.getDeclaredField("myTextField"); //set accessible so we can modify a private field, this field is private but since we are not //technically changing the field itself we do not have to call it, only if we were changing the //field would we have to call this. //field.setAccessible(true); //create an object in which to modify the field since we don't have //any, in your code you may already have a reference to an instance of the //class that has already been created. Test test = new Test(); //display the test objects textfield text test.showText(); //change the test objects's myTextField's text ((JTextField)field.get(test)).setText("This text has been modified"); //display the test object's textfield text now test.showText(); } /******************************* * INSTANCE METHODS *******************************/ public void showText() { System.out.println(myTextField.getText()); } }Last edited by fxRichard; 09-18-2011 at 05:38 PM.
- 09-18-2011, 06:13 PM #14
Member
- Join Date
- Apr 2011
- Posts
- 69
- Rep Power
- 0
Re: using string representation of component to access its methods
WHAT you can typecast a field to a component? Sweet! Can you test them using instanceof to see if they are JLabels, JButtons etc? Reflection is truly awesome!
- 09-18-2011, 06:21 PM #15
Member
- Join Date
- Dec 2008
- Posts
- 64
- Rep Power
- 0
Re: using string representation of component to access its methods
Yes you can typecast and use instanceof, and I would highly recommend that put in error/exception checking etc. My code above is simply an example of how to do what you are asking :-)
- 09-21-2011, 01:25 AM #16
Member
- Join Date
- Apr 2011
- Posts
- 69
- Rep Power
- 0
Re: using string representation of component to access its methods
Well it works like a charm, here's my code (using hibernate classes):
Some of these classes are my own but this basic structure can be applied to anything!Java Code:try { Field[] fields = this.getClass().getDeclaredFields(); for (Field f: fields) { Object obj = f.get(this); if (obj instanceof JComponent) { Composant comp = Composant.getComposant(f.getName()); if (comp != null) { if (obj instanceof JMenu) { ((JMenu)obj).setText(Composant_Langage.getTranslation(comp, langage)); } else if (obj instanceof JMenuItem) { ((JMenuItem)obj).setText(Composant_Langage.getTranslation(comp, langage)); } else if (obj instanceof JButton) { ((JButton)obj).setText(Composant_Langage.getTranslation(comp, langage)); } else if (obj instanceof JLabel) { ((JLabel)obj).setText(Composant_Langage.getTranslation(comp, langage) + " : "); } } } } } catch (Exception ex) { Logs.getFrameLog().log(Level.SEVERE, "Error translating component " + comp + " : ", ex); }
Thanks again!
Similar Threads
-
Java Representation
By lary in forum New To JavaReplies: 2Last Post: 05-08-2011, 07:28 AM -
Memory representation of an array?
By hellolleh in forum Forum LobbyReplies: 2Last Post: 04-29-2010, 03:05 AM -
Ask for help on Java access to protected methods
By fangzhong in forum Advanced JavaReplies: 3Last Post: 02-17-2009, 01:50 PM -
graphical representation of website structure
By mrpwnt in forum New To JavaReplies: 0Last Post: 12-09-2008, 10:08 PM -
[SOLVED] Unable to access array defiened in constructor in other methods.
By Shyam Singh in forum New To JavaReplies: 1Last Post: 07-20-2008, 04:42 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks