0

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):

Default TableView

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

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
KageMan
  • 9
  • 2
  • You can check this question to learn a bit on how to make a table editable: https://stackoverflow.com/questions/24732883/javafx-table-cell-editing As for accepting only numbers, that's another question. Please try to keep your questions to just one specific issue each. And welcome to SO! :) – Zephyr Jun 21 '18 at 12:24
  • https://docs.oracle.com/javase/9/docs/api/javafx/scene/control/cell/TextFieldTableCell.html#forTableColumn-javafx.util.StringConverter- (You also need to set `onEditCommit` for each column, since you don't use properties in `Week`...) – fabian Jun 21 '18 at 13:05
  • unrelated to your problem: please learn java naming conventions and stick to them – kleopatra Jun 22 '18 at 09:50

0 Answers0