Dalibor Krivokapic
Dalibor Krivokapic

Reputation: 177

Editing value of TableView column

I encountered problem I am unable to solve. Thing is, my TableView is already populated and I need for cells that match my criteria to set value zero. I have variable korZemljiste and if my condition is fullfilled cells in column colOsnovica need to be set to value 0.00.

Here is what I tried:

    if (korZemljiste < 10000)
    {
        tblTabela.getItems().stream().forEach((o)
           -> {
                colOsnovica.setCellFactory(TextFieldTableCell.forTableColumn());
                colOsnovica.setOnEditCommit(
                    new EventHandler<CellEditEvent<Nepokretnost, Number>>() {
                    @Override
                    public void handle(CellEditEvent<Nepokretnost, Number> t) {
                        ((Nepokretnost) t.getTableView().getItems().get(
                        t.getTablePosition().getRow())
                        ).setOsnovica(t.getNewValue());
    }
});});}

Sorry, for ugly end of code, couldn't make to copy it properly.

This part I modified from Oracle example and I get error:

error: incompatible types: no instance(s) of type variable(s) S exist so that Callback<TableColumn<S,String>,TableCell<S,String>> conforms to Callback<TableColumn<Nepokretnost,Number>,TableCell<Nepokretnost,Number>>
                colOsnovica.setCellFactory(TextFieldTableCell.forTableColumn());

where S is a type-variable: S extends Object declared in method forTableColumn()

error: incompatible types: Number cannot be converted to double
                        ).setOsnovica(t.getNewValue());

My TableView using <Nepokretnost, Number> form.

Note: I guess I don't need this example from Oracle site because I don't want to manually change contents of cell in column, I just want to set them to value 0.00.

Some easy solution anyone?

Thanks in advance.

Upvotes: 1

Views: 740

Answers (2)

Dalibor Krivokapic
Dalibor Krivokapic

Reputation: 177

@james-d

Hm, my mistake. Class is simple, here it is:

package obracun;

import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.property.SimpleStringProperty;

/**
*
* @author Dalibor
*/
public class Nepokretnost {

private final  SimpleStringProperty tipNepokretnosti;
private final  SimpleStringProperty zona;
private final  SimpleStringProperty pravo;
private final  SimpleDoubleProperty povrsina;
private final  SimpleDoubleProperty amortizacija;
private final  SimpleDoubleProperty osnovica;
private final  SimpleDoubleProperty kredit;
private final  SimpleDoubleProperty porez;

public Nepokretnost(String tipNepokretnosti, String zona, String pravo, 
        Double povrsina, double amortizacija, Double osnovica, Double kredit, Double porez) {
    this.tipNepokretnosti = new SimpleStringProperty(tipNepokretnosti);
    this.zona = new SimpleStringProperty(zona);
    this.pravo = new SimpleStringProperty(pravo);
    this.povrsina = new SimpleDoubleProperty(povrsina);
    this.amortizacija = new SimpleDoubleProperty(amortizacija);
    this.osnovica = new SimpleDoubleProperty(osnovica);
    this.kredit = new SimpleDoubleProperty(kredit);
    this.porez = new SimpleDoubleProperty(porez);


}

public String getTipNepokretnosti() {
    return tipNepokretnosti.get();
}

public void setTipNepokretnosti(String tipNepokretnosti) {
    this.tipNepokretnosti.set(tipNepokretnosti);
}

public String getZona() {
    return zona.get();
}

public void setZona(String zona) {
    this.zona.set(zona);
}

public String getPravo() {
    return pravo.get();
}

public void setPravo(String pravo) {
    this.pravo.set(pravo);
}

public double getPovrsina() {
    return povrsina.get();
}

public void setPovrsina(double povrsina) {
    this.povrsina.set(povrsina);
}

public double getAmortizacija() {
    return amortizacija.get();
}

public void setAmortizacija(double amortizacija) {
    this.amortizacija.set(amortizacija);
}

public double getOsnovica() {
    return osnovica.get();
}

public void setOsnovica(double osnovica) {
    this.osnovica.set(osnovica);
}

public double getKredit() {
    return kredit.get();
}

public void setKredit(double kredit) {
    this.kredit.set(kredit);
}

public double getPorez() {
    return porez.get();
}

public void setPorez(double porez) {
    this.porez.set(porez);
}

}

I didn't realized it is necessary to post class here. I had this application made before just in Swing. There I just changed contents of JTableView field. This is kinda different, thanks for help.

I posted and screenshot of app. On screenshot I already added elements in TableView I wanted. Code I posted in question is part of Calculate (Обрачун) button event handler. In this example calculation should change the contents of field Основица to 0.00.

enter image description here

Upvotes: 0

James_D
James_D

Reputation: 209684

For the first error, since your table column type is not a String, you cannot use the no-argument version of TextFieldTableCell.forTableColumn(). The underlying problem is that, to edit the cell, the text field provides a String and the data in the cell is a Number. You need to provide a converter, using the overloaded form of TextFieldTableCell.forTableColumn(...):

colOsnovica.setCellFactory(TextFieldTableCell.forTableColumn(new NumberStringConverter()));

For the second error, just replace t.getNewValue() (which returns a Number) with t.getNewValue().doubleValue() (which gets the double representation of the Number).

Upvotes: 2

Related Questions