Netbeans beans将单选按钮绑定到字符串



我想把一个RadioButton绑定到一个String。如果字符串是"x"如果字符串为"y",则应选中单选按钮。单选按钮不应该被选中。至少我知道我需要一个豆子绑定转换器。这是我尝试过的,但它不起作用。

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

转换不正确。你应该从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 "";
    }
}
}

相关内容

  • 没有找到相关文章

最新更新