Confused on how to reference info stored in a JFrame

J

jamestechinfo

I've tried to develop several JFrame programs in the past and it
always drives me crazy because I can't figure out how to set Classes
I've developed, which aren't Components, but are critical to storing
information necessary for the program to operate, so that they can be
seen by components inside the frame. I'm not very knowledgable about
the structure and coding of Swing/AWT programs, but I understand the
princepals of passing data between methods and classes, and it is very
frustrating to me that I cannot find an easy way (or even a working
way at this point) to reference the info with my program the way I
need to.

To illustrate my question with a real example, I'm trying to write a
program which allows employees at my job to easily receive products
from deliveries by scanning the barcode on the product or putting in
the supplier code on the invoice.

The program needs to be able to extract product info from the database
(to match the scanned barcode and to give the receiver visual
verification that the info in the system is correct), and it needs to
be able to match the product with the vendor, and thus the vendor
codes, which go with the invoice.

I've connected my first draft/test program to the database using a
class I call dbInfo. This class stores the connection information
specific to our database, and also acts as a container/manager for
other classes which populate arraylists such as VendorList, BrandList,
and lists for any other info which is tied to a primary key in the
database and might change during the use of the program (this way in
the info in the form can be refreshed by simply using one dbInfo
method).

My dbInfo class populates all its info into the appropriate lists
correctly. I cannot, however, get my components to see this info.
For example, I have a VendorBox, which is a JComboBox that selects the
vendor for the invoice.

My original vendorBox grabbed its vendor information directly from the
database by storing its own JDBC info and generating the SQL statement
itself. This worked fine, but is not very modular, I had to add code
to every other component that dealt with this info which would to
reference the specific VendorBox on that JFrame form. If I could get
the components to see the dbInfo for the form, I could just store the
reference to VendorBox as dbInfo.invoiceVendor and any other component
could have easy access to it by simply knowing where dbInfo was.

Long explanations can be hard to read, so here's a basic diagram

------------------------------
JFrame
|
}Components
| }VendorBox ( a ComboBox which selects the
| vendor to receive from )
|
}dbInfo (not a component)
}VendorList
------------------------------
My Question: How to I get VendorBox to see VendorList (or dbInfo for
that matter)?
------------------------------

*Note: I'm using Netbeans 5.5, which doesn't allow me to edit the
component initialisation proccess inline. I think I might be able to
do this using the properties window, but I haven't had any luck
getting this to work*

What I've tried:

Just putting it in based on its name and location:

1) A direct reference - using a setdbInfo() method, which sets the
dbInfo equal to JFrameName.dbInfoVariableName. Netbeans gives me a
variable (or symbol or something of the like) not found.

2) The Netbeans property window - because I have setdbInfo() and
getdbInfo() fields in my VendorBox class, Netbeans shows dbInfo in the
property window. Trying to edit this property gives me a form which
among other things says 'form connection mode' and has options like
'from property' and 'from method'. None of the options have any
options to select within them, which I think is because i need to do
something to set up the form connections, but I'm not sure and my own
personal searches have not presented me with useful information.

At this point I thought I'd try to make a finddbInfo class, which
would just find the dbInfo class by crawling its way out

3) Introspection - When I read the description of this I sighed
relief, I thought it would show me the class that my VendorBox was
inside, but as far as I can tell it only shows you the class
inheritance information. I tried all the getxxx() commands netbeans
offered for code completion, but all I got back was info from the
classes my vendorBox was built on or null.

4) VendorBox.getParent() - combined with introspection, I used this to
progress a Class from displaying info about my vendorBox to info about
its containing panels, when I progressed to the outmost panel,
however, instead of getting the JFrame info on the next entry, I got
null.

At this point I cried, cut myself, and poured the rest of my drink out
for my dead homies.

Please help me, I enjoy using Java because I can work with it quickly,
but I feel like screaming sometimes because I can't pass a simple
piece of info to a component. I've learned so much more than I needed
to and wasted so much valuable time trying to do this and I know in my
heart that there is an easy answer that will make me feel
simultaneously stupid and relieved. I need that feeling like you
wouldn't believe, and I'm sure there our other amateur programers out
there who could benefit greatly by understanding why my methods don't
work.

On that note, if you could even explain why solutions like 1) or 4)
don't work, I think a lot of people could benefit from that info.
 
E

Eric Sosman

[...]
My dbInfo class populates all its info into the appropriate lists
correctly. I cannot, however, get my components to see this info.
For example, I have a VendorBox, which is a JComboBox that selects the
vendor for the invoice.

My original vendorBox grabbed its vendor information directly from the
database by storing its own JDBC info and generating the SQL statement
itself. This worked fine, but is not very modular, I had to add code
to every other component that dealt with this info which would to
reference the specific VendorBox on that JFrame form. If I could get
the components to see the dbInfo for the form, I could just store the
reference to VendorBox as dbInfo.invoiceVendor and any other component
could have easy access to it by simply knowing where dbInfo was.
[...]

It seems you may not quite have grasped the idea of
separating the "view" from the "model." Instead of extending
JComboBox with a VendorBox that understands your application's
data (and may even own it, in some sense), use an ordinary
JComboBox and connect it to a VendorComboBoxModel that extends
DefaultComboBoxModel (or implements ComboBoxModel "from scratch.")

This separation addresses your problem with every gadget
needing to talk with every other gadget. Instead, each talks
to the ComboBoxModel instance, fetching data from it and posting
updates to it. To stay current with updates made, an "interested"
component registers various kinds of event listeners on the model.
When some part of the model changes, the model notifies all the
relevant event listeners, and they in turn update their
components. A lot of the pre-canned listeners and events are
strongly connected to GUI actions -- ActionListener and such --
but something like a ChangeListener makes a pretty good way to
connect components to data models.

The separation also allows non-GUI parts of your program to
interact with the model. You mentioned a bar code scanner that
(presumably) isn't managed through Swing, but its classes could
perfectly well look something up in a database and send the
information to the model, which would in turn notify all the
components that had declared an interest by registering an event
listener. Going the other way, if the operator makes a selection
or types in a text field or something, the data could update the
model -- and if the database has registered an event listener for
the update, it will be notified of the change and can generate
some SQL to update a table or something.

Reading between the lines of your frustration, I have a hunch
you might benefit from a couple sections of the Java Tutorial:

http://java.sun.com/docs/books/tutorial/uiswing/components/model.html

http://java.sun.com/docs/books/tutorial/uiswing/events/index.html

.... and there's also a worthwhile article at

http://java.sun.com/products/jfc/tsc/articles/architecture/
 

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,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top