Netbeans bean将RadioButton绑定到String

时间:2011-04-07 00:34:54

标签: java netbeans binding radio-button javabeans

我想将RadioButton绑定到String。如果字符串是“x”,则应选择单选按钮,如果字符串为“y”,则不应选择RadioButton。至少我发现我需要一个beansbinding.Converter。这是我尝试过的但是没有用。

public class TypToBooleanConverter extends Converter {

@Override
public Object convertForward(Object value) {
     return value.toString().equals("x");

}

@Override
public Object convertReverse(Object value) {
    if (value.toString().equals("true"))    {
        return "x";
    }
     else {
                return false;
     }
}}

但后来我接受了例外。希望有人能帮助我。

更新

这是完整的堆栈跟踪:

Exception in thread "AWT-EventQueue-0" java.lang.Error: java.lang.reflect.InvocationTargetException
        at org.jdesktop.application.ApplicationAction.actionFailed(ApplicationAction.java:859)
        at org.jdesktop.application.ApplicationAction.noProxyActionPerformed(ApplicationAction.java:665)
        at org.jdesktop.application.ApplicationAction.actionPerformed(ApplicationAction.java:698)
        at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
        at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
        at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
        at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
        at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
        at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:272)
        at java.awt.Component.processMouseEvent(Component.java:6267)
        at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
        at java.awt.Component.processEvent(Component.java:6032)
        at java.awt.Container.processEvent(Container.java:2041)
        at java.awt.Component.dispatchEventImpl(Component.java:4630)
        at java.awt.Container.dispatchEventImpl(Container.java:2099)
        at java.awt.Component.dispatchEvent(Component.java:4460)
        at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4577)
        at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
        at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
        at java.awt.Container.dispatchEventImpl(Container.java:2085)
        at java.awt.Window.dispatchEventImpl(Window.java:2478)
        at java.awt.Component.dispatchEvent(Component.java:4460)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
Caused by: java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.jdesktop.application.ApplicationAction.noProxyActionPerformed(ApplicationAction.java:662)
        ... 27 more
Caused by: org.jdesktop.beansbinding.PropertyResolutionException: Exception invoking method public void org.jdesktop.swingbinding.adapters.AbstractButtonAdapterProvider$Adapter.setSelected(boolean) on org.jdesktop.swingbinding.adapters.AbstractButtonAdapterProvider$Adapter@1958bf9
        at org.jdesktop.beansbinding.BeanProperty.invokeMethod(BeanProperty.java:791)
        at org.jdesktop.beansbinding.BeanProperty.write(BeanProperty.java:891)
        at org.jdesktop.beansbinding.BeanProperty.setValue(BeanProperty.java:571)
        at org.jdesktop.beansbinding.Binding.refreshUnmanaged(Binding.java:1229)
        at org.jdesktop.beansbinding.Binding.refresh(Binding.java:1207)
        at org.jdesktop.beansbinding.AutoBinding.tryRefreshThenSave(AutoBinding.java:162)
        at org.jdesktop.beansbinding.AutoBinding.sourceChangedImpl(AutoBinding.java:227)
        at org.jdesktop.beansbinding.Binding.sourceChanged(Binding.java:1411)
        at org.jdesktop.beansbinding.Binding.access$1200(Binding.java:38)
        at org.jdesktop.beansbinding.Binding$PSL.propertyStateChanged(Binding.java:1618)
        at org.jdesktop.beansbinding.PropertyHelper.firePropertyStateChange(PropertyHelper.java:212)
        at org.jdesktop.beansbinding.ELProperty.notifyListeners(ELProperty.java:688)
        at org.jdesktop.beansbinding.ELProperty.access$800(ELProperty.java:155)
        at org.jdesktop.beansbinding.ELProperty$SourceEntry.processSourceChanged(ELProperty.java:312)
        at org.jdesktop.beansbinding.ELProperty$SourceEntry.sourceChanged(ELProperty.java:326)
        at org.jdesktop.beansbinding.ELProperty$SourceEntry.propertyChange(ELProperty.java:333)
        at java.beans.PropertyChangeSupport.firePropertyChange(PropertyChangeSupport.java:339)
        at java.beans.PropertyChangeSupport.firePropertyChange(PropertyChangeSupport.java:276)
        at lagerverwaltung.articleEditor.setCurrentRecord(articleEditor.java:42)
        at lagerverwaltung.LagerverwaltungView.newRecord(LagerverwaltungView.java:141)
        ... 32 more
Caused by: java.lang.IllegalArgumentException
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.jdesktop.beansbinding.BeanProperty.invokeMethod(BeanProperty.java:782)
        ... 51 more

1 个答案:

答案 0 :(得分:0)

转换不正确。您应该从String转换为类似Boolean的类型,然后从Boolean转换为String。您可能需要更改convertReverse()方法以返回空String以外的其他内容。

public class BooleanConverter extends Converter {

@Override
public Boolean convertForward(String value) {
     if("x".equals(value) {
        return Boolean.TRUE;
     } else {
       return Boolean.FALSE;
     }
}

@Override
public String convertReverse(Boolean value) {
    if (value.booleanValue()) {
        return "x";
    } else {
        return "";
    }
}
}