tar*_*ion 4 java javafx javafx-2
我有一个JavaFX 2.2 FlowPane,并希望它的孩子可以根据需要改变他们的订单.
我发现
Collections.sort( getChildren(), new Comparator(){...} );
Run Code Online (Sandbox Code Playgroud)
导致异常
java.lang.IllegalArgumentException: Children: duplicate children added: parent = ...
Run Code Online (Sandbox Code Playgroud)
I also tried to sort using the child Nodes toFront() method (as suggested here How to change order of children in JavaFX), but this does not seem to influence the list order.
Is there anything else I could try or are children in a scene graph unmutable regarding their order?
更新:勘误表
由于JavaFX 2.x中的错误,此答案的示例解决方案在JavaFX 2.x中不起作用.感谢assylias识别JavaFX 2.x错误:
当我运行你的程序时,列表不会更新,直到我水平调整窗口大小(垂直不做任何事情) - jdk 7u25/Windows 7 x64.
该错误已在Java 8中修复,因此如果您使用Java 8,示例解决方案将起作用.
解决方案策略
代码片段
ObservableList<Node> workingCollection = FXCollections.observableArrayList(
flow.getChildren()
);
Collections.sort(workingCollection, new NodeComparator());
flow.getChildren().setAll(workingCollection);
Run Code Online (Sandbox Code Playgroud)
样本输出
排序前的样本输出:

排序后的样本输出:

样品申请
import javafx.application.Application;
import javafx.collections.*;
import javafx.event.*;
import javafx.geometry.Pos;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;
import java.util.*;
public class FlowGo extends Application {
private final ObservableList<Label> labels = FXCollections.observableArrayList(
createLabel("oranges"),
createLabel("apples"),
createLabel("pears"),
createLabel("peaches"),
createLabel("bananas")
);
@Override public void start(Stage stage) {
FlowPane flow = createFlow();
VBox layout = new VBox(10);
layout.getChildren().setAll(
flow,
createActionButtons(flow)
);
layout.setAlignment(Pos.CENTER);
layout.setStyle("-fx-padding: 10px; -fx-background-color: cornsilk;");
stage.setScene(new Scene(layout));
stage.show();
}
private HBox createActionButtons(final FlowPane flow) {
Button sort = new Button("Sort");
sort.setOnAction(new EventHandler<ActionEvent>() {
@Override public void handle(ActionEvent actionEvent) {
ObservableList<Node> workingCollection = FXCollections.observableArrayList(
flow.getChildren()
);
Collections.sort(workingCollection, new NodeComparator());
flow.getChildren().setAll(workingCollection);
}
});
Button reset = new Button("Reset");
reset.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {
flow.getChildren().setAll(labels);
}
});
HBox buttonBar = new HBox(10);
buttonBar.getChildren().addAll(
sort,
reset
);
buttonBar.setAlignment(Pos.CENTER);
return buttonBar;
}
private FlowPane createFlow() {
FlowPane flow = new FlowPane();
flow.setHgap(10);
flow.setVgap(10);
flow.getChildren().setAll(labels);
flow.setAlignment(Pos.CENTER);
flow.setStyle("-fx-padding: 10px; -fx-background-color: lightblue; -fx-font-size: 16px;");
return flow;
}
private Label createLabel(String text) {
Label label = new Label(text);
label.setUserData(text);
return label;
}
public static void main(String[] args) { launch(args); }
private static class NodeComparator implements Comparator<Node> {
@Override
public int compare(Node o1, Node o2) {
String s1 = (String) o1.getUserData();
String s2 = (String) o2.getUserData();
return s1.compareTo(s2);
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4081 次 |
| 最近记录: |