Getting 'parent' component from focus on object
I have a Class called configComponent which has a subclass called configString.
When a new instance of configString is created, a textfield and associated focuslistener is created. Also a Field called Description is created.
When a gainedFocus event is triggered, the only thing I seem to be able to do is access the textfield and its fields by calling the getSource() method. I cannot acces the original "parent" of the Textfield in order to get the Description field.
I have tried calling the getComponent() method, but I can not figure out what to do with the returned Component, because I cannot cast it to a congifComponent in order to access its methods.
Is there anyway of accessing the Description field after focus is given to this textfield?
I hope this is not too confusing.
Thanks,
Marcus
[849 byte] By [
Mag748a] at [2007-11-26 21:44:57]

Does the getParent method of Component not work? I'm assuming that ConfigString is a Container, and you add the textfield to it as a child. When you get the parent Container, cast it to a ConfigString.
The getParent() method returns a container which, just like the Component, I am not sure what to do with.
I dont really know if the configString class is a container, but I wouldnt think so. The objects that is creates get added to a JPanel for display.
Whenever I try to cast to configComponent or configString, I get and error stating that it is an Invalid cast.
BTW, I am using the pre1.3 compiler.
What does ConfigComponent extend? Component? Can you post the code for those two classes?
> Whenever I try to cast to configComponent or
> configString, I get and error stating that it is an
> Invalid cast.
It sounds like you are not where you think you are in the component tree.
Why not look before you leap?
if (var instanceof X) {
X x = (X) var;
...
} else {
System.err.println("unexpected type: " + var.getClass());
...
}
> BTW, I am using the pre1.3 compiler.
Granddad, the current version is 1.6 (=6)!
>It sounds like you are not where you think you are in the component >tree. Why not look before you leap?
Ok, I do believe I am very lost in the tree of Components. Is there a map or something?
> Granddad, the current version is 1.6 (=6)!
Actually, I am not a grandfather, nor a father. But I do work for them, and they dont like upgrading.
Here is some ...'ed code that will maybe clear up what I am trying to do.
static class configComponent { // Superclass
protected String variableName, Description, componentType;
protected JTextField textField; // Common Variable name textField.
configComponent(String name, String type) {
variableName = name;
componentType = type;
}
public String getType() { // Returns String "List" "Range" ...
return componentType;
}
public String getName() {
return variableName;
}
String getSelected() { // Here to get overridden
return null;
}
public String getDescription() {
return null;
}
}
static class configString extends configComponent { // Allows User to enter a String of there choice into the Condig file.
configString(String name, String longName, String Description, String Original) {
super(name, "String");
Description = Desc;
...
}
public String getDescription() {
return Description;
}
}
static FocusListener flistener = new FocusListener() {
public void focusGained(FocusEvent e) {
... // sets a JTextArea to Description
}
public void focusLost(FocusEvent e) {
}
};
Thanks,
Marcus
I'm confused: class configComponent is not derived from JComponent,
so how could it be in the component tree? I guess I still don't get
what you are trying to do.
I'm sure someone else will mention this, but there's a strong coding
standard bias to capitalizing the first letter of class and interface names!
I am trying to access fields in the class that created the textfield that has gotten focus.
If I could store a hidden string field in the JTextField Component this would all be a lot easier.
Sorry for the nonstandard capitalization practices. I am "New To Java".
Will try to remember that.
I also misunderstood what your code was doing. I though that the ConfigString was a Component that you added to the gui, and that contained the textfield. If that was the case you could use the component heirarchy to retrieve the "parent", but that way you're using it, it's not actually the parent. You don't have any reference back to the configstring from the textfield. Two options off the top of my head are to subclass the textfield and add those attributes directly to it, or use a map that stores the textfields and their corresponding configstrings. I don't recommend turning ConfigString into a Component since it has no gui functionality.
I'm not super-clear on what you are doing, but this may be s start:
I would define a class that implements FocusListener and has
in its state those string attributes. Create one such listener object
for every component that needs it and have it listen to that component. I
don't know if you need to define more than one listener class in this solution;
start with one and see what happens.
HEY!!!I subclassed the textfield class and added a Description field. Haha, I actually said that before but didnt actually think that was possible. Forgot that Java is the best like that.Thanks!!!!-Marcus
Sorry DrLaszloJamf, but you were a little too slow on that last post, lol. Thanks for the help!!!!-Marcus
I'm glad you've got something working, but I would caution against
subclassing, say subclassing JTextField in this case, unless you really
need to -- say you have to override a method in the class.
The trouble with subclassing just to add an attribute like description is that:
1. If you subclass JTextField to add other attributes you may end up
with a exponential explosion of subclasses! One for each subset of attributes.
2. You may have a JTextField that you want to add this focus behaviour to,
but you can't replace its construction as new JTextField() with new
MyTextField() -- perhaps it's in library code.
It's also simpler, design-wise to add a focus listener than to subclass
a component type.
Just my two cents...