Netbeans bean 将 RadioButton 绑定到 String

Posted

技术标签:

【中文标题】Netbeans bean 将 RadioButton 绑定到 String【英文标题】:Netbeans beans binding RadioButton to String 【发布时间】:2011-07-31 06:43:48 【问题描述】:

我想将 RadioButton 绑定到字符串。如果字符串为“x”,则应选择单选按钮,如果字符串为“y”,则不应选择单选按钮。至少我发现我需要一个 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

【问题讨论】:

异常信息是什么? 一个疯狂的猜测:尝试'return "y";'你在哪里'return false;' 【参考方案1】:

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


【讨论】:

以上是关于Netbeans bean 将 RadioButton 绑定到 String的主要内容,如果未能解决你的问题,请参考以下文章

使用 netbeans GUI builder 进行 JTable 绑定

无法在 NetBeans 中将 jar 添加到调色板

xampp和net beans可以一起为tomcat工作吗

在 NetBeans 中编译 Maven 项目时 Ant 无法找到环境变量

在 Netbeans 6.9.1 中编辑方法注释模板

如何制作创建超过 1 个文件的 Netbeans 模板?