Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8095341

Hidden Columns confuse Column Re-ordering with Mouse

XMLWordPrintable

      You can re-order the columns in a TableView by dragging the column headers to the left and right. There are "slots" between each column header, and as you drag a header, these slots light up to show you where the column would be inserted when you let go of the mouse button.

      Unfortunately, if you have some hidden columns in the table, the column that you are moving sometimes gets inserted into the wrong slot (i.e. not the slot that is lit up).

      To see this happen, run the following sample code:

      import javafx.application.Application;
      import javafx.beans.property.ObjectProperty;
      import javafx.beans.property.SimpleObjectProperty;
      import javafx.scene.Scene;
      import javafx.scene.control.*;
      import javafx.scene.layout.StackPane;
      import javafx.stage.Stage;

      public class TableReorderBug extends Application {
         
         // boilerplate to build table, add it to a stage, and show stage
         public static void main(String[] args) {
             launch(args);
         }
         @Override public void start(Stage primaryStage) {
             StackPane root = new StackPane();
             root.getChildren().add( buildTable() );
             primaryStage.setScene(new Scene(root, 650, 150));
             primaryStage.show();
         }

         private TableView buildTable() {

            // create table with a bunch of column and no rows...
            TableView<Integer> table = new TableView<>();
            for ( int i = 1; i <= 10; i++ ) {
               TableColumn<Integer, Integer> column = new TableColumn<>("" + i);
               table.getColumns().add( column );
               
               // sneak some hidden columns in there
               column = new TableColumn<>("h" + i);
               column.setVisible( false );
               table.getColumns().add( column );
            }

            return table;
         }
      }


      -------------------------------------------


      For the steps below, I'll refer to the slots like so: slot 0 is to the left of column 1's initial location. The next slot, to the right of column 1, is slot 1. Then slot 2, 3, etc.)

      1) Restart the sample program.
      2) Drag column 1 to slot 1. As expected, the column position doesn't change.
      3) Drag column 1 to slot 2. As expected, the column 1 and 2 swap positions.
      4) Drag column 1 to slot 0. As expected, the column 1 and 2 swap positions.
      5) Drag column 1 to slot 1 again. What? Why did they swap positions this time?

      Another example:

      1) Restart the sample program.
      2) Drag column 1 to slot 2. As expected, the column 1 and 2 swap positions.
      3) Drag column 1 to slot 0. As expected, the column 1 and 2 swap positions.
      4) Drag column 4 to slot 1. What? It behaves like it was dragged to slot 0?!

      You can easily prove that this behaviour is being caused by the hidden columns. Just comment out the last three lines of the 'buildTable' method and try following the steps again. This time, everything behaves as expected.

            jgiles Jonathan Giles
            cbanackjfx Cory Banack (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported: