java反射的应用

public class myCheckBoxTableCell<S, T> extends CheckBoxTableCell<S, T> {

    @Override
    public void updateItem(T item, boolean empty) {


        super.updateItem(item, empty);
        if (empty) {
            setText(null);
            setGraphic(null);
        } else {
            Field field;
            try {
                field = this.getClass().getSuperclass().getDeclaredField("checkBox");
                field.setAccessible(true);
                checkbox = (CheckBox) field.get(this);

            } catch (Exception e) {
                e.printStackTrace();
            }
            setText(text);
        }
    }
}
   

在这里想取到checkboxtablecell中的checkbox

通过this.getClass().getSuperclass().getDeclaredField("checkBox"获取到父类的checkbox

并且通过field.setAccessible(true);让我们能访问到私有变量

这样就可以拿到这个checkbox然后自己去写一些想要的效果了

猜你喜欢

转载自blog.csdn.net/phcgld1314/article/details/78843306