首页 \ 问答 \ Field.set抛出IllegalArgumentException(Field.set throws IllegalArgumentException)

Field.set抛出IllegalArgumentException(Field.set throws IllegalArgumentException)

我正在尝试在java中为swing对象进行单向数据绑定,就像练习一样。 目前,对于扩展JTextComponent的swing对象,我添加了一个文档侦听器,它还可以更新后备数据存储。

public class Binder {

Map<JComponent, Map.Entry<WeakReference, Field>> map = new WeakHashMap<>();
AutoDocumentListener adl;

public Binder() {
    adl = new AutoDocumentListener();
}

public void bind(Object instance, String varName, JComponent element, String property) {
    Field field;
    try {
        field = instance.getClass().getDeclaredField(varName);
        field.setAccessible(true);
        map.put(element,
                new AbstractMap.SimpleEntry<WeakReference, Field>(new WeakReference(instance), field));

        if (element instanceof JTextComponent) {
            element = (JTextComponent) element;
            Document eldoc = ((JTextComponent) element).getDocument();
            eldoc.putProperty("origin", element);

            eldoc.addDocumentListener(adl);
        } else {  }
    } catch (NoSuchFieldException | SecurityException ex) {
        Logger.getLogger(Binder.class.getName()).log(Level.SEVERE, null, ex);
    }
 }

 class AutoDocumentListener implements DocumentListener {

    @Override
    public void insertUpdate(DocumentEvent evt) {
        JTextComponent jc = (JTextComponent) evt.getDocument().getProperty("origin");
        Map.Entry<WeakReference, Field> dataToUpdate = map.get(jc);
        if (dataToUpdate != null) {
            try {
                Object data = jc.getText();
                dataToUpdate.getValue().set(dataToUpdate.getKey(), data);
            } catch (IllegalArgumentException | IllegalAccessException ex) {
                Logger.getLogger(Binder.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    @Override
    public void removeUpdate(DocumentEvent evt) {
    }
    @Override
    public void changedUpdate(DocumentEvent e) {
    }
  }
}

问题是,在下一行我得到一个IllegalArgumentException:

dataToUpdate.getValue().set(dataToUpdate.getKey(), data);

java.lang.IllegalArgumentException: Can not set java.lang.String field org.jbind.test.TestClass.data to java.lang.ref.WeakReference

就我在文档中看到的那样,对Field.set的调用是正确的。 所以,我不明白出了什么问题。

我用以下代码调用该函数:

public class TestClass {
public String data = "Text1";

public void init() {
    Binder binder = new Binder();

    JTextField jtf = new JTextField(data);
    binder.bind(this, "data", jtf, "");
    jtf.setText("Text2");
    System.out.println(data);
}

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            new TestClass().init();
        }
    });

  }
}

I'm trying to do one-way data binding in java for swing objects, just as an exercise. Currently for swing objects that extend JTextComponent, I add a document listener that also updates the backing data storage.

public class Binder {

Map<JComponent, Map.Entry<WeakReference, Field>> map = new WeakHashMap<>();
AutoDocumentListener adl;

public Binder() {
    adl = new AutoDocumentListener();
}

public void bind(Object instance, String varName, JComponent element, String property) {
    Field field;
    try {
        field = instance.getClass().getDeclaredField(varName);
        field.setAccessible(true);
        map.put(element,
                new AbstractMap.SimpleEntry<WeakReference, Field>(new WeakReference(instance), field));

        if (element instanceof JTextComponent) {
            element = (JTextComponent) element;
            Document eldoc = ((JTextComponent) element).getDocument();
            eldoc.putProperty("origin", element);

            eldoc.addDocumentListener(adl);
        } else {  }
    } catch (NoSuchFieldException | SecurityException ex) {
        Logger.getLogger(Binder.class.getName()).log(Level.SEVERE, null, ex);
    }
 }

 class AutoDocumentListener implements DocumentListener {

    @Override
    public void insertUpdate(DocumentEvent evt) {
        JTextComponent jc = (JTextComponent) evt.getDocument().getProperty("origin");
        Map.Entry<WeakReference, Field> dataToUpdate = map.get(jc);
        if (dataToUpdate != null) {
            try {
                Object data = jc.getText();
                dataToUpdate.getValue().set(dataToUpdate.getKey(), data);
            } catch (IllegalArgumentException | IllegalAccessException ex) {
                Logger.getLogger(Binder.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    @Override
    public void removeUpdate(DocumentEvent evt) {
    }
    @Override
    public void changedUpdate(DocumentEvent e) {
    }
  }
}

The problem is, at the following line I get a IllegalArgumentException:

dataToUpdate.getValue().set(dataToUpdate.getKey(), data);

java.lang.IllegalArgumentException: Can not set java.lang.String field org.jbind.test.TestClass.data to java.lang.ref.WeakReference

As far as I can see in the docs, the call to Field.set is correct. So, I don't understand what's going wrong.

I call the function with the following code:

public class TestClass {
public String data = "Text1";

public void init() {
    Binder binder = new Binder();

    JTextField jtf = new JTextField(data);
    binder.bind(this, "data", jtf, "");
    jtf.setText("Text2");
    System.out.println(data);
}

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            new TestClass().init();
        }
    });

  }
}

原文:https://stackoverflow.com/questions/44603031
更新时间:2023-03-25 20:03

最满意答案

问题似乎是图像是自己调整大小,但父div不是,在这种情况下,我会将像素值更改为vh和vw中的值(这些是“视口高度”和“veiwport宽度”的百分比:the要调整大小的窗口的高度和宽度。例如:

margin-top:100px;

会成为

margin-top:8vh;

By placing both the background image and the front image in HTML, and adding the following css rules they both scale the same.

       .divider div{
            top:-50px; // placing the image slightly above the divider div
            height:125%; // making the div containing the image bigger than the divider div
       }

       .divider img{
            width:100%; // just for this image because it isnt wide enough
            position:absolute; // make sure the image listens to top:-50px;
       }

ps: the images scale because of twitter bootstrap's css:

img {
    height: auto;
    max-width: 100%;
}

相关问答

更多

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。