0

Java is new to me and I've been trying to make an int column editable in a TableView for 1.5 days. I know many examples exist here, but they all don't work for me (for whatever reason). One example that I have found very often would be:

colID.setCellFactory(TextFieldTableCell.forTableColumn(new IntegerStringConverter()));

but this leads me to a message about incompatible types. I would need once only a short example of what works. Also, I would be interested to know why the many examples found here do not work for me. I am using JavaFX19, can this be the reason?

It would be great if someone could help me with this. Thank you...

That is my Customer class and I want to edit the ID column and the method in my CotrollerClass causing the problems on setCellFactory...


public class Customer {

/**
* Standardkonstruktor
*/
public Customer(){}

/**
* Konstruktor zur Erzeugung gefüllter Customer-Objekte
* @param id ID des Customers
* @param vorName Vorname des Customers
* @param nachName Nachname des Customers
*/
public Customer(int id, String vorName, String nachName){
    intID = id;
    strVorName = vorName;
    strNachName = nachName;
}

public int getID() {
    return intID;
}

public void setID(int id) {
    this.intID = id;
}

public String getVorName() {
    return strVorName;
}

public void setVorName(String vorName) {
    this.strVorName = vorName;
}

public String getNachName() {
    return strNachName;
}

public void setNachName(String nachName) {
    this.strNachName = nachName;
}    

// <editor-fold defaultstate="collapsed" desc=" Klassenvariablen ">

private int intID;
private String strVorName;
private String  strNachName;    

// </editor-fold>

}


private void rebindData(){
    tblCustomers.setItems(customers);

    colID.setCellValueFactory(new PropertyValueFactory<>("ID"));
    colGivenName.setCellValueFactory(new PropertyValueFactory<>("VorName"));
    colName.setCellValueFactory(new PropertyValueFactory<>("NachName"));
            
    /************************************************************/
    //colID.setCellFactory(TextFieldTableCell.forTableColumn());
    /************************************************************/
    
    
    colID.setCellFactory(TextFieldTableCell.forTableColumn(new IntegerStringConverter()));

    
    //void setCellFactory(Callback<TableColumn<Customer, String>, TableCell<Customer, String>> value) - parameters
    //public static <S,T> Callback<TableColumn<S,T>,TableCell<S,T>> forTableColumn(StringConverter<T> converter)
    
    colGivenName.setCellFactory(TextFieldTableCell.forTableColumn());
    colGivenName.setOnEditCommit(event-> {
        event.getRowValue().setVorName(event.getNewValue());
    });
    
    colName.setCellFactory(TextFieldTableCell.forTableColumn());
    colName.setOnEditCommit(event-> {
        event.getRowValue().setNachName(event.getNewValue());
    });
    
    tblCustomers.refresh();
}    

Progman
  • 16,827
  • 6
  • 33
  • 48
Cabbage
  • 11
  • First, [don’t use PropertyValueFactory.](https://stackoverflow.com/questions/65785787/javafx-tableview-not-showing-data-something-wrong-with-getter-setters/65786210#65786210) Second, IntegerStringConverter implements `StringConverter`, so your `colID` TableColumn must also use that same type as its second argument. Meaning, you must declare it as `TableColumn colID;`. – VGR Nov 13 '22 at 16:23
  • Thank you, I will have a try as soon as possible. – Cabbage Nov 14 '22 at 12:17
  • Hi VGR, belonging to the PropertyValueFactory there is no problem with my strings but with my int value colID. I'm stucking with the type cast because the CellValueFactory wants an observable and the value is of type int. I'm not able to manage the type cast. Code: clientId.setCellValueFactory(f -> f.getValue().idProperty()); I tried: colID.setCellValueFactory(f -> new SimpleIntegerProperty().getValue((Integer.valueOf(f.getValue().getID()))); – Cabbage Nov 14 '22 at 12:46
  • Again hi VGR, so after reading the PropertyValueFactory stuff again, I need a second getter for every getXXX method. Ok, I can do so but it seems a little bit of much work for me. Is that the standard way in Java? I yes, I will do so... Thanks, Cabbage – Cabbage Nov 14 '22 at 12:59
  • IntegerProperty is actually a `Property`, so you will want to declare colID as `TableColumn` and you will want to use [NumberStringConverter](https://openjfx.io/javadoc/19/javafx.base/javafx/util/converter/NumberStringConverter.html) instead of IntegerStringConverter. To restrict the converter’s values to integers, use `new NumberStringConverter(NumberFormat.getIntegerInstance())`. – VGR Nov 14 '22 at 13:00
  • Writing three methods instead of two methods for every property is a little more work, yes. But you only have to do it once. It doesn’t take that long. – VGR Nov 14 '22 at 13:01
  • Hi VGR, sure, it's copy and paste and not so much work after that. I will do so. Thanks again. – Cabbage Nov 14 '22 at 13:05
  • One last question for today: Is it better to to have observable getter and setter in the data container or is it state of the art to do the cast in the controller? – Cabbage Nov 14 '22 at 13:09
  • I’m not sure what cast you are referring to. A data class should have a get-method and property accessor method for every property, and if a property is writable, a set-method as well. No casts should be necessary. – VGR Nov 14 '22 at 13:14

0 Answers0