JavaFX 2 Automatic Column Width

前端 未结 8 837
盖世英雄少女心
盖世英雄少女心 2020-11-29 01:09

I have a JavaFX 2 table that is displaying contact details for people, lets imagine there are three columns: first name, last name and email address. When my application sta

8条回答
  •  眼角桃花
    2020-11-29 01:29

    To generelize the width and use any number and make sure that the width is 100% of the table's width, you can use :

    double[] widths = {20, 30, 50, 20, 30, 70, 50};//define the width of the columns
    
    //calculate the sum of the width
    double sum = 0;
    for (double i : widths) {
        sum += i;
    }
    
    //set the width to the columns
    for (int i = 0; i < widths.length; i++) {
        table.getColumns().get(i).prefWidthProperty().bind(
                table.widthProperty().multiply(sizes[i] / sum));
        //---------The exact width-------------^-------------^
    }
    

提交回复
热议问题