1

When tableview has to show data which is in one class (Servicios for example), it is done properly. The problem comes when I has to access another class from one class. In this case TableView shows data from the first class, but does not show data from the second class. I don't know why. Here is my code.

package mrpuppy.entity;

import java.sql.Date;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table(name="tarifa")
public class Tarifa 
{
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Long id;
    @OneToMany(mappedBy = "tarifa")
    private List<Servicio> servicios = new ArrayList<Servicio>();
    @Column(name="precio")
    private float precio;
    @Column(name="descuento")
    private float descuento;
    @Column(name="fecha_inicio")
    private Date fechaInicio;
    @Column(name="fecha_fin")
    private Date fechaFin;

    public Long getId() 
    {
        return id;
    }

    public List<Servicio> getServicios() 
    {
        return servicios;
    }

    public void setServicios(List<Servicio> servicios) 
    {
         this.servicios = servicios;
    }

    public float getPrecio()
    {
         return precio;
    }

public void setPrecio(float precio)
{
    this.precio = precio;
}

public float getDescuento() 
{
    return descuento;
}

public void setDescuento(float descuento)
{
    this.descuento = descuento;
}

public Date getFechaInicio()
{
    return fechaInicio;
}

public void setFechaInicio(Date fechaInicio) 
{
    this.fechaInicio = fechaInicio;
}

public Date getFechaFin()
{
    return fechaFin;
}

public void setFechaFin(Date fechaFin)
{
    this.fechaFin = fechaFin;
}
}

package mrpuppy.entity;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import javax.persistence.Table;

@Entity
@Table(name="servicio")
public class Servicio
{
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
@ManyToOne(cascade = {CascadeType.ALL})
@JoinColumn(name="id_tarifa")
private Tarifa tarifa;
@Column(name="nombre")
private String nombre;
@Column(name="raza")
private String raza;
@OneToOne(mappedBy="servicio", cascade = {CascadeType.ALL})
private CitaServicio citaServicio;

public Long getId() 
{
    return id;
}

public void setId(Long id) 
{
    this.id = id;
}

public Tarifa getTarifa() 
{
    return tarifa;
}

public void setTarifa(Tarifa tarifa)
{
    this.tarifa = tarifa;
}

public String getNombre() 
{
    return nombre;
}

public void setNombre(String nombre) 
{
    this.nombre = nombre;
}

public String getRaza() 
{
    return raza;
}

public void setRaza(String raza) 
{
    this.raza = raza;
}

public CitaServicio getCitaServicio() 
{
    return citaServicio;
}

public void setCitaServicio(CitaServicio citaServicio)
{
    this.citaServicio = citaServicio;
}
}

package mrpuppy.controller.tarifas;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;

import mrpuppy.entity.Servicio;
import mrpuppy.entity.Tarifa;
import mrpuppy.service.TarifaService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Stage;

@Controller
public class TarifasControllerImpl implements TarifasController
{

private Stage primaryStage;
private Scene scene;

private final String css = this.getClass().getResource("/css/tarifas.css").toExternalForm();

@Autowired
private AnadirTarifaController anadirTarifaController;
@Autowired
private TarifaService tarifaService;

@FXML
private TableView<Servicio> tablaServicios;
@FXML
private TableColumn<Servicio, String> columnServicio;
@FXML
private TableColumn<Tarifa, Float> columnTarifa;
@FXML
private TableColumn<Tarifa, Float> columnDescuento;
@FXML
private ComboBox<String> comboRaza;
@FXML
private Button buttonAnadir;
@FXML
private Button buttonMostrar;

@Override
public void openWindow() 
{
    try
    {
        primaryStage = new Stage();
        primaryStage.setResizable(false);
        FXMLLoader loader = new FXMLLoader(getClass().getResource("/view/tarifas.fxml"));
        loader.setController(this);     //Establecemos esta clase como "controller"
        scene = loader.load();
        scene.getStylesheets().add(css);
        primaryStage.setScene(scene);
        primaryStage.show();

        ObservableList<String> oListRazas;
        oListRazas = tarifaService.obtenerRazas();
        comboRaza.setItems(oListRazas);

        buttonAnadir.setOnAction(new EventHandler<ActionEvent>()
                  {
            @Override
            public void handle(ActionEvent event)
            {
                anadirTarifaController.openWindow();
            }
      });

        buttonMostrar.setOnAction(new EventHandler<ActionEvent>()
                  {
            @Override
            public void handle(ActionEvent event)
            {
                Collection<Servicio> lista = new ArrayList<Servicio>();
                String raza = comboRaza.getValue();
                lista = tarifaService.buscarServicios(raza);
                mostrarDatos(lista);
            }
       });

    }
    catch(IOException ioe)
    {
        System.out.println(ioe.getMessage());
    }
}


private void mostrarDatos(Collection<Servicio> lista)
{
    ObservableList<Servicio> servicioData = FXCollections.observableArrayList();
    for(Servicio servicio : lista)
    {
        servicioData.add(servicio);
    }
    columnServicio.setCellValueFactory(new PropertyValueFactory<Servicio, String>("nombre"));
    columnTarifa.setCellValueFactory(new PropertyValueFactory<Tarifa, Float>("precio"));
    columnDescuento.setCellValueFactory(new PropertyValueFactory<Tarifa, Float>("descuento"));

    tablaServicios.setItems(servicioData);
}

}
1
  • Can you show us an example of what does show up? Commented Aug 17, 2015 at 23:02

1 Answer 1

1

By the

private TableView<Servicio> tablaServicios;

your tableview is going to render Servicio objects, so logically the tablecolumns should render some fields/parts of Servicio object. For example

private TableColumn<Servicio, String> columnNombre;
private TableColumn<Servicio, String> columnRaza;

etc. However you are trying to render a Tarifa object in that table. That's not possible. Instead of

 private TableColumn<Tarifa, Float> columnDescuento;
 ...
 columnDescuento.setCellValueFactory(new PropertyValueFactory<Tarifa, Float>("descuento"));

define it as

 private TableColumn<Servicio, Float> columnDescuento;
 ...
 columnDescuento.setCellValueFactory(c -> 
      new ReadOnlyStringWrapper( String.valueOf( c.getValue().getTarifa().getDescuento() ) ) );

Do the same for others.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks!!!! your solution helped me. However, instead of "ReadOnlyStringWrapper" I had to define it as: "ReadOnlyObjectWrapper<Float>".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.