a HashMap question

Discussion in 'Java' started by Shawn, Sep 11, 2006.

  1. Shawn

    Shawn Guest

    Hi,

    I am reading a java GUI file which has the following lines which I
    cannot follow:

    ....
    JTextField myTextField = new javax.swing.JTextField();
    HashMap dbFieldRelations = new HashMap();

    dbFieldRelations.put(myTextField,ProductInfo.class.getField("product_weight"));

    I don't understand two places:
    (1)My shallow knowledge about HashMap is such:
    myHashMap.put("1", "A"); //1 is the key corresponding to the value of A

    I don't understand why use a JTextField reference as a key.

    (2) ProductInfo is a class which holds all related information of a
    product. ProductInfo has a public Float product_weight. There are
    methods including getProduct_weight() in the class.

    I don't understand what ProdutctInfo.class.getField("product_weight") is
    talking about.

    Thank you very much.
     
    Shawn, Sep 11, 2006
    #1
    1. Advertising

  2. Shawn

    Oliver Wong Guest

    "Shawn" <> wrote in message
    news:ee46qs$lp6$...
    > Hi,
    >
    > I am reading a java GUI file which has the following lines which I cannot
    > follow:
    >
    > ...
    > JTextField myTextField = new javax.swing.JTextField();
    > HashMap dbFieldRelations = new HashMap();
    >
    > dbFieldRelations.put(myTextField,ProductInfo.class.getField("product_weight"));
    >
    > I don't understand two places:
    > (1)My shallow knowledge about HashMap is such:
    > myHashMap.put("1", "A"); //1 is the key corresponding to the value of A
    >
    > I don't understand why use a JTextField reference as a key.


    You can pretty much use anything as a key. Whoever wrote this class felt
    that the GUI widget controls themselves would be the most convenient key to
    use, so they used that.

    >
    > (2) ProductInfo is a class which holds all related information of a
    > product. ProductInfo has a public Float product_weight. There are methods
    > including getProduct_weight() in the class.
    >
    > I don't understand what ProdutctInfo.class.getField("product_weight") is
    > talking about.


    This has to do with reflection, which is a pretty complicated topic in
    Java. Basically, the hashmap is creating an an association between widget
    controls on the GUI form to fields in the class. This might be so that
    whenever someone edits a value in the control widget, that value will get
    updated into the class field, for example. It's hard to say exactly what is
    done with the association without seeing more code.

    - Oliver
     
    Oliver Wong, Sep 11, 2006
    #2
    1. Advertising

  3. Shawn

    Shawn Guest

    Thank you very much. Highly appreciate it.
     
    Shawn, Sep 11, 2006
    #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. hal@no_spam_statsbiblioteket.dk

    HashMap implementation question

    hal@no_spam_statsbiblioteket.dk, Jul 7, 2004, in forum: Java
    Replies:
    3
    Views:
    5,578
    Andrew Thompson
    Jul 8, 2004
  2. Vince Darley
    Replies:
    4
    Views:
    4,434
    emilchacko
    Mar 2, 2010
  3. JR
    Replies:
    4
    Views:
    702
  4. Rakesh
    Replies:
    10
    Views:
    12,184
    Mike Schilling
    Apr 8, 2008
  5. Andreas Leitgeb

    Question about HashMap and Map.Entry ...

    Andreas Leitgeb, Aug 30, 2012, in forum: Java
    Replies:
    15
    Views:
    700
    Robert Klemme
    Sep 1, 2012
Loading...

Share This Page