在多列上对Javafx表进行排序 [英] Sort Javafx table on multiple columns

查看:439
本文介绍了在多列上对Javafx表进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

默认的Javafx表可以通过拖放dropzone上的列对多个字段进行排序吗?

Can a default Javafx table sort on multiple fields by dragging the columns on a dropzone?

我的用户需要选择一列或多列来对不同的列进行排序。该应用程序完全用Java8和JavaFX编写。

My user need to select one or multiple columns to sort on different columns. The application is fully written in Java8 with JavaFX.

我现在使用的源代码是:

The source code that I now use is:

import java.util.Collections;
import java.util.Comparator;
import java.util.function.Function;

import javafx.application.Application;
import javafx.beans.property.SimpleObjectProperty;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.input.ClipboardContent;
import javafx.scene.input.Dragboard;
import javafx.scene.input.TransferMode;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

public class GroupByTable extends Application {

    public enum Color { GREEN, BLUE, RED }
    public enum Shape { RECTANGLE, CIRCLE, TRIANGLE }
    public enum Size { SMALL, MEDIUM, LARGE }


    private Label groupByLabel;
    private Comparator<Item> groupingComparator ;


    @Override
    public void start(Stage primaryStage) {
        TableView<Item> table = new TableView<>();

        table.getColumns().add(column("Size", Item::getSize));
        table.getColumns().add(column("Color", Item::getColor));
        table.getColumns().add(column("Shape", Item::getShape));

        groupByLabel = new Label("Grouping");

        groupByLabel.setOnDragOver(e -> {
            if (groupingComparator != null && "grouping".equals(e.getDragboard().getString())) {
                e.acceptTransferModes(TransferMode.COPY);
            }
        });

        groupByLabel.setOnDragDropped(e -> {
            if (groupingComparator != null && "grouping".equals(e.getDragboard().getString())) {
                table.getItems().sort(groupingComparator);
                e.setDropCompleted(true);
            }
        });

        for (Color color : Color.values()) {
            for (Size size : Size.values()) {
                for (Shape shape : Shape.values()) {
                    table.getItems().add(new Item(color, shape, size));
                }
            }
        }

        Collections.shuffle(table.getItems());

        BorderPane root = new BorderPane(table);
        BorderPane.setAlignment(groupByLabel, Pos.CENTER);
        BorderPane.setMargin(groupByLabel, new Insets(20));

        root.setTop(groupByLabel);

        Scene scene = new Scene(root, 600, 600);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    private <T extends Comparable<T>> TableColumn<Item,T> column(String title, Function<Item,T> property) {
        TableColumn<Item,T> col = new TableColumn<>();
        col.setCellValueFactory(cellData -> new SimpleObjectProperty<>(property.apply(cellData.getValue())));

        Label graphic = new Label(title);
        graphic.setOnDragDetected(e -> {
            groupingComparator = Comparator.comparing(property);
            Dragboard dragboard = graphic.startDragAndDrop(TransferMode.COPY);
            ClipboardContent cc = new ClipboardContent();
            cc.putString("grouping");
            dragboard.setContent(cc);
        });
        graphic.setOnDragDone(e -> {
            groupingComparator = null ;
        });

        col.setGraphic(graphic);

        return col ;
    }

    public static class Item {
        private final Color color ;
        private final Shape shape ;
        private final Size size ;
        public Item(Color color, Shape shape, Size size) {
            super();
            this.color = color;
            this.shape = shape;
            this.size = size;
        }
        public Color getColor() {
            return color;
        }
        public Shape getShape() {
            return shape;
        }
        public Size getSize() {
            return size;
        }

        @Override
        public String toString() {
            return String.format("%s %s %s", size, color, shape);
        }
    }

    public static void main(String[] args) {
        launch(args);
    }
}


推荐答案

我查看TableView API,发现JavaFX表确实有这个默认实现。只需使用shift键点击列。

I view the TableView API and found that the JavaFX table does have a default implementation of this. Just click on the columns using the shift key.

这篇关于在多列上对Javafx表进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆