a HashMap question

S

Shawn

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.
 
O

Oliver Wong

Shawn said:
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
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top