Description
In our application we allow the dragging of a table row to another portion of our ui. For simplicity this is implemented using an onDragDetected handler set on the TableView since all it has to do is put the currently selected rows into the clipboard.
This strategy works will with the initial release of Java 8 but in 8u20 it causes the drag reordering of Table columns to fail.
To reproduce run the following test class:
1. Drag and drop a selected row to another text widget to see the DND support work.
2. Next try dragging on a column header to change their order. It will start to drag and then fail.
3. If you comment out the onDragDetected handler and run again the Table columns can be reordered as expected.
******************************************** Test Class *************************************************
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.input.ClipboardContent;
import javafx.scene.input.Dragboard;
import javafx.scene.input.MouseEvent;
import javafx.scene.input.TransferMode;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class HeaderTest extends Application {
@Override public void start(Stage primaryStage) {
TableView<String> table = new TableView<>();
// add some rows
for ( int i = 1; i <= 25; i++ ) {
table.getItems().add("" +i);
}
// add some columns
for ( int i = 0; i < 5; i++ ) {
final TableColumn<String, String> column =
new TableColumn<>("Column " + i);
column.setPrefWidth(100);
final int columnIndex = i;
column.setCellFactory(c -> new TableCell<String, String>() {
@Override protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if ( empty || getTableRow() == null ) {
setText("");
} else {
setText("row " + getTableRow().getItem() + ", " +
"column " + columnIndex);
}
}
}) ;
table.getColumns().add( column );
}
// setup simple DND of row data...
table.setOnDragDetected(( MouseEvent event) -> {
String sel = table.getSelectionModel().getSelectedItem();
if ( sel != null ) {
Dragboard db = table.startDragAndDrop(TransferMode.COPY);
ClipboardContent content = new ClipboardContent();
content.putString(sel);
db.setContent(content);
event.consume();
}
});
primaryStage.setScene(new Scene(new StackPane(table), 550, 250));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
This strategy works will with the initial release of Java 8 but in 8u20 it causes the drag reordering of Table columns to fail.
To reproduce run the following test class:
1. Drag and drop a selected row to another text widget to see the DND support work.
2. Next try dragging on a column header to change their order. It will start to drag and then fail.
3. If you comment out the onDragDetected handler and run again the Table columns can be reordered as expected.
******************************************** Test Class *************************************************
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.input.ClipboardContent;
import javafx.scene.input.Dragboard;
import javafx.scene.input.MouseEvent;
import javafx.scene.input.TransferMode;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class HeaderTest extends Application {
@Override public void start(Stage primaryStage) {
TableView<String> table = new TableView<>();
// add some rows
for ( int i = 1; i <= 25; i++ ) {
table.getItems().add("" +i);
}
// add some columns
for ( int i = 0; i < 5; i++ ) {
final TableColumn<String, String> column =
new TableColumn<>("Column " + i);
column.setPrefWidth(100);
final int columnIndex = i;
column.setCellFactory(c -> new TableCell<String, String>() {
@Override protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if ( empty || getTableRow() == null ) {
setText("");
} else {
setText("row " + getTableRow().getItem() + ", " +
"column " + columnIndex);
}
}
}) ;
table.getColumns().add( column );
}
// setup simple DND of row data...
table.setOnDragDetected(( MouseEvent event) -> {
String sel = table.getSelectionModel().getSelectedItem();
if ( sel != null ) {
Dragboard db = table.startDragAndDrop(TransferMode.COPY);
ClipboardContent content = new ClipboardContent();
content.putString(sel);
db.setContent(content);
event.consume();
}
});
primaryStage.setScene(new Scene(new StackPane(table), 550, 250));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}