I'm currently trying to create a JavaFX application where the User can write numbers in each cell and do something with it later.
Currently my table looks like that (0 is the default value):
Every cell should be editable and only accept numbers (doubles to be exact)
My Code looks like this :
My Class that get used in the tableview (fills the tableview with 6 objects of this class)
public class Week {
double Monday;
double Tuesday;
double Wednesday;
double Thursday;
double Friday;
double Saturday;
double Sunday;
public Week(double monday, double tuesday, double wednesday, double thursday, double friday, double saturday,
double sunday) {
Monday = monday;
Tuesday = tuesday;
Wednesday = wednesday;
Thursday = thursday;
Friday = friday;
Saturday = saturday;
Sunday = sunday;
--
+getter/setter for each variable
}
And my controller to init the table
public class ViewController implements Initializable {
@FXML
private TableView<Week> fxTable;
//Table Elements
TableColumn<Week,Double> Mo;
TableColumn<Week,Double> Tu;
TableColumn<Week,Double> We;
TableColumn<Week,Double> Th;
TableColumn<Week,Double> Fr;
TableColumn<Week,Double> Sa;
TableColumn<Week,Double> Su;
public void initialize(URL fxmlFileLocation, ResourceBundle resources) {
staticInit();
createTableCols();
fillTable();
}
public void createTableCols() {
Mo = new TableColumn<Week, Double>("Mo");
Mo.setResizable(false);
Mo.setSortable(false);
Mo.setCellValueFactory(new PropertyValueFactory<Week, Double>("Monday"));
Tu = new TableColumn<Week, Double>("Tu");
Tu.setResizable(false);
Tu.setSortable(false);
Tu.setCellValueFactory(new PropertyValueFactory<Week, Double>("Tuesday"));
We = new TableColumn<Week, Double>("We");
We.setResizable(false);
We.setSortable(false);
We.setCellValueFactory(new PropertyValueFactory<Week, Double>("Wednesday"));
Th = new TableColumn<Week, Double>("Th");
Th.setResizable(false);
Th.setSortable(false);
Th.setCellValueFactory(new PropertyValueFactory<Week, Double>("Thursday"));
Fr = new TableColumn<Week, Double>("Fr");
Fr.setResizable(false);
Fr.setSortable(false);
Fr.setCellValueFactory(new PropertyValueFactory<Week, Double>("Friday"));
Sa = new TableColumn<Week, Double>("Sa");
Sa.setResizable(false);
Sa.setSortable(false);
Sa.setCellValueFactory(new PropertyValueFactory<Week, Double>("Saturday"));
Su = new TableColumn<Week, Double>("Su");
Su.setResizable(false);
Su.setSortable(false);
Su.setCellValueFactory(new PropertyValueFactory<Week, Double>("Su"));
fxTable.getColumns().addAll(Mo,Tu,We,Th,Fr,Sa,Su);
//prevent user to move the cols
fxTable.getColumns().addListener(new ListChangeListener() {
public boolean suspended;
@Override
public void onChanged(Change change) {
change.next();
if (change.wasReplaced() && !suspended) {
this.suspended = true;
fxTable.getColumns().setAll(Mo,Di,Mi,Do,Fr,Sa,So);
this.suspended = false;
}
}
});
}
public void fillTable () {
Week wT1 = new Week(0, 0, 0, 0, 0, 0, 0);
Week wT2 = new Week(0, 0, 0, 0, 0, 0, 0);
Week wT3 = new Week(0, 0, 0, 0, 0, 0, 0);
Week wT4 = new Week(0, 0, 0, 0, 0, 0, 0);
Week wT5 = new Week(0, 0, 0, 0, 0, 0, 0);
Week wT6 = new Week(0, 0, 0, 0, 0, 0, 0);
ObservableList<Week> emptyList = FXCollections.observableArrayList();
emptyList.addAll(wT1,wT2,wT3,wT4,wT5,wT6);
fxTable.setItems(emptyList);
}
}
How could i achieve that? Im pretty new to javaFX so help woud be nice. Greetings and thanks in advance