使用 beans 绑定更新 JLabel 时出现问题

发布于 2024-10-25 10:02:25 字数 2647 浏览 6 评论 0原文

我正在尝试使用 netbeans IDE 来使用 beans 绑定。我想更新标签中的文本。 这是我创建的 bean。

public class SystemTimeBean implements Serializable {

public static final String PROP_SAMPLE_PROPERTY = "systemTime";

private String systemTime;

private PropertyChangeSupport propertySupport;

public SystemTimeBean() {
    propertySupport = new PropertyChangeSupport(this);
}

public String getSystemTime() {
    return systemTime;
}

public void setSystemTime(String value) {
    String oldValue = systemTime;
    systemTime = value;
    propertySupport.firePropertyChange(PROP_SAMPLE_PROPERTY, oldValue, systemTime);
}


public void addPropertyChangeListener(PropertyChangeListener listener) {
    propertySupport.addPropertyChangeListener(listener);
}

public void removePropertyChangeListener(PropertyChangeListener listener) {
    propertySupport.removePropertyChangeListener(listener);
}

}

public class SystemTimeModel {

private long systemTime;
private SystemTimeBean bean;

public SystemTimeModel() {
    bean = new SystemTimeBean();
}

public long getSystemTime() {
    return systemTime;
}

public void setSystemTime(long systemTime) {
    this.systemTime = systemTime;
    bean.setSystemTime(Long.toString(systemTime));
}

}

中的绑定代码

bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

    systemTimeBean1 = new beansbindingapp.SystemTimeBean();
    lblBinding = new javax.swing.JLabel();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, systemTimeBean1, org.jdesktop.beansbinding.ELProperty.create("${systemTime}"), lblBinding, org.jdesktop.beansbinding.BeanProperty.create("text"));
    bindingGroup.addBinding(binding);

    bindingGroup.bind();

我的 JFrame和 Main.class

public class Main {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {

        public void run() {
            JFrame frame = new SystemTimeFrame();
            frame.setVisible(true);
        }
    });

    SystemTimeModel time = new SystemTimeModel();

    for(int i = 0; i < 10; i++) {
        time.setSystemTime(System.currentTimeMillis());
        try {
            Thread.sleep(1000);
        } catch (InterruptedException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

}

标签未更新。 感谢您的帮助。

i m trying to use beans binding using netbeans IDE. I would like to update the text in a label.
Here is the bean that i created.

public class SystemTimeBean implements Serializable {

public static final String PROP_SAMPLE_PROPERTY = "systemTime";

private String systemTime;

private PropertyChangeSupport propertySupport;

public SystemTimeBean() {
    propertySupport = new PropertyChangeSupport(this);
}

public String getSystemTime() {
    return systemTime;
}

public void setSystemTime(String value) {
    String oldValue = systemTime;
    systemTime = value;
    propertySupport.firePropertyChange(PROP_SAMPLE_PROPERTY, oldValue, systemTime);
}


public void addPropertyChangeListener(PropertyChangeListener listener) {
    propertySupport.addPropertyChangeListener(listener);
}

public void removePropertyChangeListener(PropertyChangeListener listener) {
    propertySupport.removePropertyChangeListener(listener);
}

}

public class SystemTimeModel {

private long systemTime;
private SystemTimeBean bean;

public SystemTimeModel() {
    bean = new SystemTimeBean();
}

public long getSystemTime() {
    return systemTime;
}

public void setSystemTime(long systemTime) {
    this.systemTime = systemTime;
    bean.setSystemTime(Long.toString(systemTime));
}

}

The code for binding in my JFrame

bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

    systemTimeBean1 = new beansbindingapp.SystemTimeBean();
    lblBinding = new javax.swing.JLabel();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, systemTimeBean1, org.jdesktop.beansbinding.ELProperty.create("${systemTime}"), lblBinding, org.jdesktop.beansbinding.BeanProperty.create("text"));
    bindingGroup.addBinding(binding);

    bindingGroup.bind();

and the Main.class

public class Main {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {

        public void run() {
            JFrame frame = new SystemTimeFrame();
            frame.setVisible(true);
        }
    });

    SystemTimeModel time = new SystemTimeModel();

    for(int i = 0; i < 10; i++) {
        time.setSystemTime(System.currentTimeMillis());
        try {
            Thread.sleep(1000);
        } catch (InterruptedException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

}

The label is not updated.
Thanks for help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

剧终人散尽 2024-11-01 10:02:25

我最近一直在使用 bean 绑定,刚刚解决了代码中的读/写问题。我稍微修改了您的 Main 代码,通过更改 SystemTimeBean 来更新时间。我相信您的代码的问题是您没有更新绑定对象。

public class Main {

private JFrame _frame;
private SystemTimeModel _systemTimeModel = new SystemTimeModel();

public static void main( final String[] args ) {
    final Main systemTimeFrame = new Main();

    systemTimeFrame._frame.setVisible( true );

    systemTimeFrame.doStuff();
}

public Main() {
    initialize();
}

public void doStuff() {
    for( int i = 0; i < 10; i++ ) {
        _systemTimeModel.setSystemTime( System.currentTimeMillis() );
        try {
            Thread.sleep( 1000 );
        }
        catch ( InterruptedException ex ) {
            Logger.getLogger( Main.class.getName() ).log( Level.SEVERE, null, ex );
        }
    }
}

private void initialize() {
    _frame = new JFrame();
    _frame.setBounds( 100, 100, 450, 300 );
    _frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

    JLabel lblBinding = new JLabel( "" );
    _frame.add( lblBinding );

    BindingGroup bindingGroup = new BindingGroup();
    BeanProperty< SystemTimeBean, String > sourceProperty = BeanProperty.create( "systemTime" );
    BeanProperty< JLabel, String > targetProperty = BeanProperty.create( "text" );
    AutoBinding< SystemTimeBean, String, JLabel, String > binding =
        Bindings.createAutoBinding( UpdateStrategy.READ_WRITE, _systemTimeModel.getBean(), sourceProperty, lblBinding, targetProperty );
    binding.bind();
    bindingGroup.addBinding( binding );
}

}

I have been using bean bindings recently and just solved the read/write issue in my code. I modified your Main code a bit to make the time update by changing the SystemTimeBean. I believe that the issue with your code is that you are not updating the bound object.

public class Main {

private JFrame _frame;
private SystemTimeModel _systemTimeModel = new SystemTimeModel();

public static void main( final String[] args ) {
    final Main systemTimeFrame = new Main();

    systemTimeFrame._frame.setVisible( true );

    systemTimeFrame.doStuff();
}

public Main() {
    initialize();
}

public void doStuff() {
    for( int i = 0; i < 10; i++ ) {
        _systemTimeModel.setSystemTime( System.currentTimeMillis() );
        try {
            Thread.sleep( 1000 );
        }
        catch ( InterruptedException ex ) {
            Logger.getLogger( Main.class.getName() ).log( Level.SEVERE, null, ex );
        }
    }
}

private void initialize() {
    _frame = new JFrame();
    _frame.setBounds( 100, 100, 450, 300 );
    _frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

    JLabel lblBinding = new JLabel( "" );
    _frame.add( lblBinding );

    BindingGroup bindingGroup = new BindingGroup();
    BeanProperty< SystemTimeBean, String > sourceProperty = BeanProperty.create( "systemTime" );
    BeanProperty< JLabel, String > targetProperty = BeanProperty.create( "text" );
    AutoBinding< SystemTimeBean, String, JLabel, String > binding =
        Bindings.createAutoBinding( UpdateStrategy.READ_WRITE, _systemTimeModel.getBean(), sourceProperty, lblBinding, targetProperty );
    binding.bind();
    bindingGroup.addBinding( binding );
}

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文