Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
524 views
in Technique[技术] by (71.8m points)

java - How to disable column-reordering in a JavaFX2 TableView?

JavaFX2's TableView features "Column reordering by the user at runtime". I'd like to disable this feature for one specific table in my Application.

Looking at the API doc, there is no obvious API hook for this. There is, however, the columns-property. According to the doc, it represents

The TableColumns that are part of this TableView. As the user reorders the TableView columns, this list will be updated to reflect the current visual ordering.

Hoping that I'd at least be able to reset a change after it occurred, I tried adding a listener to reset changes after the fact.

import javafx.application.Application;
import javafx.collections.ListChangeListener;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.stage.Stage;

public class TableTest extends Application {

  public static void main(String[] args) {
    launch(args);
  }


  @Override
  public void start(Stage stage) {
    TableView tableView = new TableView();
    tableView.getColumns().setAll(new TableColumn(), new TableColumn());
    tableView.getColumns().addListener(new ListChangeListener() {
        @Override
        public void onChanged(Change change) {
            if (change.wasPermutated()){
               change.reset();
            }
        }
    });
    stage.setScene(new Scene(tableView));
    stage.show();
  }
}

However, the listener aborts with an IllegalStateException when I ask for wasPermutated.
Is there a way to prevent reordering, or at least revert it programatically?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

See below a SSCCE that shows that the listener gets called - but the flag is set to added when moving columns. Note that you need to call next() before using the change or you will get an IllegalStateException. See the javadoc of ListChangeListener.Change for a simple canonical example.

import javafx.application.Application;
import javafx.collections.ListChangeListener;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.stage.Stage;

public class TableTest extends Application {

  public static void main(String[] args) {
      launch(args);
  }

  @Override
  public void start(Stage stage) {
    final TableView tableView = new TableView();
    final TableColumn[] columns = {new TableColumn("1"), new TableColumn("2")};
    tableView.getColumns().setAll(columns);
    tableView.getColumns().addListener(new ListChangeListener() {
        public boolean suspended;

        @Override
        public void onChanged(Change change) {
            change.next();
            if (change.wasReplaced() && !suspended) {
                this.suspended = true;
                tableView.getColumns().setAll(columns);
                this.suspended = false;
            }
        }
    });
    stage.setScene(new Scene(tableView));
    stage.show();
  }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...