JavaFX disable column reorder in TableView [Solved]


Hi guys, today I will tell you how to remove the column reorder in JavaFX TableView.

First, lets see how a table view looks like.It has it header columns,columns and rows.In the example I created I also imported some demo data.

Here are 4 columns :

  1. Id
  2. First Name
  3. Last Name
  4. Phone Number

Each one of the columns has a fx:id which serves to identify that specific column.

See the official Oracle Documentation :

Our fx:id’s for these columns are

  1. col_id
  2. col_firstname
  3. col_lastname
  4. col_phonenumber
  5. table -> (THIS IS THE FX:ID OF THE TABLE. We will need it later for our column reorder method)

So here is the table-view.fxml file I created :

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="592.0" prefWidth="686.0" xmlns="" xmlns:fx="" fx:controller="table_controller">
      <TableView layoutX="58.0" layoutY="14.0" prefHeight="540.0" prefWidth="571.0" styleClass="table" stylesheets="@../css/table-style.css">
          <TableColumn fx:id="col_id" prefWidth="114.0" text="Id" />
            <TableColumn fx:id="col_firstname" prefWidth="154.0" text="First Name" />
          <TableColumn fx:id="col_lastname" minWidth="0.0" prefWidth="143.0" text="Last Name" />
            <TableColumn fx:id="col_phonenumber" prefWidth="155.0" text="Phone Number" />


And here is the css style :

   -fx-background-color: #b2b9c4;
   -fx-border-color: #525053; 
    -fx-background-color: transparent;
.table .column-header-background{
    -fx-background-color: #e6553a;
.table .column-header-background .label{
    -fx-background-color: transparent;
    -fx-text-fill: white;
    -fx-font-size: 14px;
.table-column {
  -fx-alignment: center;
.table .column-header {
    -fx-background-color: transparent;
    -fx-background-color: #E4E4E4;
    -fx-background-insets: 0, 0 0 1 0;
    -fx-padding: 0.0em;
    /*-fx-border-color : red;*/
    -fx-background-color: white;
    -fx-background-insets: 0, 0 0 1 0;
    -fx-padding: 0.0em;




Now we want to remove the column reorder. Here is the method do that :

public static <S, T> void columnReorder(TableView table, TableColumn<S, T> ...columns) {
    table.getColumns().addListener(new ListChangeListener() {
          public boolean suspended;

          public void onChanged(Change change) {
              if (change.wasReplaced() && !suspended) {
                  this.suspended = true;
                  this.suspended = false;


  • we create our method.
  • it has 2 parameters, one for the table and the other for the table column.
  • for every column of that table, if we drag it and try to replace it, we change the value of our suspended variable.
  • set all the columns to the original place where they were.

If we would call this method, it would look something like this :


NOTE : The order of the columns when we call the method, should be at the same order on the table view. (In order for the method to function correctly)

Hope this was helpful. Stay tuned for more.


Please enter your comment!
Please enter your name here