我有一个以随机顺序填充复合材料的 GridLayout。现在我正在对填充列表/集合中的 GridLayout 的组合进行排序,并希望像我的列表/集合中的排序结果一样对它们进行排序。我试图通过将它们再次分配给他们的父母来做到这一点,以便他们按正确的顺序排列,但由于某种原因什么也没发生。然后我尝试将它们缓存在您看不到的 Composite 中,然后使用与第一次尝试相同的方法将它们带回父级。根本没有变化。有人有指针吗?我按日期订购,以防万一/所以想知道。
这就是我的网格的样子,现在我想在 arrayList() 中对它们进行排序;

你正在寻找的方法Control#moveAbove(Control control),并Control#moveBelow(Control control)进行重新排序:
private static List<Label> labels = new ArrayList<Label>();
private static List<Color> colors = new ArrayList<Color>();
public static void main(String[] args)
{
Display display = new Display();
final Shell shell = new Shell(display);
shell.setText("Stackoverflow");
shell.setLayout(new RowLayout(SWT.VERTICAL));
colors.add(display.getSystemColor(SWT.COLOR_BLUE));
colors.add(display.getSystemColor(SWT.COLOR_CYAN));
colors.add(display.getSystemColor(SWT.COLOR_GREEN));
colors.add(display.getSystemColor(SWT.COLOR_YELLOW));
colors.add(display.getSystemColor(SWT.COLOR_RED));
for (int i = 0; i < 5; i++)
{
Label label = new Label(shell, SWT.BORDER);
label.setText("Button " + i);
label.setBackground(colors.get(i));
labels.add(label);
}
Button sortButton = new Button(shell, SWT.TOGGLE);
sortButton.setText("Sort");
sortButton.addListener(SWT.Selection, new Listener()
{
@Override
public void handleEvent(Event e)
{
Button source = (Button) e.widget;
final boolean asc = source.getSelection();
Label oldFirst = labels.get(0);
Collections.sort(labels, new Comparator<Label>()
{
@Override
public int compare(Label o1, Label o2)
{
int result = o1.getText().compareTo(o2.getText());
if (asc)
result = -result;
return result;
}
});
Label label = labels.get(0);
label.moveAbove(oldFirst);
for (int i = 1; i < labels.size(); i++)
{
labels.get(i).moveBelow(labels.get(i - 1));
}
shell.layout();
}
});
shell.pack();
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
Run Code Online (Sandbox Code Playgroud)
启动后:

按下按钮后:

| 归档时间: |
|
| 查看次数: |
1033 次 |
| 最近记录: |