Convert POJO to JavaFX Property - java

In my project, I have a number of POJOs whose fields are shown in a JavaFX GUI. I currently update the UI using a manual refresh – whenever the POJO changes, I call a refresh() method. I would like to attempt using binding to accomplish this instead, which requires using JavaFX properties. Whilst I can change the object, its internal fields are other objects, which I cannot change (they are populated using GSON, which AFAIK uses 'normal' Java objects – e.g. String, not StringProperty). Furthermore, the object is read only – it only has getters, not setters.
I believe I can use a ReadOnlyJavaBeanObjectPropertyBuilder (yay Java naming?) or a ReadOnlyObjectWrapper to wrap the object as a property. However, the internal fields – which are what I want to bind the Labels to – are not converted to properties. Is there any way of doing such a recursive conversion – convert an entire object which contains normal object fields into a property which contains further properties? Is this even necessary – am I doing something wrong?
EDIT: I suspect any solution would have to use reflection.

I'm not sure but have a look at BeanPathAdapter
Its part of JFXtras-Labs, downloadable jar.
Source is here on GitHub.

How about adapters for the POJOs?
Example
public class Person{
private String name;
private Address addr;
\\getters, setters...
}
And for the JavaFX GUI
public class FXPerson{
public FXPerson(Person p){
this.name = \\build StringProperty
this.fxaddr = \\build ObjectProperty<FXAddress>
}
private StringProperty name;
private ObjectProperty<FXAddress> fxaddr;
}
Downside:
For every POJO you will have to write an adapter. And if a POJO changes (e.g. new property etc.) you will need to update the corresponding adapter.

Related

When is getters setters needed in android studio project?

I can generate getters and setters in java program and I know that it is used to access private variables. Besides this How will I be able to decide this, at this point I need to create java class with getters and setters.
Getter and Setter methods are used for encapsulate the data.
it means wrapping the data into single unit.
For example create a ListView with 3 TextView who has different-different value.
so now the question is how will you send the data to your custom ListView adapter. In this case you have to use a beam(has getter-setter methods) class.
EDIT:
Here is another example it will show how to send multiple data by single object from one Activity to another Activity
https://stackoverflow.com/a/7827593/6676466
in modern java programming you must importantly follow information
hiding this is making the class fields to become private or
protected(to be used by child class) that is why we have getter and setter
method, also getter and setter is useful when you want to have a range check in each data field
if you want to directly access the variable you can create a child class of that parent class and set each fields to become protected

Google Cloud Endpoint - cast return model

I'm a little confused about the returned object from my GoogleCloudEndpoint.
My Android App uses an Object e.g TestObject with something like this path: com.example.classes.TestObject. My GoogleCloudEndpoint uses this class too and returns it in some methods, but returned objects of those methods are always another generated class like this: `com.example.testApi.model.TestObject.
Now in my App this is very confusing because I have to handle two nearly identical objects with the same name. The only difference between the object types is, that the generated object has private fields with getters & setters, while the original one had public fields. Obviously there is not way to cast those object to the other.
Is there any way to transform this generated object back to the original one? Or should I only use the generated one in my App?
You have to create your own helpers to transform back and forth. The model classes are subclasses of a generic JSON container object.

Hibernate Annotations : No default constructor for entity

I am trying to persist the objects generated by JAXB. Here is the sample structure:
#Column(name = "reporting_identifier")
private String reportingIdentifier;
#Column(name = "apply_quiet_time")
private boolean applyQuietTime;
#Embedded
private RecipientDetailsList recipientDetailsList;
Below is the structure of RecipientDetailsList class:
#ElementCollection(targetClass=String.class)
private List<RecipientDetails> recipientDetails;
Now, the RecipientDetails class has one argument constructor, which accepts a String. That String I want to persist in the database as a part of the whole record. I am seeing
org.hibernate.InstantiationException: No default constructor for entity: RecipientDetailsList
exception when I try to save an object. I have two questions:
Do we have any work around this exception? I can't change the class as it is designed for JAXB marshalling/unmarhsalling. Can I somehow store the objects without altering the structure? Also, I am interested in only storing the first record of the list referenced by
recipientDetails as I want only one row for object. I want it to ignore the rest of the records if it has more than 1 record. Is it possible?
Is this good design to use the annotation directly into classes which are generated by JAXB? Should I create another classes (and possibly mappers/converters) just to store and retrieve the information?
For your first question: this is happening because when Hibernate tries to create a bean, it does it via reflection. It does the object creation by calling the no-arg constructor, and then using the setter methods to set the properties. You can't use a bean that doesn't have a no-arg constructor.
For the second question: if something else has generated classes for you that don't have a no-arg constructor, really your only option (if you can't modify the class) is to create a wrapper round it, or a subclass that has a no-arg constructor. I don't see any other way of doing it if you can't modify the class directly. But the subclassing should be fine as long as the class you've got has enough visibility on the methods (i.e., doesn't have private methods that you then can't get to).

GWT generate class methods

In my project I have a series of Models which basically just contain data, and have getters and setters for that data (which has to match an API). I am trying to use GWT generators to generate the getters and setters (because they have some logic in them for setting default values and I don't want to have this typed out all the time.
For example, MyBusinessModel.java:
public class MyBusinessModel extends AbstractBusinessModel {
private Integer uid;
private String name;
//... and so on
}
I then create a public abstract class AbstractBusinessModel which has some implemented base methods. I had created a generator for this, AbstractBusinessModelGenerator extends Generator, which automatically creates all the getters and setters, but I keep getting errors about MyBusinessModel not being able to be a superinterface of MyBusinessModelImpl (the generated class) because it's not an interface.
Is there a way for me to generate classes like this (I can't make MyBusinessModel an interface because I need it to have private properties), or can I only generate interfaces (which become classes)?
The answer is to use setSuperClass on the ClassSourceFileComposerFactory instead of addImplementedInterface. I didn't realise that this existed. Now I do.

Why to prefer getter and setter methods for variable instead of making it public? [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Why use getters and setters?
Yes, It's a very simple thing but I am not finding a good reson for this. I know it is a good practice to make a variable private and providing getter to setter to access that variable. Still Is there any reason other than this ?
Lookup "encapsulation" and "information hiding".
Basically, a class manages data and provides operations on them/ give access to them, but the internal representation of that data should kept private to the class. Like this, a class can change its internal representation without breaking other code.
E.g. consider:
A)
public Date date;
B)
private Date date;
public Date getDate(){...}
public setDate(Date date){...}
Later you choose to use Calendar instead of Date.
In case A) you cannot change the data type without breaking code.
In case B) you can do:
private Calendar calendar;
public Date getDate(){...}
public setDate(Date date){...}
public Calendar getCalendar (){...}
public setCalendar(Calendar calendar){...}
You can provide a conversion in the get/setDate methods. Client code doesn't break.
Another reason you sometimes want to use getter and setter is the use of libraries/
frameworks which base on JavaBeans patterns.
There are many reasons. It is a way to control access(accessors) to the inner workings of your class. Sometimes for example you may want the getter to return a clone of the object or the setter to just copy values on the private object rather than pointing the handler to the new object passed to the setter. Another reason would be to do something before or after you get or set an object without exposing this functionality.
To sum it up:
You give room for future extensions
You close the door to uncontrolled access
Use them only if needed though.
Only provide getters/setters if it is actually needed, don't just do it automatically
A getter/setter hides the internal implementation and in theory gives you a well defined interface and allows you to change the internal implementation without clients having to change their code
If you leave variable public, you can't make sure the object will keep consistent state. In your setters you can for example have some checking like e.g. avoid setting negative number as age.
If you have public variable you drop for example possibility of having listeners since listeners are typically using notifications on setting the variable via setter. There is no chance to track the change when directly changing public variable.
These are some very basic examples but can be found more for sure.

Categories