Тема: Допоможіть написати метод, який буде обчислювати суму в колонці JavaFx
Controller.java
package main;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
//import java.util.Random;
import java.util.Scanner;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.HBox;
import main.Znach;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
public class Controller {
private ObservableList<Znach> usersData = FXCollections.observableArrayList();
@FXML
private TableView<Znach> table;
@FXML
private TableColumn<Znach, Integer> firstColumn;
@FXML
private TableColumn<Znach, Integer> secondColumn;
@FXML
private TextField text_fld1;
@FXML
private TextField text_fld2;
@FXML
private Button btn_add;
@FXML
private Label lbl;
@FXML
private HBox box;
// *********************
@FXML
private void initialize() {
// *******************************
firstColumn.setCellValueFactory(new PropertyValueFactory<Znach, Integer>("columnX"));
secondColumn.setCellValueFactory(new PropertyValueFactory<Znach, Integer>("columnY"));
// *******************
table.setItems(usersData);
}
/// *********************************
public void AddRecord() {
Znach znach = new Znach();
znach.setColumnX(Integer.parseInt(text_fld1.getText()));
znach.setColumnY(Integer.parseInt(text_fld2.getText()));
usersData.add(znach);
// text_fld1.clear();
// text_fld2.clear();
System.out.println(Integer.parseInt(text_fld1.getText()));
System.out.println(Integer.parseInt(text_fld2.getText()));
}
public void sumNumber(ActionEvent t) {
int b = Integer.parseInt(text_fld1.getText());
int n = Integer.parseInt(text_fld2.getText());
int sum = 0;
int summs = 0;
sum = sum + b;
summs = summs + n;
// lbl.setText("Сума X "+sum);
// int[] number = {41, 4, 7, 8};
// int summ = 0;
// for(int i = 0; i < number.length; i++){
// summ = summ+number[i];
// }
lbl.setText("Сума X " + sum + "Сума Y " + summs);
}
public void sumNumbers(ActionEvent event) {
// int usersData;
// int sum=0;
// for(int i=0; i<usersData.length; i++) {
// sum=sum+usersData[i];
///
// lbl.setText(Integer.toString(sum));
// }
// Random rand = new Random();
// int myrand = rand.nextInt(50)+1;
// lbl.setText(Integer.toString(myrand));
// System.out.println(Integer.toString(myrand));
}
}
Znach.java
package main;
public class Znach {
private int columnX;
private int columnY;
public Znach(int columnX, int columnY) {
this.columnX = columnX;
this.columnY = columnY;
}
public Znach() {
}
public int getColumnX() {
return columnX;
}
public void setColumnX(int columnX) {
this.columnX = columnX;
}
public int getColumnY() {
return columnY;
}
public void setColumnY(int columnY) {
this.columnY = columnY;
}
@Override
public String toString() {
return "Y=" + getColumnY() + " " + "X=" + getColumnX() + "\n";
}
}
ще є main.java
і tableview.fxml