Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
自省(JavaFx)_Java_Javafx_Introspection - Fatal编程技术网

自省(JavaFx)

自省(JavaFx),java,javafx,introspection,Java,Javafx,Introspection,在做了很多搜索之后,我把它留给你。 在我的应用程序JavaFx中,我使用内省自动生成一个gridPane(然后插入到对话框中)。因此,我有了TableView,当用户双击上面的按钮时,它会生成包含该TableView列的对话框。 因此,在此对话框中有允许修改TableView中字段值的TextFields。 但是,我不能通过内省来获取属性的值,如何才能获取由于内省而创建的文本字段的值呢? 这是我的自省方法: public static GridPane analyserChamp(Et

在做了很多搜索之后,我把它留给你。 在我的应用程序JavaFx中,我使用内省自动生成一个gridPane(然后插入到对话框中)。因此,我有了TableView,当用户双击上面的按钮时,它会生成包含该TableView列的对话框。 因此,在此对话框中有允许修改TableView中字段值的TextFields。 但是,我不能通过内省来获取属性的值,如何才能获取由于内省而创建的文本字段的值呢? 这是我的自省方法:

    public static  GridPane analyserChamp(Etudiant etu) {
    List<String> list = new ArrayList<>();
    Class<? extends Etudiant> classPixel = etu.getClass();
    Field attribut[] = classPixel.getDeclaredFields();
    GridPane gp = new GridPane();

    int i=0;
    for(Field p : attribut) {
        list.add(p.getName());
        Label lab = new Label();

        if(!p.getName().equals("classe")) {
            TextField l = new TextField();
            lab.setText(p.getName());
            gp.add(l, 1, i);

        }else {
            ComboBox<String> cb = new ComboBox<String>();
            cb.getItems().addAll("1Bi","2Bi","3Bi");
            gp.add(cb, 1, i);
        }

        gp.add(lab, 0, i);
        i++;

    }
    return gp;
}
publicstaticgridpane分析器champ(Etudiant-etu){
列表=新的ArrayList();

类解决此问题的方法有很多,例如,您可以使用
userData
属性存储属性的键,以便稍后可以迭代
GridPane
子项,并在
对话框中获取每个值

当您反思班级时,
Etudiant

if(!p.getName().equals("classe")) {
            TextField l = new TextField();
            l.setUserData(p.getName()); //Store the attribute name in the TextField
            lab.setText(p.getName());
            gp.add(l, 1, i);

        }else {
            ComboBox<String> cb = new ComboBox<String>();
            cb.setUserData(p.getName()); //Store the attribute name in the ComboBox
            cb.getItems().addAll("1Bi","2Bi","3Bi");
            gp.add(cb, 1, i);
        }

存储一个
Supplier
以获取
Map中某个字段的输入值感谢您的回答。但是l.setUserData(p.getName())不起作用,事实上TextField仍然是空的。当我获取Textfields的值时,也有空的。(String属性=((TextField)child)。getText();当我用您的代码获取TextField的值时,他会在我的AnalyzerChamp()方法所在的另一个类中获取TextField的值,而不是用户在对话框中输入的新值。然后您应该迭代实际内容:
dialog.getDialogPane().getContent().getChildren()
,以获取新值。
if(!p.getName().equals("classe")) {
            TextField l = new TextField();
            l.setUserData(p.getName()); //Store the attribute name in the TextField
            lab.setText(p.getName());
            gp.add(l, 1, i);

        }else {
            ComboBox<String> cb = new ComboBox<String>();
            cb.setUserData(p.getName()); //Store the attribute name in the ComboBox
            cb.getItems().addAll("1Bi","2Bi","3Bi");
            gp.add(cb, 1, i);
        }
    Dialog<Etudiant> dialog = new Dialog<>();
    ...
    GridPane content = Analysateur.analyserChamp(test); //Keep the content accesible
    ...
    dialog.getDialogPane().setContent(content);
    ...
    dialog.setResultConverter(button -> { //Convert the result
        Etudiant result = new Etudiant();
        for (Node child : content.getChildren()) { //Iterate over the GridPane children
            if (child instanceof TextField) {
                String attribute = ((TextField)child).getUserData();
                String value = ((TextField)child).getTest();
                //Set the value in the result attribute via instrospection
            }
            if (child instanceof ComboBox) {
                //Do the same with combos
            }
        }
    });
public class ReflectionDialog<T> extends Dialog<T> {

    public ReflectionDialog(Class<T> type, Supplier<T> factory) throws IllegalAccessException {
        GridPane gp = new GridPane();
        Field[] fields = type.getDeclaredFields();

        // stores getters for result value
        final Map<Field, Supplier<?>> results = new HashMap<>();

        int i = 0;
        for (Field field : fields) {
            if (String.class.equals(field.getType())) {
                String name = field.getName();
                Node input;
                Supplier<?> getter;
                if ("classe".equals(name)) {
                    ComboBox<String> cb = new ComboBox<>();
                    cb.getItems().addAll("1Bi", "2Bi", "3Bi");
                    getter = cb::getValue;
                    input = cb;
                } else {
                    TextField l = new TextField();
                    getter = l::getText;
                    input = l;
                }
                results.put(field, getter);
                gp.addRow(i, new Label(name), input);
                i++;
            }
        }

        getDialogPane().setContent(gp);
        getDialogPane().getButtonTypes().addAll(ButtonType.OK, ButtonType.CANCEL);
        setResultConverter(buttonType -> {
            if (buttonType == ButtonType.OK) {
                // create & initialize new object
                final T object = factory.get();
                results.forEach((k, v) -> {
                    try {
                        k.set(object, v.get());
                    } catch (IllegalAccessException ex) {
                        throw new IllegalStateException(ex);
                    }
                });
                return object;
            } else {
                return null;
            }
        });
    }

}
public class A {

    String classe;
    String value;

    @Override
    public String toString() {
        return "A{" + "classe=" + classe + ", value=" + value + '}';
    }

}
ReflectionDialog<A> dialog = new ReflectionDialog<>(A.class, A::new);
A result = dialog.showAndWait().orElse(null);
System.out.println(result);