DummyTester
DummyTester

Reputation: 13

How to autoset column-width of each column header in a table

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?

Upvotes: 1

Views: 3316

Answers (4)

truejasonxiefans
truejasonxiefans

Reputation: 179

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.

Upvotes: 0

DummyTester
DummyTester

Reputation: 13

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

Upvotes: 0

Marcel
Marcel

Reputation: 1768

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

Upvotes: 1

Marcel
Marcel

Reputation: 1768

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

Upvotes: 0

Related Questions