4

I'm having some issues with the Constrained Resize Policy for a TableView inside a ScrollPane. It seems as though the headings do not line up completely with its column. On scroll or resize the columns snap to their correct position.

I created a small example to demonstrate:

import java.util.ArrayList;

import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.scene.Scene;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class Test extends Application
{
    public static void main(String[] args) throws Exception
    {
        launch(args);
    }

    @Override
    public void start(Stage primarystage) throws Exception
    {
        // Create layout
        VBox root = new VBox();

        TableView<TableObject> table = new TableView<TableObject>();
        TableColumn<TableObject, String> col1 = new TableColumn<TableObject, String>("Column 1");
        TableColumn<TableObject, String> col2 = new TableColumn<TableObject, String>("Column 2");
        table.getColumns().addAll(col1, col2);

        col1.setCellValueFactory(new PropertyValueFactory<TableObject, String>("column1"));
        col2.setCellValueFactory(new PropertyValueFactory<TableObject, String>("column2"));
        table.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);

        root.getChildren().add(table);

        ScrollPane scrollpane = new ScrollPane();
        scrollpane.setFitToWidth(true);
        scrollpane.setFitToHeight(true);
        scrollpane.setPrefSize(500, 200);
        scrollpane.setContent(root);

        // Create and show scene            
        Scene scene = new Scene(scrollpane);
        primarystage.setScene(scene);
        primarystage.show();

        // Populate table
        ArrayList<TableObject> data = new ArrayList<TableObject>();
        for (int i = 0; i < 20;)
        {
            TableObject entry = new TableObject(String.valueOf(i++), String.valueOf(i++));
            data.add(entry);
        }

        table.setItems(FXCollections.observableArrayList(data));
    }

    public class TableObject
    {
        private StringProperty column1;
        private StringProperty column2;

        public TableObject(String col1, String col2)
        {
            column1 = new SimpleStringProperty(col1);
            column2 = new SimpleStringProperty(col2);
        }

        public StringProperty column1Property()
        {
            return column1;
        }

        public StringProperty column2Property()
        {
            return column2;
        }
    }
}

The result is this:

Sample Screenshot

Is there perhaps something I am missing here?

Thanks

8
  • 1
    I tried running your example and the output seems to be alright. Check this out Commented Sep 16, 2014 at 14:18
  • Wow, this is actually really worrying now. If code is going to be inconsistent. Thanks for checking, any ideas? Javafx versions or something? Commented Sep 16, 2014 at 14:22
  • There can be various reasons. Which JavaFX version are you using ? I tried it on Java 1.8.0 b-132 on Mac Commented Sep 16, 2014 at 14:23
  • Java 1.8.0_05, Windows 8.1 Commented Sep 16, 2014 at 14:31
  • I do not have windows 8.1, but if you come across the issue again, raise a [JIRA]( javafx-jira.kenai.com/secure/Dashboard.jspa) for the JAVAFX dev team Commented Sep 16, 2014 at 14:46

1 Answer 1

1

just show the stage after setting table items:

stage.show();        
table.setItems(FXCollections.observableArrayList(data));
Sign up to request clarification or add additional context in comments.

4 Comments

the stage is shown - or how would s/he have a made a screenshot :) Please read the code thoroughly (note: the relative sequence of show vs. setItems doesn't matter)
I was wrong - here sequence does matter ;) Edited to revert the vote (and format the code vs. bolding it)
That's not always possible. What if the data for the table is retrieved from a background thread?
Then you have to calculate how many rows you have and if the ScrollPane will be active based on the window size and set the value of scaleX of the col dynamically

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.