JavaFX在单击按钮时设置表格视图的表格行工厂
我正在用javafx编写一个表格拖放重新整理表单应用程序。在这个应用程序中,我打算通过拖放来编辑表格顺序,我已经在以前的搜索中为我的表格正式编写了拖放操作,但困扰我的是,我只想在单击按钮时设置拖放tablerowfactory 这是我的密码: '''JavaFX在单击按钮时设置表格视图的表格行工厂,java,javafx,javafx-11,openjfx,Java,Javafx,Javafx 11,Openjfx,我正在用javafx编写一个表格拖放重新整理表单应用程序。在这个应用程序中,我打算通过拖放来编辑表格顺序,我已经在以前的搜索中为我的表格正式编写了拖放操作,但困扰我的是,我只想在单击按钮时设置拖放tablerowfactory 这是我的密码: ''' import java.util.function.function; 导入javafx.application.application; 导入javafx.application.Platform; 导入javafx.beans.property
import java.util.function.function;
导入javafx.application.application;
导入javafx.application.Platform;
导入javafx.beans.property.SimpleStringProperty;
导入javafx.beans.property.StringProperty;
导入javafx.beans.value.observeValue;
导入javafx.event.ActionEvent;
导入javafx.event.EventHandler;
导入javafx.event.EventType;
导入javafx.scene.scene;
导入javafx.scene.control.Button;
导入javafx.scene.control.TableColumn;
导入javafx.scene.control.TablePosition;
导入javafx.scene.control.TableRow;
导入javafx.scene.control.TableView;
导入javafx.scene.input.ClipboardContent;
导入javafx.scene.input.DataFormat;
导入javafx.scene.input.Dragboard;
导入javafx.scene.input.TransferMode;
导入javafx.scene.layout.BorderPane;
导入javafx.scene.layout.VBox;
导入javafx.stage.stage;
公共类TableViewDrawer扩展了应用程序{
私有静态最终数据格式序列化_MIME_TYPE=新数据格式(“应用程序/x-java-SERIALIZED-object”);
公共桌面视图桌面视图;
@凌驾
公共无效开始(阶段primaryStage){
tableView=新的tableView();
tableView.getColumns().add(createCol(“First Name”,Person::firstNameProperty,150));
tableView.getColumns().add(createCol(“姓氏”,Person::lastNameProperty,150));
tableView.getColumns().add(createCol(“Email”,Person::emailProperty,200));
tableView.getItems().addAll(
新人(“雅各布”、“史密斯”、“雅各布”。smith@example.com","1"),
新人(“伊莎贝拉”、“约翰逊”、“伊莎贝拉”。johnson@example.com","2"),
新人(“伊桑”、“威廉姆斯”、“伊桑”。williams@example.com","3"),
新人(“艾玛”、“琼斯”、“艾玛”。jones@example.com","4"),
新人(“迈克尔”、“布朗”、“迈克尔”。brown@example.com","5")
);
按钮顺序=新按钮(“设置顺序”);
setOnAction(新的EventHandler(){
@凌驾
公共无效句柄(ActionEvent e){
tableView.setRowFactory(电视->{
TableRow行=新TableRow();
检测到行setOnDragDetected(事件->{
如果(!row.isEmpty()){
整数索引=row.getIndex();
Dragboard db=row.startDragAndDrop(TransferMode.MOVE);
db.setDragView(行快照(null,null));
ClipboardContent cc=新的ClipboardContent();
cc.put(序列化的MIME类型,索引);
db.setContent(cc);
event.consume();
}
});
行。setOnDragOver(事件->{
Dragboard db=event.getDragboard();
if(db.hasContent(序列化的MIME类型)){
if(row.getIndex()!=((整数)db.getContent(序列化的MIME类型)).intValue()){
事件.acceptTransferModes(TransferMode.COPY\u或\u MOVE);
event.consume();
}
}
});
行。SetOnDragDrop(事件->{
Dragboard db=event.getDragboard();
if(db.hasContent(序列化的MIME类型)){
int draggedIndex=(整数)db.getContent(序列化的MIME类型);
Person draggedPerson=tableView.getItems().remove(draggedIndex);
整数下降指数;
if(row.isEmpty()){
dropIndex=tableView.getItems().size();
}否则{
dropIndex=row.getIndex();
}
tableView.getItems().add(dropIndex,draggedPerson);
事件。setDropCompleted(true);
tableView.getSelectionModel().select(dropIndex);
event.consume();
}
});
返回行;
});
}
});
order.pressedProperty().addListener((obs、WAS聚焦、ISNOWSPRESSED)->{
如果(现在按下){
Platform.runLater(()->tableView.setRowFactory(tv->{
TableRow行=新TableRow();
检测到行setOnDragDetected(事件->{
如果(!row.isEmpty()){
整数索引=row.getIndex();
Dragboard db=row.startDragAndDrop(TransferMode.MOVE);
db.setDragView(行快照(null,null));
ClipboardContent cc=新的ClipboardContent();
cc.put(序列化的MIME类型,索引);
db.setContent(cc);
event.consume();
}
});
行。setOnDragOver(事件->{
Dragboard db=event.getDragboard();
if(db.hasContent(序列化的MIME类型)){
if(row.getIndex()!=((整数)db.getContent(序列化的MIME类型)).intValue()){
事件.acceptTransferModes(TransferMode.COPY\u或\u MOVE);
event.consume();
}
}
});
行。SetOnDragDrop(事件->{
Dragboard db=event.getDragboard();
if(db.hasContent(序列化的MIME类型)){
int draggedIndex=(整数)db.getContent(序列化的MIME类型);
Person draggedPerson=tableView.getItems().remove(draggedIndex);
整数下降指数;
if(row.isEmpty()){
dropIndex=tableView.getItems().size(
import java.util.function.Function;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.event.EventType;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TablePosition;
import javafx.scene.control.TableRow;
import javafx.scene.control.TableView;
import javafx.scene.input.ClipboardContent;
import javafx.scene.input.DataFormat;
import javafx.scene.input.Dragboard;
import javafx.scene.input.TransferMode;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class TableViewDragRows extends Application {
private static final DataFormat SERIALIZED_MIME_TYPE = new DataFormat("application/x-java-serialized-object");
public TableView<Person> tableView;
@Override
public void start(Stage primaryStage) {
tableView = new TableView<>();
tableView.getColumns().add(createCol("First Name", Person::firstNameProperty, 150));
tableView.getColumns().add(createCol("Last Name", Person::lastNameProperty, 150));
tableView.getColumns().add(createCol("Email", Person::emailProperty, 200));
tableView.getItems().addAll(
new Person("Jacob", "Smith", "jacob.smith@example.com","1"),
new Person("Isabella", "Johnson", "isabella.johnson@example.com","2"),
new Person("Ethan", "Williams", "ethan.williams@example.com","3"),
new Person("Emma", "Jones", "emma.jones@example.com","4"),
new Person("Michael", "Brown", "michael.brown@example.com","5")
);
Button order = new Button("Set Order");
order.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent e) {
tableView.setRowFactory(tv -> {
TableRow<Person> row = new TableRow<>();
row.setOnDragDetected(event -> {
if (! row.isEmpty()) {
Integer index = row.getIndex();
Dragboard db = row.startDragAndDrop(TransferMode.MOVE);
db.setDragView(row.snapshot(null, null));
ClipboardContent cc = new ClipboardContent();
cc.put(SERIALIZED_MIME_TYPE, index);
db.setContent(cc);
event.consume();
}
});
row.setOnDragOver(event -> {
Dragboard db = event.getDragboard();
if (db.hasContent(SERIALIZED_MIME_TYPE)) {
if (row.getIndex() != ((Integer)db.getContent(SERIALIZED_MIME_TYPE)).intValue()) {
event.acceptTransferModes(TransferMode.COPY_OR_MOVE);
event.consume();
}
}
});
row.setOnDragDropped(event -> {
Dragboard db = event.getDragboard();
if (db.hasContent(SERIALIZED_MIME_TYPE)) {
int draggedIndex = (Integer) db.getContent(SERIALIZED_MIME_TYPE);
Person draggedPerson = tableView.getItems().remove(draggedIndex);
int dropIndex ;
if (row.isEmpty()) {
dropIndex = tableView.getItems().size() ;
} else {
dropIndex = row.getIndex();
}
tableView.getItems().add(dropIndex, draggedPerson);
event.setDropCompleted(true);
tableView.getSelectionModel().select(dropIndex);
event.consume();
}
});
return row ;
});
}
});
order.pressedProperty().addListener((obs, wasFocused, isNowPressed) -> {
if (isNowPressed) {
Platform.runLater(() -> tableView.setRowFactory(tv -> {
TableRow<Person> row = new TableRow<>();
row.setOnDragDetected(event -> {
if (! row.isEmpty()) {
Integer index = row.getIndex();
Dragboard db = row.startDragAndDrop(TransferMode.MOVE);
db.setDragView(row.snapshot(null, null));
ClipboardContent cc = new ClipboardContent();
cc.put(SERIALIZED_MIME_TYPE, index);
db.setContent(cc);
event.consume();
}
});
row.setOnDragOver(event -> {
Dragboard db = event.getDragboard();
if (db.hasContent(SERIALIZED_MIME_TYPE)) {
if (row.getIndex() != ((Integer)db.getContent(SERIALIZED_MIME_TYPE)).intValue()) {
event.acceptTransferModes(TransferMode.COPY_OR_MOVE);
event.consume();
}
}
});
row.setOnDragDropped(event -> {
Dragboard db = event.getDragboard();
if (db.hasContent(SERIALIZED_MIME_TYPE)) {
int draggedIndex = (Integer) db.getContent(SERIALIZED_MIME_TYPE);
Person draggedPerson = tableView.getItems().remove(draggedIndex);
int dropIndex ;
if (row.isEmpty()) {
dropIndex = tableView.getItems().size() ;
} else {
dropIndex = row.getIndex();
}
tableView.getItems().add(dropIndex, draggedPerson);
event.setDropCompleted(true);
tableView.getSelectionModel().select(dropIndex);
event.consume();
}
});
return row ;
}));
}
});
Button button = new Button("Get Order");
button.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent e) {
for(Person p: tableView.getItems()){
System.out.println(p.getid());
} }
});
VBox vbox = new VBox();
vbox.getChildren().addAll(tableView, button, order);
Scene scene = new Scene(new BorderPane(vbox), 600, 400);
primaryStage.setScene(scene);
primaryStage.show();
}
private TableColumn<Person, String> createCol(String title,
Function<Person, ObservableValue<String>> mapper, double size) {
TableColumn<Person, String> col = new TableColumn<>(title);
col.setCellValueFactory(cellData -> mapper.apply(cellData.getValue()));
col.setPrefWidth(size);
return col ;
}
public class Person {
private final StringProperty firstName = new SimpleStringProperty(this, "firstName");
private final StringProperty lastName = new SimpleStringProperty(this, "lastName");
private final StringProperty email = new SimpleStringProperty(this, "email");
private final StringProperty id = new SimpleStringProperty(this, "id");
public Person(String firstName, String lastName, String email, String id) {
this.firstName.set(firstName);
this.lastName.set(lastName);
this.email.set(email);
this.id.set(id);
}
public final StringProperty idProperty() {
return this.id;
}
public final String getid() {
return this.idProperty().get();
}
public final StringProperty firstNameProperty() {
return this.firstName;
}
public final String getFirstName() {
return this.firstNameProperty().get();
}
public final void setFirstName(final String firstName) {
this.firstNameProperty().set(firstName);
}
public final StringProperty lastNameProperty() {
return this.lastName;
}
public final String getLastName() {
return this.lastNameProperty().get();
}
public final void setLastName(final String lastName) {
this.lastNameProperty().set(lastName);
}
public final StringProperty emailProperty() {
return this.email;
}
public final String getEmail() {
return this.emailProperty().get();
}
public final void setEmail(final String email) {
this.emailProperty().set(email);
}
}
public static void main(String[] args) {
launch(args);
}
}