Hi,
For my small application I want to use tabs. On one tab I display 'default
values' in JTextFields. On another tab I have a button which should do some
actions, based on the default values. I tried to write a method to collect
the desired default value, but I can't seem to find the right way. I see
why it does not work but I'm on the wrong track and can't see which way I
should go. The method I tried is given below.
Thanks for you help!
Mike
public static String getValueFromPanel(String panelName, String fieldName) {
String fieldValue="";
try {
Class cl = Class.forName("MyMusic");
Field[] f = cl.getFields();
for (int j = 0; j < f.length; j++) {
if (f[j].getType().toString().equals("class javax.swing.JPanel")) {
for (int i = 0; i < f[j].getComponentCount(); i++) {
if (f[j].getComponent(i).getName() == fieldName) {
fieldValue = f[j].getComponent(i).toString();
}
}
} else {
return "object: " + f[j].getType() + " not handled by method";
}
}
} catch (Exception ex){System.out.println(ex);};
return fieldValue;
}
klynn47@comcast.net - 07 Jan 2005 17:25 GMT
Are both tabs created in the same class definition? If so, then you
could make the textfields and buttons instance variables in the class
definition. Then they could be accessed in any instance method in the
class definition.
Mike Bosschaert - 07 Jan 2005 19:06 GMT
Yes, They are accessible if I point to them directly. The following code
works:
fieldName="myDefaultValue"
for (int i=0; i<defaultTab.getComponentCount();i++) {
if (defaultTab.getComponent(i).getName() == fieldName) {
JTextField tf = (JTextField)defaultTab.getComponent(i);
fieldValue = tf.getText();
tf = null;
}
}
I however want to pass the tabname (in this case defaultTab) as a string to
the method.
> Are both tabs created in the same class definition? If so, then you
> could make the textfields and buttons instance variables in the class
> definition. Then they could be accessed in any instance method in the
> class definition.
Nigel Wade - 10 Jan 2005 10:44 GMT
> Hi,
> For my small application I want to use tabs. On one tab I display 'default
[quoted text clipped - 15 lines]
> for (int j = 0; j < f.length; j++) {
> if (f[j].getType().toString().equals("class javax.swing.JPanel"))
why not use instanceof?
> {
> for (int i = 0; i < f[j].getComponentCount(); i++) {
> if (f[j].getComponent(i).getName() == fieldName) {
You are trying to compare strings with ==, use equals().

Signature
Nigel Wade, System Administrator, Space Plasma Physics Group,
University of Leicester, Leicester, LE1 7RH, UK
E-mail : nmw@ion.le.ac.uk
Phone : +44 (0)116 2523548, Fax : +44 (0)116 2523555