PropertyValueFactory java

0

I have the sig. code in my FXML driver:

package floreria;

import DB.ConexionMYSQL;
import java.net.URL;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.ContextMenuEvent;
import objetos.Clientes;

public class FXMLDocumentController implements Initializable {

private Label label;
@FXML
private TextField txtID;
@FXML
private TextField txtNOMBRE;
@FXML
private TextField txtCORREO;
@FXML
private TextField txtDIRECCION;
@FXML
private TextField txtTELEFONO;
@FXML
private Button btnNUEVO;
@FXML
private Button btnAGREGAR;
@FXML
private Button btnELIMINAR;
@FXML
private TableView<Clientes> tablaCLIENTES;
@FXML
private TableColumn<Clientes, Long> columnaID;
@FXML
private TableColumn<Clientes, String> columnaNOMBRE;
@FXML
private TableColumn<Clientes, String> columnaCORREO;
@FXML
private TableColumn<Clientes, String> columnaDIRECCION;
@FXML
private TableColumn<Clientes, String> columnaTELEFONO;

private void handleButtonAction(ActionEvent event) {
    System.out.println("You clicked me!");
    label.setText("Hello World!");
}

public ObservableList<Clientes> data = FXCollections.observableArrayList();
private void setTable(){//asignamos los valores a las columnas
        columnaID.setCellFactory(
        new PropertyValueFactory<> ("id"));

        columnaNOMBRE.setCellFactory(
        new PropertyValueFactory<> ("nombre"));

        columnaCORREO.setCellFactory(
        new PropertyValueFactory<> ("correo"));

        columnaDIRECCION.setCellFactory(
        new PropertyValueFactory<> ("direccion"));

        columnaTELEFONO.setCellFactory(
        new PropertyValueFactory<> ("telefono"));

        tablaCLIENTES.setItems(data);
}


private void rellenarTabla(){
        data.clear();

        String query = ("SELECT * FROM clientes");
        ConexionMYSQL conexionMYSQL = new ConexionMYSQL();//conectamos con la base de datos
        Connection conn = conexionMYSQL.conectar();//conectamos con la base de datos

        Statement stQuery;

        try {//consulta a base de datos
            stQuery = conn.createStatement();
            ResultSet rsResultado;
            ResultSet ResultSet = rsResultado = stQuery.executeQuery(query);

            while (rsResultado.next()) {
                 data.add(new Clientes(Long.toString(rsResultado.getLong("id")), rsResultado.getString("nombre"), rsResultado.getString("correo"), rsResultado.getString("telefono")));

            }
    } catch (SQLException ex) {
    }

}

@Override
public void initialize(URL url, ResourceBundle rb) {
    setTable();
    rellenarTabla();
}    

@FXML
private void btnNUEVO_click(ActionEvent event) {

    txtID.setDisable(true);//Autoincrementable
    txtNOMBRE.setDisable(false);
    txtDIRECCION.setDisable(false);
    txtTELEFONO.setDisable(false);
    txtCORREO.setDisable(false);

    btnNUEVO.setDisable(true);
    btnELIMINAR.setDisable(false);
    btnAGREGAR.setDisable(false);


}

@FXML
private void btnAGREGAR_click(ActionEvent event) {
    txtCORREO.setText("");
    txtDIRECCION.setText("");
    txtID.setText("");
    txtNOMBRE.setText("");
    txtTELEFONO.setText("");

    btnNUEVO.setDisable(false);
    btnELIMINAR.setDisable(true);
    btnAGREGAR.setDisable(false);

    rellenarTabla();
}

@FXML
private void btnELIMINAR_click(ActionEvent event) {
}

@FXML
private void tablaCLIENTES_contextMenu(ContextMenuEvent event) {
}

}

But when executing it, it sends me the sig. error:

It does not mark any error in any line but in the console it leaves that.

    
asked by M.Sha2 31.03.2017 в 20:05
source

1 answer

0

PropertyValueFactory is a generic class.

A generic class is one that defines one or more placeholders for classes that will be defined at the time of instantiation. For example, java.util.ArrayList<T> defines one, which is T . When you create an instance, you should 1 specify the class by which it is replaced: new ArrayList<String , for example.

As of Java 7, the diamond operator <> can be used for the compiler to automatically assign the types if you can deduce them by context . For example

 ArrayList<String> miLista = new ArrayList<>();

What fails here is this, the compiler can not deduce the types and protest. Therefore, you will have to specify them in the constructor.

I do not know very well how JavaFX or this particular class works, but looking at the examples it seems that the types could be: <Clientes, String> . In that case, the expressions would look like:

columnaID.setCellFactory(
    new PropertyValueFactory<Clientes, String> ("id"));

Of course, adapt it to the types you really want to use.

1 If not specified ( new ArrayList() ), it is as if Object is specified.

    
answered by 01.04.2017 в 02:00