Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / January 2005

Tip: Looking for answers? Try searching our database.

Access swing component on other tab of JTabbedPane

Thread view: 
Mike Bosschaert - 07 Jan 2005 14:14 GMT
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



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.