JavaFX 2 Automatic Column Width

前端 未结 8 807
盖世英雄少女心
盖世英雄少女心 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-------------^-------------^
    }
    
    0 讨论(0)
  • 2020-11-29 01:29

    my idea.

    table.getColumns().add(new TableColumn<>("Num") {
        {
            // 15%
            prefWidthProperty().bind(table.widthProperty().multiply(0.15));
        }
    });
    table.getColumns().add(new TableColumn<>("Filename") {{
        // 20%
        prefWidthProperty().bind(table.widthProperty().multiply(.2));
    }});
    table.getColumns().add(new TableColumn<>("Path") {{
        // 50%
        prefWidthProperty().bind(table.widthProperty().multiply(.5));
    }});
    table.getColumns().add(new TableColumn<>("Status") {
        {
            // 15%
            prefWidthProperty().bind(table.widthProperty().multiply(.15));
        }
    });
    
    0 讨论(0)
  • 2020-11-29 01:33

    As I use SceneBuider, I just define the MinWidth, and MaxWidth to some columns and to the main column I just define the PrefWidth to "USE_COMPUTED_SIZE"

    0 讨论(0)
  • 2020-11-29 01:33

    After 3 years, finally I found the solution, javafx column in tableview auto fit size

    import com.sun.javafx.scene.control.skin.TableViewSkin;
    import javafx.scene.control.Skin;
    import javafx.scene.control.TableColumn;
    import javafx.scene.control.TableView;
    import java.lang.reflect.InvocationTargetException;
    import java.lang.reflect.Method;
    
    public class GUIUtils {
        private static Method columnToFitMethod;
    
        static {
            try {
                columnToFitMethod = TableViewSkin.class.getDeclaredMethod("resizeColumnToFitContent", TableColumn.class, int.class);
                columnToFitMethod.setAccessible(true);
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            }
        }
    
        public static void autoFitTable(TableView tableView) {
            tableView.getItems().addListener(new ListChangeListener<Object>() {
                @Override
                public void onChanged(Change<?> c) {
                    for (Object column : tableView.getColumns()) {
                        try {
                            columnToFitMethod.invoke(tableView.getSkin(), column, -1);
                        } catch (IllegalAccessException | InvocationTargetException e) {
                            e.printStackTrace();
                        }
                    }
                }
            });
        }
    }
    
    0 讨论(0)
  • 2020-11-29 01:34

    This works for me in JavaFX 8

    table.setColumnResizePolicy( TableView.CONSTRAINED_RESIZE_POLICY );
    col1.setMaxWidth( 1f * Integer.MAX_VALUE * 50 ); // 50% width
    col2.setMaxWidth( 1f * Integer.MAX_VALUE * 30 ); // 30% width
    col3.setMaxWidth( 1f * Integer.MAX_VALUE * 20 ); // 20% width
    

    In the other examples you have the problem, the vertical scrollbar width is ignored.

    0 讨论(0)
  • 2020-11-29 01:34

    If you had 4 columns and only last column needed to expand to fill the rest of the table width and the other columns remained the size I set in Scene Builder.

    double width = col1.widthProperty().get();
    width += col2.widthProperty().get();
    width += col3.widthProperty().get();
    
    col4.prefWidthProperty().bind(table.widthProperty().subtract(width));
    

    Or if you had 2 columns that needed to expand then.

    double width = col1.widthProperty().get();
    width += col3.widthProperty().get();
    
    col2.prefWidthProperty().bind(table.widthProperty().subtract(width).divide(2));
    col4.prefWidthProperty().bind(table.widthProperty().subtract(width).divide(2));
    
    0 讨论(0)
提交回复
热议问题