Skip to content

Instantly share code, notes, and snippets.

@jewelsea
Created December 6, 2013 09:47
Show Gist options
  • Save jewelsea/7821196 to your computer and use it in GitHub Desktop.
Save jewelsea/7821196 to your computer and use it in GitHub Desktop.
Use Drag and Drop to reorder items in a JavaFX ListView
import javafx.application.Application;
import javafx.collections.*;
import javafx.geometry.*;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.image.*;
import javafx.scene.input.*;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import java.util.*;
public class ListOrganizer extends Application {
private static final String PREFIX =
"http://icons.iconarchive.com/icons/jozef89/origami-birds/72/bird";
private static final String SUFFIX =
"-icon.png";
private static final ObservableList<String> birds = FXCollections.observableArrayList(
"-black",
"-blue",
"-red",
"-red-2",
"-yellow",
"s-green",
"s-green-2"
);
private static final ObservableList<Image> birdImages = FXCollections.observableArrayList();
@Override
public void start(Stage stage) throws Exception {
birds.forEach(bird -> birdImages.add(new Image(PREFIX + bird + SUFFIX)));
ListView<String> birdList = new ListView<>(birds);
birdList.setCellFactory(param -> new BirdCell());
birdList.setPrefWidth(180);
VBox layout = new VBox(birdList);
layout.setPadding(new Insets(10));
stage.setScene(new Scene(layout));
stage.show();
}
public static void main(String[] args) {
launch(ListOrganizer.class);
}
private class BirdCell extends ListCell<String> {
private final ImageView imageView = new ImageView();
public BirdCell() {
ListCell thisCell = this;
setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
setAlignment(Pos.CENTER);
setOnDragDetected(event -> {
if (getItem() == null) {
return;
}
ObservableList<String> items = getListView().getItems();
Dragboard dragboard = startDragAndDrop(TransferMode.MOVE);
ClipboardContent content = new ClipboardContent();
content.putString(getItem());
dragboard.setDragView(
birdImages.get(
items.indexOf(
getItem()
)
)
);
dragboard.setContent(content);
event.consume();
});
setOnDragOver(event -> {
if (event.getGestureSource() != thisCell &&
event.getDragboard().hasString()) {
event.acceptTransferModes(TransferMode.MOVE);
}
event.consume();
});
setOnDragEntered(event -> {
if (event.getGestureSource() != thisCell &&
event.getDragboard().hasString()) {
setOpacity(0.3);
}
});
setOnDragExited(event -> {
if (event.getGestureSource() != thisCell &&
event.getDragboard().hasString()) {
setOpacity(1);
}
});
setOnDragDropped(event -> {
if (getItem() == null) {
return;
}
Dragboard db = event.getDragboard();
boolean success = false;
if (db.hasString()) {
ObservableList<String> items = getListView().getItems();
int draggedIdx = items.indexOf(db.getString());
int thisIdx = items.indexOf(getItem());
Image temp = birdImages.get(draggedIdx);
birdImages.set(draggedIdx, birdImages.get(thisIdx));
birdImages.set(thisIdx, temp);
items.set(draggedIdx, getItem());
items.set(thisIdx, db.getString());
List<String> itemscopy = new ArrayList<>(getListView().getItems());
getListView().getItems().setAll(itemscopy);
success = true;
}
event.setDropCompleted(success);
event.consume();
});
setOnDragDone(DragEvent::consume);
}
@Override
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (empty || item == null) {
setGraphic(null);
} else {
imageView.setImage(
birdImages.get(
getListView().getItems().indexOf(item)
)
);
setGraphic(imageView);
}
}
}
// Iconset Homepage: http://jozef89.deviantart.com/art/Origami-Birds-400642253
// License: CC Attribution-Noncommercial-No Derivate 3.0
// Commercial usage: Not allowed
}
@jewelsea
Copy link
Author

jewelsea commented Dec 6, 2013

Answer to StackOverflow Question: How to create a reorder-able TableView in JavaFx

@mojmirnovak778
Copy link

Hi jewelsea, I have one question please. I can't compile this code because an error on this row:

birds.forEach(bird -> birdImages.add(new Image(PREFIX + bird + SUFFIX)));

Error text:
"bird cannot be resolved to a variable".

I think there is something with this "->".

I use Eclipse Kepler and Java (jdk1.7.0_51, jre7).

Thank you for advice. (I am new in Java)

@mojmirnovak778
Copy link

Oh I probably found an answer. The "->" represents a lambda functions like in C#. But they are expected in Java 8 and that is not released officially yet so I was confused before.

@shiyifan
Copy link

nice :-)

@Falco44
Copy link

Falco44 commented Nov 22, 2020

Didn't get my vers working, i was exploding. Thanks for sharing 🙏 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment