I have a interesting problem in Java, its a little wordy though so bear with me.
I decided to make a customizable Jpanel to act as a properties window, So instead manually designing each panel i wanted. I could just use the custom panel over and over with different parameters to "watch and edit" in the constructor, and it would do the rest (layout property listeners ect).
The problem i have is p开发者_如何学编程assing the primitive objects to and from the panel. When a value is changed in the panel, it should update the value that it corresponds to in the program. This would be easy with non-primitive objects, I would just pass the Object to the panel, then changing the object in the panel, would change the value of the object in the main program as well. With Primitive types such as Boolean and int, the actual value is passed to the Panel and not a reference and so changing these values in the panel wont affect the rest of the program.
the ideal solution would be to use a pointer. but these don't exist in java. Another solution would be to encapsulate all the integer and Boolean types in a object, but i don't really want to do this as it would mean a lot of rewriting of code in the main program.
There must be a way to do this but i cant think of it, any ideas?
Thanks in advance,
Chris.
I'd personally use the observer design pattern for this - this way you can just fire events off to whoever has registered for that event when something changes.
Note that it's not just primitive types that would have this behaviour - it's also all immutable types (String for instance.)
The only way around this with your current pattern is as you describe, wrapping up all the boolean values in objects and passing those around. It's not the nicest solution though, an observer pattern would make your code more maintainable and generally nicer in my opinion.
I would just pass the Object to the panel, then changing the object in the panel, would change the value of the object in the main program as well. With Primitive types such as Boolean and int, the actual value is passed to the Panel and not a reference and so changing these values in the panel wont affect the rest of the program.
That's wrong. In Java, everything is passed as copy of reference. It is just the case, that the reference of a calendar points to the object, as well as the copy of that reference, so changing the value the reference references, does what you want.
But if you have a User, with User.name (String) and User.age (int), and modify them by an JTextField, for instance, you have the same problem, while String isn't a primitive. Strings are immutable, so you could just change the String, a reference is pointing to, but since, what you pass, is a copy, the original object is not affected.
// Invokes your Panel with a new copy of the string reference
StringPropPanel ppun = new StringPropPanel (user.name);
// Invokes your Panel with a new copy of the int reference
IntPropPanel ppua = new IntPropPanel (user.age);
// Invokes your Panel with a new copy of a User reference
IntPropPanel ppup = new IntPropPanel (user.parent);
The last one would work - but raise the question, how to generate such a Panel from a User.
PropPanel ppun = new PropPanel (user.name) {
public void update () {
user.setName (textfield.getText ());
}
}
PropPanel ppun = new PropPanel (user.age) {
public void update () {
user.setAge (Integer.parseInt (textfield.getText ()));
}
}
With an abstract update
method, which is uniquely triggered, and can be overriden by anonymous ad-hoc implementations, you might be more successful.
精彩评论