2016-06-14 3 views
0

dropzone에서 열을 끌어서 여러 필드에서 기본 Javafx 테이블 정렬을 할 수 있습니까?여러 열에서 Javafx 테이블 정렬

사용자가 다른 열을 정렬하려면 하나 이상의 열을 선택해야합니다. 이 응용 프로그램은 JavaFX로 완전히 Java8로 작성됩니다. 내가 지금 사용

소스 코드는 다음과 같습니다

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); 
    } 
} 

답변

3

는 내가있는 TableView의 API를 확인하고 자바 FX 테이블이 디폴트 구현을 가지고 있음을 발견했다. Shift 키를 사용하여 열을 클릭하기 만하면됩니다.

+0

이것은 바퀴를 다시 발명하지 못하게했습니다. – NonlinearFruit