Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Get member variable names, and their reference

Reply
Thread Tools

Get member variable names, and their reference

 
 
Ashraf Fouad
Guest
Posts: n/a
 
      01-07-2004
Dears,
I have JComponent object I want to get all its internal components
and their member variable names. i.e.:
Lets consider

import javax.swing.*;
import java.awt.GridLayout;

public class MainWindow
extends JDialog
{
private GridLayout gridLayout1 = new GridLayout();
private JLabel jLabel1 = new JLabel();
private JLabel jLabel2 = new JLabel();
private JTextField jTextField1 = new JTextField();
private JTextField jTextField2 = new JTextField();

public MainWindow()
{
this.getContentPane().setLayout(gridLayout1);
gridLayout1.setColumns(2);
gridLayout1.setRows(2);
jLabel1.setText("jLabel1");
jLabel2.setText("jLabel2");
jTextField1.setText("jTextField1");
jTextField2.setText("jTextField2");
this.getContentPane().add(jLabel1, null);
this.getContentPane().add(jTextField1, null);
this.getContentPane().add(jLabel2, null);
this.getContentPane().add(jTextField2, null);

this.show();
}

public static void main(String[] args)
{
MainWindow mainWindow = new MainWindow();
}
}


All I want is 2 Arrays:
========================
- one holding member field names (String) l_Names containing [
"jLabel1", "jLabel2", "jTextField1", "jTextField2" ]
- second holding internal components (Objects) l_Components containing
[ ref to jLabel1, ref to jLabel2, ref to jTextField1,

ref to jTextField2 ]

I can get the first Vector by:
String[] l_Names = new String();
Field[] l_Fields = MainWindow.getClass().getDeclaredFields();
for ( int i = 0; i < l_Fields.length; i++)
{
l_Names[i] = l_Fields[i].getName();
}

The second I can get by:
Component[] l_Components = MainWindow.getComponents();


The problem is that I can't gaurantee that the order of the two tables
are the same ??
Does anyone have a solution or another approach ??

Thankx
 
Reply With Quote
 
 
 
 
Robert Olofsson
Guest
Posts: n/a
 
      01-07-2004
Ashraf Fouad <> wrote:
: public class MainWindow
: extends JDialog
: {
: public static void main(String[] args)
: {
: MainWindow mainWindow = new MainWindow();
: }
: }


: I can get the first Vector by:
: String[] l_Names = new String();
: Field[] l_Fields = MainWindow.getClass().getDeclaredFields();
: for ( int i = 0; i < l_Fields.length; i++)
: {
: l_Names[i] = l_Fields[i].getName();
: }


Field also makes it possible to get the value it points to.

l_Fields[i].get (mainWindow);

Where mainWindow is the instance of MainWindow you want to inspect.

/robo
 
Reply With Quote
 
 
 
 
Ashraf Fouad
Guest
Posts: n/a
 
      01-08-2004
> Field also makes it possible to get the value it points to.
>
> l_Fields[i].get (mainWindow);
>
> Where mainWindow is the instance of MainWindow you want to inspect.



I saw this method but I don't understand it at all.
And if I have already an instance of Field I know ==> where will be
the problem

All I want is a Field name and instance of the object itself so that I
can call methods in it depending on the field name where I can get the
customization I need from XML file !!!
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What are the naming convention for private member variable, andprivate and public member function? Peng Yu Python 3 09-21-2009 04:49 AM
A reference to a hash member? (to an object's member variable) A. Farber Perl Misc 5 08-23-2008 07:00 AM
ASP.NET 2.0: master pages and web user controls: reference to a non-shared member requires an object reference bminder ASP .Net 0 06-24-2005 12:22 AM
How would I use qsort to sort a struct with a char* member and a long member - I want to sort in order of the long member Angus Comber C Programming 7 02-05-2004 06:41 PM
What the pros use to power their flashes... and their digital cameras. Dan Sullivan Digital Photography 21 01-04-2004 04:40 PM



Advertisments