0
votes

My experience with TableView is only 3 days(Searching and learning).JavaFx tableView seems very complicated to me.

I have a simple application with tableview of few columns(Name,Quantity,MRP,Total). The table is editable. If a Quantity cell is edited then it would do multiplication of quantitycolumn*mrpcolumn and set the value for totalcolumn.

This is what I want: enter image description here

Here is the data processing Class called TableData:

public class TableData {
    private final SimpleStringProperty name;
    private final SimpleStringProperty batch;
    private final SimpleStringProperty exp;
    private final SimpleIntegerProperty qty;
    private final SimpleDoubleProperty mrp;
    private final SimpleDoubleProperty amt;


    public TableData(String name,String batch,
                     String exp,int qty,Double mrp,Double amt) {
        this.name = new SimpleStringProperty(name);
        this.batch = new SimpleStringProperty(batch);
        this.exp = new SimpleStringProperty(exp);
        this.qty = new SimpleIntegerProperty(qty);
        this.mrp = new SimpleDoubleProperty(mrp);
        this.amt = new SimpleDoubleProperty(amt);

    }

    public String getName() {
        return name.get();
    }

    public SimpleStringProperty nameProperty() {
        return name;
    }

    public void setName(String name) {
        this.name.set(name);
    }

    public String getBatch() {
        return batch.get();
    }

    public SimpleStringProperty batchProperty() {
        return batch;
    }

    public void setBatch(String batch) {
        this.batch.set(batch);
    }

    public String getExp() {
        return exp.get();
    }

    public SimpleStringProperty expProperty() {
        return exp;
    }

    public void setExp(String exp) {
        this.exp.set(exp);
    }

    public int getQty() {
        return qty.get();
    }

    public SimpleIntegerProperty qtyProperty() {
        return qty;
    }

    public void setQty(int qty) {
        this.qty.set(qty);
    }

    public double getMrp() {
        return mrp.get();
    }

    public SimpleDoubleProperty mrpProperty() {
        return mrp;
    }

    public void setMrp(double mrp) {
        this.mrp.set(mrp);
    }

    public double getAmt() {
        return amt.get();
    }

    public SimpleDoubleProperty amtProperty() {
        return amt;
    }

    public void setAmt(double amt) {
        this.amt.set(amt);
    }
}

I tried to add listener as follows in initialize():

tableView.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<TableData>() {
    @Override
    public void changed(ObservableValue<? extends TableData> observable, TableData oldValue, TableData newValue) {
        System.out.println("The value:"+newValue.getQty());
        getQtyAmt(newValue);
        //getQtyAmt(newValue);

    }
});

getQtyAmt method:

public void getQtyAmt(TableData tableData){
    Double am = tableData.getMrp() * tableData.getQty();
    tableData.setAmt(am);
}

Here is how I am making the TableView editable:

public void editTable(){

    nameColumn.setCellFactory(TextFieldTableCell.forTableColumn());
    qtyColumn.setCellFactory(TextFieldTableCell.forTableColumn(new IntegerStringConverter()));
    expColumn.setCellFactory(TextFieldTableCell.forTableColumn());
    mrpColumn.setCellFactory(TextFieldTableCell.forTableColumn(new DoubleStringConverter()));
    amtColumn.setCellFactory(TextFieldTableCell.forTableColumn(new DoubleStringConverter()));
    final int[] ad = {0};

    nameColumn.setOnEditCommit(new EventHandler<TableColumn.CellEditEvent<TableData, String>>() {
        @Override
        public void handle(TableColumn.CellEditEvent<TableData, String> event) {
            ((TableData)event.getTableView().getItems().get(event.getTablePosition().getRow())).setName(event.getNewValue());
            //TablePosition tp = tableView.getFocusModel().getFocusedCell();

        }
    });
    qtyColumn.setOnEditCommit(new EventHandler<TableColumn.CellEditEvent<TableData, Integer>>() {
        @Override
        public void handle(TableColumn.CellEditEvent<TableData, Integer> event) {
            ((TableData)event.getTableView().getItems().get(event.getTablePosition().getRow())).setQty(event.getNewValue());

            ad[0] = event.getTableView().getItems().get(event.getTablePosition().getRow()).getQty();
            dynCal();


        }
    });
    expColumn.setOnEditCommit(new EventHandler<TableColumn.CellEditEvent<TableData, String>>() {
        @Override
        public void handle(TableColumn.CellEditEvent<TableData, String> event) {
            ((TableData)event.getTableView().getItems().get(event.getTablePosition().getRow())).setExp(event.getNewValue());
        }
    });
    mrpColumn.setOnEditCommit(new EventHandler<TableColumn.CellEditEvent<TableData, Double>>() {
        @Override
        public void handle(TableColumn.CellEditEvent<TableData, Double> event) {
            event.getTableView().getItems().get(event.getTablePosition().getRow()).setMrp(event.getNewValue());

        }
    });



}

How I can do real time calculation of two cells(quantitycolumn*mrpcolumn) and then set the result to amtcolumn cell?

1
You mean real-time calculation as in while typing or after pressing enter? - ihsan
Pressing Enter is a solution too but I am trying get it while typing. - u4547878
you should look into event listeners. I unfortunately don't have to time to write the code but you should be fine by using Google and take a look at this: stackoverflow.com/questions/28478525/… - ihsan
I updated the post please check - u4547878
in the constructor add: this.amt.bind(this.qty.multiply(this.mrp)); - Kachna

1 Answers

0
votes

Since Amt is calculated from other properties, you should probably make it read only, or at the very least not allow to set it through the constructor.

The simplest way is to simply have it be the actual calculation you want -

DoubleBinding amt = qty.multiply(mrp);

Since DoubleBinding implements ObservableValue<Number> you should have no problem using it as a non-editable value in the table.

See also Bindings