Home > Archive > Java Help > March 2008 > Handling private jtextfields
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Handling private jtextfields
|
|
| Jl_G_0 2008-02-27, 7:18 pm |
| Hey all. Trying a simple script here, but I have no idea on how to do
this.
I'm trying to create a script like this:
void setValue(String strName, String strText){
[strName].setText(strText);
}
let me explain:
strName should be converted to a JTextField (of course its not done
right there), I mean, I'll send the text field name as a parameter,
and I want to know if there's some function that searches my
components and returns the one with the same name as strName so I can
set its value.
In c#/asp.net I could use a function to search through my
components(foreach control c in page.controls...) , so its another way
to do it, IF I knew how to do this in java.
Is there any way that I can make this work ? Thanks.
| |
| Eric Sosman 2008-02-27, 7:18 pm |
| Jl_G_0 wrote:
> Hey all. Trying a simple script here, but I have no idea on how to do
> this.
> I'm trying to create a script like this:
>
> void setValue(String strName, String strText){
> [strName].setText(strText);
> }
>
> let me explain:
> strName should be converted to a JTextField (of course its not done
> right there), I mean, I'll send the text field name as a parameter,
> and I want to know if there's some function that searches my
> components and returns the one with the same name as strName so I can
> set its value.
>
> In c#/asp.net I could use a function to search through my
> components(foreach control c in page.controls...) , so its another way
> to do it, IF I knew how to do this in java.
Starting from any Container (probably the one at the top
of your GUI's containment hierarchy), you can apply the
getComponents() method to find every Component the Container
contains, and look through them. If a contained Component
is itself a Container, apply getComponents() to it in turn
and search recursively. Alternatively, use getComponentCount()
and getComponent() instead of getComponents().
However, I'm not sure what you're searching for. Every
Component has a "name," but the names can be changed at any
time and are not guaranteed to be unique. Unless you call
a Component's setName() method, I think its name will remain
null. You could adopt some scheme to give each of your
JTextFields a unique non-null name and then search for it,
but if you're going to that much trouble you might as well
just build yourself a HashMap of "names" to JTextFields.
What problem are you trying to solve? Where do these
"names" come from, and why can't you use a simpler (and more
robust) handle?
--
Eric.Sosman@sun.com
| |
| Jl_G_0 2008-02-27, 7:18 pm |
|
Ill give it a try.
The real problem is trying to set and get values from private
JTextFields from another class. Its not my code entirely, so I dont
know how much I can change on the structure.
| |
| Eric Sosman 2008-02-27, 7:18 pm |
| Jl_G_0 wrote:
>
> Ill give it a try.
>
> The real problem is trying to set and get values from private
> JTextFields from another class. Its not my code entirely, so I dont
> know how much I can change on the structure.
There's that word "private" again ...
Objects are not private or protected or public or
whatever; the access control mechanisms have nothing
to do with them. One easy way to see this is with a
simple (and rather silly) class:
class Silly {
private Integer priv = new Integer(42);
protected Integer prot = priv;
public Integer publ = priv;
Integer dflt = priv;
}
Each Silly instance "wraps" an Integer instance -- but
is that Integer private, or protected, or public, or
package-private? It's just one Integer object, so it
can't have all these different access levels at the
same time! In fact, the Integer has *no* access level;
accessibility doesn't mean anything for objects.
So when you refer to "private JTextFields from
another class," I don't know what you're talking about.
And I rather suspect you don't know, either: it seems
likely that you're trying to understand Java by applying
notions from another framework, and that the analogies
are not close enough to be useful.
--
Eric.Sosman@sun.com
| |
| Mark Space 2008-02-27, 7:18 pm |
| Jl_G_0 wrote:
>
> Ill give it a try.
>
> The real problem is trying to set and get values from private
> JTextFields from another class. Its not my code entirely, so I dont
> know how much I can change on the structure.
Well, that's an issue.
The maker of the other class should have provided a method for you to
change the text field. If they did not, then there may be a reason.
Like, you aren't supposed to change it. Or, they are incompetent.
Can you give us more context? Is this class developed in house? Did
you download it somewhere? Can you point us at the relevant Javadoc for
the class?
In a pinch, you can use reflection, but that's pretty extreme. Let's
try some more reasonable methods first.
| |
| Jl_G_0 2008-02-28, 8:20 am |
| >
> The maker of the other class should have provided a method for you to
> change the text field. =A0If they did not, then there may be a reason.
> Like, you aren't supposed to change it. Or, they are incompetent.
>
Well, I believe I can add this method myself after all, but what if I
had 10 textfields, would it be right to create 10 methods, or maybe 20
(gets AND sets for each textfield) ? What is the right way to do it ?
Any good place with guidelines for this stuff ?
| |
| Jeff Higgins 2008-02-28, 8:20 am |
|
Jl_G_0 wrote:
> Hey all. Trying a simple script here, but I have no idea on how to do
> this.
> I'm trying to create a script like this:
>
> void setValue(String strName, String strText){
> [strName].setText(strText);
> }
>
> let me explain:
> strName should be converted to a JTextField (of course its not done
> right there), I mean, I'll send the text field name as a parameter,
> and I want to know if there's some function that searches my
> components and returns the one with the same name as strName so I can
> set its value.
>
> In c#/asp.net I could use a function to search through my
> components(foreach control c in page.controls...) , so its another way
> to do it, IF I knew how to do this in java.
>
> Is there any way that I can make this work ? Thanks.
You want to retrieve a reference to a JTextField by 'name' ?
Here's an idea or two, may be way off base.
You have a JPanel. MyJPanel
and you want to add 20 JTextFields to it.
String nameJTextField1 = "JTEXTFIELD1"
JTextField jTextField1 = new JTextField();
MyJPanel.add(jTextField1);
Map<String, JTextField> textfieldMap =
new HashMap<String, JTextField>();
textfieldMap.put(nameJTextField1, jTextField1);
void setValue(String strName, String strText){
textfieldMap.get(strname).setText(strText);
}
Or ask the Container for a list of Components it contains.
Component[] ca = MyJPanel.getComponents()
for(Component c : ca) {
if(it's the Component you want)
doSomething();
}
Or maybe even better use the java.awt Event system.
| |
| Roedy Green 2008-03-07, 7:23 pm |
| On Wed, 27 Feb 2008 10:11:03 -0800 (PST), Jl_G_0
<jlguerreiro@gmail.com> wrote, quoted or indirectly quoted someone who
said :
>In c#/asp.net I could use a function to search through my
>components(foreach control c in page.controls...) , so its another way
>to do it, IF I knew how to do this in java.
you can assign names to your Components with Component.setName
You can then search for particular names by scanning
Container.getComponents
Back in the days of Java 1.0 I used this field to declare labels for
fields that would automatically display under certain conditions.
It gets a bit hairy when you think about internationalisation. You
can't use the field both for ID and for display.
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
| |
| Mark Space 2008-03-08, 4:37 am |
| Jl_G_0 wrote:
>
>
> Well, I believe I can add this method myself after all, but what if I
> had 10 textfields, would it be right to create 10 methods, or maybe 20
> (gets AND sets for each textfield) ? What is the right way to do it ?
I'm going to guess almost certainly not. Eric's suggestion (pervious
post) was to call getComponents() on the top level object and iterate
over the result.
> Any good place with guidelines for this stuff ?
Your design document?
I'm curious, what "name" are you looking for? Who is setting this name?
As Eric points out it's very rare for anyone to set a component's
name. If you are asking what we think you are asking, you've got a
rather funky design.
My suspicion is that you aren't asking what we think you are, and so
we're giving you some bad advice. Can you posts a short, complete
example we can compile and run ourselves? That might answer a lot of
questions.....
|
|
|
|
|