Reputation: 37
I need to save column width in user-settings file that i cloud load the TableView with the same width of each column that user previously given for better user experience.
In my research there is a listener on change in width property
tcName.widthProperty().addListener(new ChangeListener<Number>() {});
Where "tcName" is column object.
this is firing several times in a raw while changing the column width.
is there any event called changed ? Or
any possiblility to get output like below
@FXML
private TableView<ItemEmployee> tvData;
@FXML
private TableColumn<ItemEmployee, String> tcCode;
@FXML
private TableColumn<ItemEmployee, String> tcName;
@FXML
private TableColumn<ItemEmployee, String> tcCompany;
tcCode.setCellValueFactory(new PropertyValueFactory<>("Code"));
tcName.setCellValueFactory(new PropertyValueFactory<>("FullName"));
tcCompany.setCellValueFactory(new PropertyValueFactory<>("CompanyName"));
tvData.columnWidthChanged(){
system.out.println("width changed column name: tcCode/Code " );
system.out.println("width of column tcCode/Code : 150" );
saveColumnWidth("tcCode/Code",150);
};
Upvotes: 1
Views: 826
Reputation: 10537
1.Create a boolean variable with default value of false
boolean isUIUpdatedByUser = false;
2.in your
tcName.widthProperty().addListener(new ChangeListener<Number>() {
isUIUpdatedByUser = true; // just set it to true
});
Now handle the close event. there are number of ways to handle the scene close event.e.g. a) Give an Id to your AnchorPane and get the close event like
ap.getScene().getWindow().setOnCloseRequest(event -> { });
b) where you add your stage
primaryStage.setOnCloseRequest(event -> {
});
c) see this answer
Now in your close event.
{ //Save your ui changes saveColumnWidth("tcCode/Code",150); }
To ease the task of handling multiple columns width. You can do the following
// create a listener
final ChangeListener<Number> listener = new ChangeListener<Number>()
{
@Override
public void changed(ObservableValue<? extends Number> observable, Number oldValue, final Number newValue)
{
//do your code here
}
};
and use it like
column1.widthProperty().addListener(listener);
column2.widthProperty().addListener(listener);
Create a common function add call it
public void changelistener(final TableColumn listerColumn) { listerColumn.widthProperty().addListener(new ChangeListener() {
@Override
public void changed(ObservableValue<? extends Number> ov, Number t, Number t1) {
System.out.print(listerColumn.getText() + " ");
System.out.println(t1);
}
});
}
User it like
changelistener(column1);
changelistener(column2);
changelistener(column3);
Upvotes: 1