Get member variable names, and their reference

Discussion in 'Java' started by Ashraf Fouad, Jan 7, 2004.

  1. Ashraf Fouad

    Ashraf Fouad Guest

    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
    Ashraf Fouad, Jan 7, 2004
    #1
    1. Advertising

  2. 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 = 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
    Robert Olofsson, Jan 7, 2004
    #2
    1. Advertising

  3. Ashraf Fouad

    Ashraf Fouad Guest

    > 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 !!!
    Ashraf Fouad, Jan 8, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michael Krasnyk

    Print variable values by their names

    Michael Krasnyk, Oct 18, 2004, in forum: Python
    Replies:
    1
    Views:
    270
    Peter Otten
    Oct 18, 2004
  2. Noah
    Replies:
    2
    Views:
    267
  3. Sfdesigner Sfdesigner
    Replies:
    5
    Views:
    163
    Chris Shea
    Aug 13, 2007
  4. Sandman

    input names with '-' in their names.

    Sandman, Sep 3, 2003, in forum: Javascript
    Replies:
    4
    Views:
    104
    Lasse Reichstein Nielsen
    Sep 4, 2003
  5. A. Farber
    Replies:
    5
    Views:
    171
    A. Farber
    Aug 23, 2008
Loading...

Share This Page