如何为JavaFX2 TableView TableColumn“ width change”事件监听器创建?

我想到的是:

tableView.getColumns().addListener(new ListChangeListener() {
    public void anyMethod() {
        System.out.println("TableView Column Width Have Changed")
    }
}

最佳答案

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

10-07 15:19