JavaFX表中的按函数分组

JavaFX表中的按函数分组,javafx,javafx-8,Javafx,Javafx 8,我们正在将swt/nattable应用程序迁移到新的java8和Javafx8 一切都进展顺利,但无法找到nattable的一个功能。我们正在搜索的功能是“分组依据”功能。因此,我们需要一个库/实现来拖放标签上的列,并将这些列作为表中的分组依据。有人能帮我们吗 分组功能在Nattable中的工作方式 有两个主要组件—表本身和不同列的dropzone。当用户将列拖动到dropzone时,表会将该列的值分组在一起(就像SQL中的Group by)。之后,表格将按操作显示分组的结果。我只能将表格中的“

我们正在将swt/nattable应用程序迁移到新的java8和Javafx8

一切都进展顺利,但无法找到nattable的一个功能。我们正在搜索的功能是“分组依据”功能。因此,我们需要一个库/实现来拖放标签上的列,并将这些列作为表中的分组依据。有人能帮我们吗

分组功能在Nattable中的工作方式

有两个主要组件—表本身和不同列的dropzone。当用户将列拖动到dropzone时,表会将该列的值分组在一起(就像SQL中的Group by)。之后,表格将按操作显示分组的结果。

我只能将表格中的“分组”解释为针对要分组的字段执行稳定的项目排序

您只需在此处设置拖放,这样,当您从表格标题拖放到标签上时,表格的项目将按列表示的属性排序。唯一棘手的是,没有简单的方法访问表标题:最简单的方法不是设置列文本,而是创建包含文本的标签,并将列图形设置为该标签。这样,就可以用标签注册拖动处理程序

以下是SSCCE:

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);
    }
}
import java.util.Collections;
导入java.util.Comparator;
导入java.util.function.function;
导入javafx.application.application;
导入javafx.beans.property.SimpleObject属性;
导入javafx.geometry.Insets;
导入javafx.geometry.Pos;
导入javafx.scene.scene;
导入javafx.scene.control.Label;
导入javafx.scene.control.TableColumn;
导入javafx.scene.control.TableView;
导入javafx.scene.input.ClipboardContent;
导入javafx.scene.input.Dragboard;
导入javafx.scene.input.TransferMode;
导入javafx.scene.layout.BorderPane;
导入javafx.stage.stage;
公共类GroupByTable扩展了应用程序{
公共枚举颜色{绿色、蓝色、红色}
公共枚举形状{矩形、圆形、三角形}
公共枚举大小{小、中、大}
自有品牌groupByLabel;
私有比较器分组比较器;
@凌驾
公共无效开始(阶段primaryStage){
TableView table=新TableView();
table.getColumns().add(column(“Size”,Item::getSize));
table.getColumns().add(column(“Color”,Item::getColor));
table.getColumns().add(column(“Shape”,Item::getShape));
groupByLabel=新标签(“分组”);
groupByLabel.setOnDragOver(e->{
if(groupingComparator!=null&“grouping”.equals(例如getDragboard().getString())){
e、 acceptTransferModes(TransferMode.COPY);
}
});
groupByLabel.SetOnDragDrop(e->{
if(groupingComparator!=null&“grouping”.equals(例如getDragboard().getString())){
table.getItems().sort(groupingComparator);
e、 setDropCompleted(真);
}
});
对于(颜色:Color.values()){
对于(大小:Size.values()){
对于(形状:Shape.values()){
table.getItems().add(新项(颜色、形状、大小));
}
}
}
Collections.shuffle(table.getItems());
BorderPane根=新的BorderPane(表);
BorderPane.setAlignment(groupByLabel,位置中心);
BorderPane.setMargin(groupByLabel,新插图(20));
root.setTop(groupByLabel);
场景=新场景(root,600600);
初级阶段。场景(场景);
primaryStage.show();
}
私有表列(字符串标题、函数属性){
TableColumn col=新TableColumn();
col.setCellValueFactory(cellData->new SimpleObject属性(property.apply(cellData.getValue()));
标签图形=新标签(标题);
图.已检测到setOnDragDetected(e->{
groupingComparator=比较器。比较(属性);
拖板拖板=graphic.startdragandrop(TransferMode.COPY);
ClipboardContent cc=新的ClipboardContent();
cc.putString(“分组”);
拖板。设置内容(cc);
});
图形。setOnDragDone(e->{
groupingComparator=null;
});
col.setGraphic(图形);
返回列;
}
公共静态类项{
私人最终颜色;
私人最终形状;
私人最终尺寸;
公共物品(颜色、形状、大小){
超级();
这个颜色=颜色;
这个形状=形状;
这个。大小=大小;
}
公共颜色getColor(){
返回颜色;
}
公共形状getShape(){
返回形状;
}
公共大小getSize(){
返回大小;
}
@凌驾
公共字符串toString(){
返回字符串。格式(“%s%s%s”,大小、颜色、形状);
}
}
公共静态void main(字符串[]args){
发射(args);
}
}
初始屏幕截图:

将“颜色”表列标题拖动到“分组”后:

我只能将表中的“分组”解释为针对要分组的字段执行稳定的项目排序

您只需在此处设置拖放,这样,当您从表格标题拖放到标签上时,表格的项目将按列表示的属性排序。唯一棘手的是,没有简单的方法访问表标题:最简单的方法不是设置列文本,而是创建包含文本的标签,并将列图形设置为该标签。这样,就可以用标签注册拖动处理程序

以下是SSCCE:

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);
    }
}
import java.util.Collections;
导入java.util.Comparator;
感应电动机