---
title: 'Contact to javax.beans.binding'
url: 'https://tutego.de/blog/javainsel/2007/05/contact-to-javax-beans-binding'
markdown: 'https://tutego.de/blog/javainsel/2007/05/contact-to-javax-beans-binding.md'
date: '2007-05-09'
description: 'This little example will show you how to bind a JTextField to a name property of a Person with Beans Binding (JSR-295). First the Person: package com.tutego.binding;import java.beans.PropertyChangeListener;import java.beans.PropertyChangeSupport;public class Person{ private PropertyChangeSupport pc…'
taxonomy:
  category:
    - Allgemein
---

# Contact to javax.beans.binding

![Illustration einer tropischen Insel mit Palmen und einem hohen Berg](https://tutego.de/blog/javainsel/user/themes/tutego/images/insel-cover.jpg)# Java Blog für Programmierer

[Java Blog für Programmierer](https://tutego.de/blog/javainsel/)

## Contact to javax.beans.binding

09.05.2007 · [Christian Ullenboom](https://tutego.de/blog/javainsel/author/admin/)

This little example will show you how to bind a `JTextField` to a name property of a Person with [Beans Binding](https://beansbinding.dev.java.net/) (JSR-295). First the Person:

```java
package com.tutego.binding;import java.beans.PropertyChangeListener;import java.beans.PropertyChangeSupport;public class Person{ private PropertyChangeSupport pcs = new PropertyChangeSupport( this ); private String name = ""; public void addPropertyChangeListener( PropertyChangeListener x ) {   pcs.addPropertyChangeListener( x ); } public void removePropertyChangeListener( PropertyChangeListener x ) {   pcs.removePropertyChangeListener( x ); } public String getName() {   return name; } public void setName( String name ) {   String old = getName();   this.name = name;   pcs.firePropertyChange( "name", old, getName() );     System.out.println( "Changed name!" ); }}
```

The code for the JavaBean Person is a litte bit cumbersome because of the `PropertyChangeListener` who will notify the Binding Framework if the model change.

```java
package com.tutego.binding;import javax.beans.binding.BindingContext;import javax.swing.JFrame;import javax.swing.JTextField;public class BindingDemo{ public static void main( String[] args ) {   Person p = new Person();   JTextField tf = new JTextField();    BindingContext bindingContext = new BindingContext();   bindingContext.addBinding( p, "${name}", tf, "text" );   bindingContext.bind();     JFrame f = new JFrame();   f.add( tf );   f.pack();   f.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );   f.setVisible( true );   p.setName( "Christian Ullenboom" ); }}
```

Swing on the other side will although notify the model if the user type text in the text field and press Return. This will change the model.

Kategorien: [Allgemein](https://tutego.de/blog/javainsel/category/allgemein/)

[← Alle Beiträge](https://tutego.de/blog/javainsel/)

---

## Navigation

- Previous: [Using format()/printf() for formatting and Scanner for parsing localized elements](https://tutego.de/blog/javainsel/2007/05/using-formatprintf-for-formatting-and-scanner-for-parsing-localized-elements.md)
- Next: [NASA stellt seine World Wind Applikation in Java vor](https://tutego.de/blog/javainsel/2007/05/nasa-stellt-seine-world-wind-applikation-in-java-vor.md)
