JavaFX TableView column width change event listener


Question

How can I create for JavaFX2 TableView TableColumn "width change" event listener?

I have in mind something like:

tableView.getColumns().addListener(new ListChangeListener() {
    public void anyMethod() {
        System.out.println("TableView Column Width Have Changed")
    }
}
1
2
4/29/2019 6:41:42 AM

EXample code of WidthProperty()

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

/**
 *
 * @author reegan
 */
public class TableWidthChange extends Application {

    @Override
    public void start(Stage primaryStage) {
        TableView tableView = new TableView();
        TableColumn column1 =  new TableColumn("Column1");
        TableColumn column2 = new TableColumn("Column2");
        TableColumn column3 = new TableColumn("Column3");

        changelistener(column1);
        changelistener(column2);
        changelistener(column3);

        tableView.getColumns().addAll(column1,column2,column3);
        StackPane root = new StackPane();
        root.getChildren().add(tableView);

        Scene scene = new Scene(root, 300, 250);

        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    /**
     * The main() method is ignored in correctly deployed JavaFX application.
     * main() serves only as fallback in case the application can not be
     * launched through deployment artifacts, e.g., in IDEs with limited FX
     * support. NetBeans ignores main().
     *
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

    public void changelistener(final TableColumn listerColumn) {
        listerColumn.widthProperty().addListener(new ChangeListener<Number>() {

            @Override
            public void changed(ObservableValue<? extends Number> ov, Number t, Number t1) {
                System.out.print(listerColumn.getText() + "  ");
                System.out.println(t1);
            }
        });
    }
}
1
12/21/2013 6:51:54 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon