I am supposed to write a window application using the JavaFX library that uses the reflection mechanism will automatically detect, edit and change object properties of any class written in the convention JavaBean. Properties are private fields of an object, to which access is controlled by so-called access methods. getters and setters. Unfortunately Java is not my strongest element so I am asking for some help with this task. The application itself should look something like this:
Currently, when I try to save a change in the fields but it keeps popping up that I haven't saved from "catch (Throwable e)".
Code:
1) Controller:
public class Controller {
@FXML
private TextField tekstField;
@FXML
private TextArea area_text;
@FXML
private VBox vbox;
private Class<?> MyClass;
private Object ObjectInstance;
LinkedList<sample.Field> fields = new LinkedList<>();
public void CreateObject() {
vbox.getChildren().clear();
this.fields.clear();
try {
MyClass = Class.forName(tekstField.getText());
Field[] allFields = MyClass.getDeclaredFields();
for (Field field : allFields) {
field.setAccessible(true);
String methodSetName = "set" + field.getName().substring(0, 1).toUpperCase() + field.getName().substring(1);
String methodGetName = "get" + field.getName().substring(0, 1).toUpperCase() + field.getName().substring(1);
Class[] setterArgs = new Class[1];
setterArgs[0] = field.getType();
Method setterMethod = MyClass.getMethod(methodSetName, setterArgs);
Method getterMethod = this.MyClass.getMethod( methodGetName , null);
String s = field.getName();
HBox hb = new HBox();
TextField textfield = new TextField();
this.fields.add( new sample.Field(methodGetName, methodSetName, textfield, setterMethod, getterMethod, field.getType().getName()) );
textfield.setPrefWidth(300);
Label label = new Label("<- " + field.getName());
label.setWrapText(true);
hb.getChildren().add(textfield);
hb.getChildren().add(label);
vbox.getChildren().add(hb);
}
area_text.setText("Obiekt o nazwie: " + MyClass.getSimpleName() + " został utworzony!");
} catch (ClassNotFoundException e) {
area_text.setText("Błąd");
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
}
public void SaveChanges() {
String wiadomosc="";
for (sample.Field field : fields) {
Object[] args = new Object[1];
try {
switch( field.getFieldType() ) {
case "int":
args[0] = Integer.parseInt(field.getTextField().getText());
break;
case "java.lang.String":
args[0] = field.getTextField().getText();
break;
}
}
catch(Exception e) {
area_text.setText("Zła wartość");
}
try {
field.getSetterMethod().invoke(ObjectInstance, args );
wiadomosc = wiadomosc + "Wprowadzono: "+ field.getTextField().getText() +"\n";
area_text.setText(wiadomosc);
}catch(Throwable e) {
area_text.setText("Nieudana próba zapisu!");
}
}
}
2) Book Class:
public class Book {
private String title;
private String autor;
private String type;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAutor() {
return autor;
}
public void setAutor(String autor) {
this.autor = autor;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}
3) Field Class:
public class Field {
private String getterName, setterName, fieldType;
private TextField textField;
private Method setterMethod, getterMethod;
public Field(String getterName, String setterName, TextField textField, Method setterMethod, Method getterMethod, String fieldType ) {
this.getterName = getterName;
this.setterName = setterName;
this.textField = textField;
this.setterMethod = setterMethod;
this.getterMethod = getterMethod;
this.fieldType = fieldType;
}
public String GetGetterName() {
return this.getterName;
}
public String GetSetterName() {
return this.setterName;
}
public TextField getTextField() {
return this.textField;
}
public Method getSetterMethod() {
return this.setterMethod;
}
public Method getGetterMethod() {
return this.getterMethod;
}
public String getFieldType() {
return this.fieldType;
}
}
I probably broke something before and I don't know where - I'm sorry about the spaggetti code in general.
Aucun commentaire:
Enregistrer un commentaire