1

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

};
Labeeb
  • 37
  • 1
  • 8

1 Answers1

1

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

  2. 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

  1. create a Anonymous class

// 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);
  1. 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);
Sahil Manchanda
  • 9,812
  • 4
  • 39
  • 89
  • Thanks bro for the grate snippet, is there any way to listen the TableView rather that the column. (yes of course, I need to know the column is resized or not) – Labeeb Sep 16 '17 at 17:07
  • @Labeeb I have updated my answer to help you ease with multiple column width change handling. If this answer fits your need. please accept the answer :) – Sahil Manchanda Sep 16 '17 at 17:58