JavaFX tableview可编辑单元格

时间:2018-06-21 10:56:40

标签: java javafx cells

我目前正在尝试创建一个JavaFX应用程序,用户可以在该应用程序中在每个单元格中写入数字,并在以后对其进行处理。
目前,我的表格看起来像这样(默认值为0):

Default TableView

每个单元格都应该是可编辑的,并且只能接受数字(确切地说是双精度数字)
我的代码如下所示: 在表格视图中使用的“我的班级”(用此类的6个对象填充表格视图)

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
}

还有我用来初始化表格的控制器

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);    
    }
}

我该如何实现?我是javaFX的新手,所以帮助会很棒。 预先致以问候和感谢

0 个答案:

没有答案