1

I want to autoset the column-width programatically so that the table header is fitted to its content.

@Override
public void initialize(URL url, ResourceBundle rb) {
    TableColumn<String, String> col1 = new TableColumn<>("C1");
    TableColumn<String, String> col2 = new TableColumn<>("Column 2");
    TableColumn<String, String> col3 = new TableColumn<>("Last Column (C3)");
    TableColumn<String, String> col4 = new TableColumn<>("blablabla");
    table.getColumns().add(col1);
    table.getColumns().add(col2);
    table.getColumns().add(col3);
    table.getColumns().add(col4);
    for(int i=0;i<4;i++) {
        System.out.println("Column width >> "+table.getColumns().get(i).getPrefWidth());
    }
}

The column-width of each header in a table is always set to a value of 80. How can I get and set the the width values so that the header titles are visible in each column header?

wogsland
  • 9,106
  • 19
  • 57
  • 93
DummyTester
  • 13
  • 1
  • 5

4 Answers4

1

You can use the FontMetrics here:

TableColumn<String, String> column1 = new TableColumn<>("C1");
    TableColumn<String, String> column2 = new TableColumn<>("Column 2");
    TableColumn<String, String> column3 = new TableColumn<>("Last Column (C3)");
    TableColumn<String, String> column4 = new TableColumn<>("blablabla");
    table.getSelectionModel().getSelectedIndex();
    table.getColumns().add(column1);
    table.getColumns().add(column2);
    table.getColumns().add(column3);
    table.getColumns().add(column4);

    FontMetrics fontMetrics = Toolkit.getToolkit().getFontLoader().getFontMetrics(new Font("Arial", 12));

    for (int i = 0; i < 4; i++) {
        String text = table.getColumns().get(i).getText();
        double textwidth =      fontMetrics.computeStringWidth(text);
        table.getColumns().get(i).setPrefWidth(textwidth + 10);
    }
Marcel
  • 1,606
  • 16
  • 29
0

You can try: Column Resize Policy: https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/TableView.html#getColumnResizePolicy--

        table.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);

See Screenshots:

UNCONSTRAINED_RESIZE_POLICY UNCONSTRAINED_RESIZE_POLICY CONSTRAINED_RESIZE_POLICY CONSTRAINED_RESIZE_POLICY

Marcel
  • 1,606
  • 16
  • 29
  • That doesn't resize to content, and doesn't allow resizing below or above total table width. I think what OP asked about was something like what you get when you double-click on the column separator. – Itai Feb 03 '16 at 16:02
  • as sillyfly mentioned, I an trying to find a solution for resizibg a column that automatically fits its width to its content – DummyTester Feb 03 '16 at 18:00
  • In this case you need to calculate the width of the content and set the width of the Column the the widthest content. I'll write an example later. – Marcel Feb 04 '16 at 10:17
0

ok I found an easy way of how to resize the header-columns to its content. Referring to FontMetrics it calculates the text width and after you are able to set the right width

package tabledemo;
import java.awt.Font;
import java.awt.font.FontRenderContext;
import java.awt.geom.AffineTransform;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.*;
import javafx.scene.control.*;

public class FXMLDocumentController implements Initializable {
    @FXML private TableView<String> table;
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        TableColumn<String, String> column1 = new TableColumn<>("C1");
        TableColumn<String, String> column2 = new TableColumn<>("Column 2");
        TableColumn<String, String> column3 = new TableColumn<>("Last Column (C3)");
        TableColumn<String, String> column4 = new TableColumn<>("blablabla");
        table.getSelectionModel().getSelectedIndex();
        table.getColumns().add(column1);
        table.getColumns().add(column2);
        table.getColumns().add(column3);
        table.getColumns().add(column4);
        for(int i=0;i<4;i++) {
            String text = table.getColumns().get(i).getText();
            AffineTransform affinetransform = new AffineTransform();     
            FontRenderContext frc = new FontRenderContext(affinetransform,true,true);     
            Font font = new Font("Arial", Font.PLAIN, 12); //assuming 'Arial' is standard font in TableView
            double textwidth = (double)(font.getStringBounds(text, frc).getWidth());
            table.getColumns().get(i).setPrefWidth(textwidth+10);
        }            
    }     
}

Header-width fitted to its content

Community
  • 1
  • 1
DummyTester
  • 13
  • 1
  • 5
  • This is the right way. But i Would recomand to use the JavaFX api: FontMetrics fontMetrics = Toolkit.getToolkit().getFontLoader().getFontMetrics(new Font("Arial", 12)); fontMetrics.computeStringWidth(text); But its not public api so be carefull with its use. – Marcel Feb 04 '16 at 14:47
0

One more thing I recommend to you is that you need to consider the text style of column header first to use fontMetrics.

CSS analyzer or senicView will give big help to insepect the css property.

truejasonxiefans
  • 179
  • 2
  • 12