Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   a HashMap question (http://www.velocityreviews.com/forums/t369885-a-hashmap-question.html)

Shawn 09-11-2006 05:36 PM

a HashMap question
 
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.

Oliver Wong 09-11-2006 05:43 PM

Re: a HashMap question
 

"Shawn" <shaw@nospam.com> wrote in message
news:ee46qs$lp6$1@news.nems.noaa.gov...
> 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


Shawn 09-11-2006 06:16 PM

Re: a HashMap question
 
Thank you very much. Highly appreciate it.


All times are GMT. The time now is 01:35 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.