I am using ControlFx PropertySheet in my project . I manage To get it running.
BeansObj:
public class BeansObj implements Serializable {
private String name;
private String mail;
private boolean smart;
private int age;
//Getters,Setters, Beans stuff....
}
ControllerClass:
public class Controller implements Initializable {
#FXML
private PropertySheet sheet;
#Override
public void initialize(URL location, ResourceBundle resources){
sheet.getItems().addAll(BeanPropertyUtils
.getProperties(new BeansObj("foo","foo#bar.foo",true,41)));
}
}
For the boolen field, the generated control is a checkbox with an empty text;
1-How to set this text value implicitly?
For the mail field(String)
2-how to add custom validation method?
For each field
3-how to add a css class|id to specific Control?
By default(I guess) all the controls belong to the Basic Category( PropertySheet.Mode)
4-how to set the category implicitly or explicitly ?
and, in case BeansObj has an Collection attribute
5-how to make it generate a tableView?
Thank you in advance.
Related
I want to force user to send numeric value for a field in request, as user may enter char as well.
Since I haven't found any built in solution in spring mvc validation, I chose to create my own custom validator to check the entered value is number or not.
Please find below code snippet.
Constraint interface :
#Documented
#Constraint(validatedBy = {IntegerValidator.class})
#Target({ METHOD, FIELD, ANNOTATION_TYPE })
#Retention(RUNTIME)
public #interface IntegerConstraint {
String message() default "Please enter numers only...!!!";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
Validator class :
public class IntegerValidator implements ConstraintValidator<IntegerConstraint, String> {
#Override
public void initialize(IntegerConstraint contactNumber) {
}
#Override
public boolean isValid(String reqParam, ConstraintValidatorContext cxt) {
return !StringUtils.isEmpty(reqParam) && reqParam.matches("^(0|[1-9][0-9]*)$");
}
}
DTO class field :
#IntegerConstraint
#PositiveOrZero(message = "Sorting number either can be positive or zero...!!!")
private Integer sortOrd;
Controller :
public ModelAndView addDetail(#Valid #ModelAttribute("fooDetails") FooDTO footDTO,
BindingResult result, HttpServletRequest request)
Error log :
javax.validation.UnexpectedTypeException: HV000030: No validator could be found for constraint 'com.eps.customvalidator.IntegerConstraint' validating type 'java.lang.Integer'. Check configuration for 'sortOrd'
at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.getExceptionForNullValidator(ConstraintTree.java:108) ~[hibernate-validator-6.0.10.Final.jar:6.0.10.Final]
at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.getInitializedConstraintValidator(ConstraintTree.java:140) ~[hibernate-validator-6.0.10.Final.jar:6.0.10.Final]
at org.hibernate.validator.internal.engine.constraintvalidation.SimpleConstraintTree.validateConstraints(SimpleConstraintTree.java:55) ~[hibernate-validator-6.0.10.Final.jar:6.0.10.Final]
at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.validateConstraints(ConstraintTree.java:73) ~[hibernate-validator-6.0.10.Final.jar:6.0.10.Final]
This might happened because your IntegerValidator is implementing ConstraintValidator<IntegerConstraint, String>, thus it is expected to validate String fields. But you are applying it on an Integer field.
You should either consider to change ConstraintValidator<IntegerConstraint, String> to ConstraintValidator<IntegerConstraint, Integer> or to change your DTO to private String sortOrd;
I have two controllers, one controller has a tableview that lists all members and the other controller allows a member to be added. I am having trouble getting the table to update when a member is added. I use two models that are already given to me, so I don't change anything in the models.
Each member has an id and a profession, I use cellValueFactory for listing the members. I tried adding listeners to the cellValueFactoryProperties like this:
idColumn.cellValueFactoryProperty().addListener((obs, oldV, newV) -> memberTv.refresh());
But it still doesn't show new members
The Controller with the TableView:
#FXML
TableColumn<Member, Integer> idColumn;
#FXML
TableColumn<Member, String> profColumn;
#FXML
TableView<Member> memberTv;
#FXML
private void initialize() {
memberTv.setItems(getClub().getMembers());
idColumn.setCellValueFactory(cellData -> new
ReadOnlyObjectWrapper(cellData.getValue().getId()));
profColumn.setCellValueFactory(cellData -> new
ReadOnlyStringWrapper(cellData.getValue().getProf()));
}
public final Club getClub() {
\\\returns Club model
}
The Controller that adds members
private int getId() {
return Integer.parseInt(idTf.getText());
}
private void setId(String id) {
idTf.setText(id);
}
private String getProf() {
return profTf.getText();
}
private void setProf(String prof) {
profTf.setText("" + prof);
}
#FXML
public void handleAddMember(){
getClub().addMember(getId(), getProf());
}
When I add a member I want the TableView to show the added member, but it only shows members already added in the model.
I figured it out, I had a ClubController that sets the stage based after a menu button is clicked. The problem was that whenever I set a stage I used a new instance of teh Club model instead of using getKiosk(). I changed all the new Club()```` to getClub()``` and it everything works perfectly now.
For configuration I use simple xml. I also use this model for TableView. My problem is using of boolean. TableView needs BooleanProperty but simple xml cannot access to this object, obviously. How can I combine this without write big code?
Model
#Root(name="scriptdata")
#Order(elements={"title", "active"})
public class ScriptData {
#Element (required=true)
private String title;
#Element (required=false)
private BooleanProperty active;
/**
*
* #param title
* #param active
*/
public ScriptData() {
this.active = new SimpleBooleanProperty(active);
}
public boolean isActive() {
return active.getValue();
}
public void setActive(boolean active) {
this.active.set(active);
}
CellFactory
modulActiveColumn.setCellValueFactory(new PropertyValueFactory<>("active"));
modulActiveColumn.setCellFactory(CheckBoxTableCell.forTableColumn(modulActiveColumn));
modulActiveColumn.setOnEditCommit((EventHandler<CellEditEvent>) t -> {
((ScriptData) t.getTableView().getItems().get(
t.getTablePosition().getRow())
).setActive((boolean) t.getNewValue());
}
My problem is using of boolean. TableView needs BooleanProperty
You're wrong. In fact the TableView never gains access to the BooleanProperty object stored in the active field of it's items.
PropertyValueFactory uses reflection to
Access a property object by invoking a method with the constructor parameter concatenated with "Property". (This method would be called activeProperty() in your case).
If the above doesn't work it wraps the value returned by a the getter for the property in a ObservableValue. (The name of the getter in this case is getActive() or isActive).
In your case the cellValueFactory does something similar to the following factory
modulActiveColumn.setCellValueFactory(cellData -> new SimpleBooleanProperty(cellData.getValue().isActive()));
Using a boolean field to store the data achieves exactly the same result in your case. The drawback of this approach is that programatic updates of the property do not trigger an update of the TableView and the edits need to be handled manually.
#Root(name="scriptdata")
#Order(elements={"title", "active"})
public class ScriptData {
#Element (required=true)
private String title;
#Element (required=false)
private boolean active;
/**
*
* #param title
* #param active
*/
public ScriptData() {
}
public boolean isActive() {
return active;
}
public void setActive(boolean active) {
this.active = active;
}
}
I am working on spring boot application ,I have one property file ,I am reading property file like below
#Configuration
#ConfigurationProperties(locations = "classpath:mail.properties", prefix = "mail")
public class MailConfiguration {
public static class Smtp {
private boolean auth;
private boolean starttlsEnable;
// ... getters and setters
}
#NotBlank
private String host;
private int port;
private String from;
private String username;
..............
}
Mail .properites
mail.host=localhost
mail.port=25
mail.smtp.auth=false
mail.smtp.starttls-enable=false
mail.from=me#localhost
This working fine ,But Instead of reading one by one property , I want to get all property keys from properties file ,How can I get this .
Use Map for that. Something like: (its "pseudo-code" - may contain spelling mistakes or something, just to show You the idea)
#Configuration
#ConfigurationProperties(locations = "classpath:mail.properties", prefix = "mail")
public static class MailConfiguration {
private Map<String, Object> mail = new HashMap<String, Object>();
public Map<String, Object> getMail() {
return this.mail;
}
}
Should do the work.
Regards,
I need to set some private fields in an object using another object's fields. Those two objects may not be instances of same class.
What I see from a short reading, I can use Apache's BeanUtils and Spring's ReflectionUtils for that. I couldn't find a satisfying explanation for them regarding security, performance, support etc.
The solution will be used in production environment too, so I need a concrete solution.
Which approach do you suggest for such a task.
I think you need use just the BeanUtils library. See my sample, i do a copy properties from CustomerBean to SellerBean.
package testes.beanutils;
import org.apache.commons.beanutils.BeanUtils;
public class Main {
public static void main(String[] args) throws Exception {
Customer customer = new Customer();
customer.setId((long)1);
customer.setName("Bruno");
customer.setLastname("Tafarelo");
Seller seller = new Seller();
BeanUtils.copyProperties(seller, customer);
System.out.println(customer);
System.out.println(seller);
}
}
class Customer {
private Long id;
private String name;
private String lastname;
//getters and setters
//toString
}
class Seller {
private Long id;
private String name;
private int sales;
//getters and setters
//toString
}