1

I'm trying to bind a TextField to a String (the attribute "nombre") in my program. I've seen that you can bind two JavaFX objects with the bind() method, but since java variables don't have properties, I am unable to use it in order to bind my TextField to a String. Here's the class where I'm trying to achieve this (without getters and setters):

public class NodoJugadores implements Serializable
{
private String nombre;
private int cedula;
private String sexo;
private int edad;
private String equipo;
private int categoria;
private int puntos;
private NodoJugadores proximo;
private NodoJugadores hijoIzquierdo;
private NodoJugadores hijoDerecho;

public NodoJugadores(String nombre, int cedula, String sexo, int edad, String equipo, int categoria, int puntos, NodoJugadores proximo)
{
    this.nombre = nombre;
    this.cedula = cedula;
    this.sexo = sexo;
    this.edad = edad;
    this.equipo = equipo;
    this.categoria = categoria;
    this.puntos = puntos;
    this.proximo = proximo;
}

public NodoJugadores(String nombre, int cedula, String sexo, int edad, String equipo, int categoria, int puntos, NodoJugadores hijoIzquierdo, NodoJugadores hijoDerecho) 
{
    this.nombre = nombre;
    this.cedula = cedula;
    this.sexo = sexo;
    this.edad = edad;
    this.equipo = equipo;
    this.categoria = categoria;
    this.puntos = puntos;
    this.hijoIzquierdo = hijoIzquierdo;
    this.hijoDerecho = hijoDerecho;
}

public void llenarGrid(GridPane grid)
{

    if(sexo.equalsIgnoreCase("m"))
    {
        for(int i = 0 ; i < 8 ; i++)
        {
            TextField text = new TextField(nombre);

            grid.add(text, i, 0);
        }
    }
}

static final long serialVersionUID = 8925409;
}

Please, try to explain with an actual example of how this would work. I would greatly appreciate it. Also, it's worth mentioning that I can't convert my String to a StringProperty because I'm Serializing the class.

GFV450
  • 111
  • 1
  • 11
  • You can use a `StringProperty` and still make the class `Serializable`. You just have to define a custom serialized form. – James_D Nov 23 '15 at 14:52
  • @James_D I'm sorry, but I'm not familiar with custom serializing, How would I do this? – GFV450 Nov 23 '15 at 14:59

1 Answers1

3

Solution 1: Use a StringProperty

Use a StringProperty and a custom serialized form to make the class Serializable:

public class NodoJugadores implements Serializable {

    private final transient StringProperty nombre = new SimpleStringProperty();

    public StringProperty nombreProperty() {
        return nombre ;
    }

    public final String getNombre() {
        return nombreProperty().get();
    }

    public final void setNombre(String nombre) {
        nombreProperty().set(nombre);
    }

    private void writeObject(ObjectOutputStream s) throws IOException {
        s.defaultWriteObject();
        s.writeObject(getNombre());
    }

    private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundException {
        s.defaultReadObject();
        this.nombre = new SimpleStringProperty((String) s.readObject());
    }
}

Solution 2: Use PropertyChangeListeners and a JavaBeanPropertyAdapter

This is covered in JavaBean wrapping with JavaFX Properties, but basically you would do

public class NodoJugadores implements Serializable {

    private String nombre ;

    private final PropertyChangeSupport pcs ;

    public NodoJugadores() {
        this.pcs = new PropertyChangeSupport(this);
    }

    public String getNombre() {
        return nombre ;
    }

    public void setNombre(String nombre) {
        String oldNombre = this.nombre ;
        this.nombre = nombre ;
        pcs.firePropertyChange("nombre", oldNombre, this.nombre);
    }

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

}

Then in your UI code you can do

Label nombreLabel = new Label();
NodoJugadores nj = new NodoJugadores();
nombreLabel.textProperty().bind(new JavaBeanStringPropertyBuilder()
    .bean(nj)
    .name("nombre")
    .build());
Community
  • 1
  • 1
James_D
  • 201,275
  • 16
  • 291
  • 322