Get member variable names, and their reference

A

Ashraf Fouad

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 = l_Fields.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
 
R

Robert Olofsson

: 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 = l_Fields.getName();
: }


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

l_Fields.get (mainWindow);

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

/robo
 
A

Ashraf Fouad

Field also makes it possible to get the value it points to.
l_Fields.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 !!!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top