Beans binding problem - JavaBeans (Archived)

I have the following situation:
- A static class "Data" with reference to a class "Basic"
- A JPanel which includes a reference to Basic from Data (Data.getBasic())
- Inside of JPanel all form elements are bind to the reference of Basic
- Now another process is creating a new instance of Basic and is calling Data.setBasic(newBasic)
The problem now is that all bind elements inside the JPanel are not aware that the reference of Basic is invalid. A call to update the basic reference inside JPanel is not changing anything.
A refresh on my bindinggroup does not have any effect.
Does anybody have a solution for that problem ? 

- A static class "Data" with reference to a class "Basic"A static reference? There's the problem right there. Data is only designed to be used with one instance of Basic. If you have a need for > 1 instance of Basic you have to redesign Data. 

I do not want more than one instance of basic. The problem is that the reference of basic can be replaced by the server component and therefore I need a way to update the GUI elements. 

I do not want more than one instance of basic.Maybe not, but you have got more than one instance of Basic. So your design has to cope. As you've described it here, it doesn't. When Data.setBasic() gets called, you have more work to do.

Related

Creating a blank instance

Suppose I've got a Class c and I want to create an instance of it. I can only use
c.newInstance()if c has a nullary (zero-argument) constructor. Is there a way I can create a "blank" instance without invoking any constructor? (I'm automating a cloning process, so I'm going to assign values to the fields after I do this.) 
(I'm automating a cloning
process, so I'm going to assign values to the fields
after I do this.)why not just serialize and deserialize it?
There are faster ways of coding a clone method, but the suggestion above is the least code.
I don't want to use the serialize/deserialize trick because there are some fields I don't want copied.
More context: I'm writing a class Revertible which maintains a stack of its old states. Whenever a change is to be made, a saveState() method is called to push a quasiclone of the current object onto the stack. Undo pops the stack and copies the fields back into the current object. All of this behavior should be inheritable.
If an object contains a reference to another Revertible object, that object should NOT be copied.
Also, speed is an issue.
Furthermore, if an object contains an unserializable field, it cannot be serialized (as far as I can tell). 
When I did something similar to what you're doing, I used a a dynamic proxy to intercept the method calls and keep track of the arguments (technically I used a java.lang.reflect.InvocationHandler to do the interception/storage). So when creating an object, I would wrap it in a dynamic proxy (this meant that all my undoable objects needed to implement an interface).
You might want to look some of the various implementation of the GoF memento pattern. 
I don't want to use the serialize/deserialize trick
because there are some fields I don't want copied.make them transient - transient fields aren't serialized.

Reflection

I have passed the class name across in the request object:
String className = request.getParameter("class");Class c = Class.forName(className);But my problem is that now I when I need to Instantiate an object of this class with :
Object o = c.newInstance(); I have no way of typecasting the object to its appropriate class, can anybody tell me a way to do this or a way around it? The class is not known until runtime, but I need access to the classes methods and not those of Object.
I hope this is understandable as I have never touched on reflection before. I have read the API.. 
I have no way of typecasting the object to its
appropriate class, can anybody tell me a way to do
this or a way around it? Have these obbjects implement a mutual interface like "Performer", with a single run() method.
Then continue like this:
Performer p = (Performer) Class.forName(className).newInstance();
p.run();
And you're done, and all's nicely encapsulated.
The class is not known until
runtime, but I need access to the classes methods and
not those of Object.A second way would be to set the fields or invoke the methods by name. Or use of 100 instanceof checks. 
Of course you don't know the class at runtime. If you would know, you wouldn't use Class.forName(). So, the best approach is that all of this generic classes implement an Interface that your calling class knows. Then it can cast to this interface and use its methods. Just like the programmer said. 
The problem is that other people are writing the web services, and this will just test them, so I cant enforce implementing an interface.
The idea is that to test the services there wouold be just one test, selected from a drop down, from this the input screen is derived by looking at the properties of the selected class.
Once the fields have been filled in then a submit is actioned and the object is created.. the web service is consumed, and then the result is passed back and displayed to the screen.
Is it possible to invoke methods based on theur name? I know all the services conform to bean standards.
Thanks 
The problem is that other people are writing the web
services, and this will just test them, so I cant
enforce implementing an interface. Then you have a problem. If you neither know how the classes are named nor what their methods are or how they're used, how is it supposed to work? You can find the methods, but can you dynamically generate valid arguments?
Is it possible to invoke methods based on theur name?Yes. As I already pointed out. And what are you going to put into them as arguments? 
Passed into them will be values used to test the web services, this is determined based on the parameters of a service..
for example if a service is called update details: and it has fields name and age then I can safely assume that I need to draw two fields for entry, one for name and one for age.
I need to stress that this is never going to be used by a client, this is to test the web services react in the correct way. The purpose of the services is to perform functions for a front end written by another company.
I hope this makes sense? I may be trying to make this easy when in fact I need a test for each service, I am just trying to find out if this is possible.
Thanks 
Oh by the way, I know all the class names, they are stoed in a lookup table in a DB. The thing is I will not know which one has been selected by the person testing the service 
If it helps you: Class has a getMethod(name, argTypes) method. Or getMethods(). You could use the former with maybe an XML template defining a webservice's specific interface - the arguments array could be filled with Class.forName() objects of the argument types specified in that XML. 
Oh by the way, I know all the class names, they are
stoed in a lookup table in a DB. The thing is I will
not know which one has been selected by the person
testing the serviceThen you know the classes and therefore the methods. So, the approach using
if (className.equalsIgnoreCase(..)) {
or
if (object.getClass() == Class1.class) {                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       

accessing parent object

I have an Experiment class and within it is a Vector of Containers. The Containers contain a a variable parent which is of type Experiment. When I go to serialize Experiment, it would create a loop and crash. To fix this I set up a custom serialization. The problem was, I need to serialize the parent variable right? anyone know a way around this?? Ive been trying to figure it out for 2 days. btw, Experiment extends JTabbedPane which is made made up of Containers... 
make the Experiment class (or whatever class you have that isn't) Serializable? 
no they all are but it creates an infinite loop because they each have references to each other. Im asking if anyone has any logical solution to this? I need a way of accessing the parent without having it be a field in the class... 
Serialization takes care of circular references by keeping track of what it has already serialized and just writing out some kind of pointers the second time it encounters an object. You could do the same thing.

HELP: How do I separate GUI and data

(posted this on Swing section to, but got no response so far!)
Hi there, I'm developing a SWING client, and need to know some way of separating the data from the design...
The architecture:
I have a Controller object that holds the JPanel object with all the text fields. When the user has entered info into the textfields and presses the "Save"-button, this will trigger an action event on the JButton, which will invoke a method in the Controller object (some sort of call back, I guess). This Controller validates the data and calls an EJB method to save it in the DB.
So far so good. But I feel I'm only half way in separating design from the rest, with the Controller object. Sure, it handles the logics, and that's fine, but from it I use calls such as myJPanelForm.JTxtfieldName.getText(), to get the data from the JPanel it holds. And that feels a bit awkward, calling getText on (public) text fields and comboboxes, from the controller object. I should be able to not have any Swing imports here, just perform the logics on request from the event triggered by the JPanel buttons...
Any idea on how I could achieve this?? (I know I could use private textfield and getter-methods, but that too feel wrong...)
I'd very much appreciate any help on this!!
Best regards,
//AC
I call them primitives, Sun calls them "Value Object pattern".
See here...
http://www.iplanet.ne.jp/developers/ias-samples/jps1.1.1/docs/patterns/ValueObject.html
It represents a class that is nothing but a collection (general sense not design sense) of attributes. It has no significant functionality.
I call it a Primitive because I do not consider is a true Object.
It should not interact with other objects. This is somewhat relative sense obviously if an attribute is a java.lang.String then it is interacting. But for instance a Customer class does not have a vector of Orders. For implementation convenience I might allow vector of other objects in the class. However the class itself is not aware that it is acting as a container. The users of the class must be aware of what it contains. (And like I said this is based on implementation convenience rather than design.)

By Value

I understand that everything besides primative variables get passed by reference.
Is there any way to pass a class by value.
I'll be changing things on the UI and I don't want to change the origional. 
create a copy constructor or use the clone interface for the class you want to pass. Then pass either a new object (created with the copy constructor) or a cloned object.
I use the first approach when passing a class to a dialog box. THe dialog box can then modify its local copy. When the box is dismissed, if the user pressed OK and not cancel, I then assign the reference to the class to the copy (the one that got modified).
Hope this helps. 
I understand that everything besides primative
variables get passed by reference.
Is there any way to pass a class by value.
I'll be changing things on the UI and I don't want to
change the origional. You are right incase of a local method. However in case of remote methods both the primitive data types and object types are passed by value while exported remote objects are passed by reference. 
Here's what I'm trying to do. I'm trying to pass a copy of a Panel to a method. The method does it's thing and changes things. (makes everything smaller). But a panel is unclonable. ( I get CloneNotSupportedException). I don't want the orgional to change. The Panel has a JTextArea on it that the user would change, so I don't think copying the constructor would work.
I'm not sure why I have to make the JPanel smaller, it's just the specs of the project. 
I think that your understanding is correct but I think it is important to be accurate when we post information about how variables are passed in java. It is a huge source of confusion for new java developers.
All varaiables are passed by value in Java. All of them. Every single time you pass a variable to a method, it is passed by value.
All Object variables in Java (everything but primitives) are reference variables.
the result of this is every time an Object variable is passed to a method it is a reference variable that is passed by value. This is similar to passing by reference but it isn not the same thing.
Your problem is very interesting. Even if the Panel was Cloneable it might still cause problems i.e. the reference variables for the Components on the Panel would refer to the same objects in both Panels. You may need to create a deep clone method for your Panel.
It seems weird that you need a copy of the Panel to resize. I can see being able to resize a panel but I'm not really sure what saving the original does for you. 
I'm not quite sure what is to be done with this JPanel and I'd rather not give the other programmer free reign on my class.
My Specs are not clear what is to be done with my class. All I know that the panel is going to be resized some where along the way. I'm a C++ programmer and if someone wants a reference to something, I like to create a copy, create a pointer to that copy and return it. 
I'm not quite sure what is to be done with this JPanel
and I'd rather not give the other programmer free
reign on my class.
My Specs are not clear what is to be done with my
class. All I know that the panel is going to be
resized some where along the way. I'm a C++
programmer and if someone wants a reference to
something, I like to create a copy, create a pointer
to that copy and return it.Yes I can see how this would be useful, but I don't think that in any language it is always appropriate. Why don't you just have a method called something like resize() on your JPanel? You can do the resizing without returning a reference at all. Another common solution to this problem is to create a constructor on your JPanel class that takes a JPanle as an argument makes a new JPanel that is a copy of the argument. It seems like a lot of work though.

Categories

Resources