【问题标题】:Re-align ragged-left SWT TableEditor after content updated内容更新后重新对齐左对齐 SWT TableEditor
【发布时间】:2023-04-09 18:15:02
【问题描述】:

我在 SWT Table 中注入了一个 TableEditor,左对齐不规则 (SWT.RIGHT)。 比如:

final TableEditor editor = new TableEditor(table);
editor.horizontalAlignment = SWT.RIGHT;
editor.setEditor(toDisplay, item, 1);

toDisplay 是具有两个孩子的 2x1 GridLayout 组合:

  • 动态Label
  • Button

我的问题是:当动态标签更新(变得更宽)时,toDisplay 包装器组合移动到表格列之外,而不是重新对齐为一个好衣衫褴褛的左男孩[附上此行为的屏幕截图:标签没有文字,而是包含一个图标]。

两个孩子的布局数据通过以下方式设置:

GridDataFactory.fillDefaults().create()

该按钮有一个SelectionListener,它会更新文本,然后布置父复合材料。

看来.pack().layout()的所有组合都不会影响编辑的新职位。

知道什么会有所帮助吗?


  • 这是一个视觉演示:

  • 这是一个 sn-p (更新:我突出显示来自 @Baz's answer 的补丁编辑,这些编辑是使事情按预期工作所需的!):

package com.stackoverflow.swt.snippets;

import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.TableEditor;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;

/**
 * Updates the width of a <code>Composite<code> inside <code>TableEditor</code>:
 * alignment of the editor is not proper after update,
 * the position of the composite shifts out on the right.
 *
 * @see https://stackoverflow.com/questions/32645683/re-align-ragged-left-swt-tableeditor-after-content-updated
 */
public class SWTDynamicRaggedLeftTableEditorSnippet {

    public static void main(String[] args) {
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setText(SWTDynamicRaggedLeftTableEditorSnippet.class.getSimpleName());

        // create table
        Table table = new Table(shell, SWT.BORDER | SWT.MULTI);
        table.setLayout(new GridLayout(1, true));
        table.setLinesVisible(true);
        table.addListener(SWT.MeasureItem, new Listener() {
            @Override
            public void handleEvent(Event event) {
                event.height = 40;
            }
        });

        new TableItem(table, SWT.NONE).setText("Tizio");
        new TableItem(table, SWT.NONE).setText("Caio");
        new TableItem(table, SWT.NONE).setText("Sempronio");

        // add editor
        for (TableItem item : table.getItems()) {
+++         final TableEditor editor = new TableEditor(table);

            /*
             * Parent Composite:
             * +----------------------------+
             * |+-------+ +----------------+|
             * || Label | |    Button      ||
             * |+-------+ +----------------+|
             * +----------------------------+
             */
            final Composite composite = new Composite(table, SWT.NONE);
            composite.setLayout(new GridLayout(2, false));
            composite.setLayoutData(GridDataFactory.fillDefaults().create());

            // 1st child: Label (empty now)
            final Label label = new Label(composite, SWT.NONE);
            label.setLayoutData(GridDataFactory.fillDefaults().create());

            // 2nd child: the Button
            Button button = new Button(composite, SWT.PUSH);
            button.setText(ButtonText.OFF.name());
            button.setLayoutData(GridDataFactory.fillDefaults().create());
            button.addSelectionListener(new SelectionAdapter() {
                @Override
                public void widgetSelected(SelectionEvent e) {
                    // update Button
                    label.setText("And suddenly pops out this wonderful sentence");
                    button.setText(((ButtonText) ButtonText.valueOf(button.getText()).toggle()).name());
                    // uncover the Label:
---                 composite.layout();
+++                 editor.minimumWidth = composite.computeSize(SWT.DEFAULT, SWT.DEFAULT).x;
+++                 editor.layout();
                }
            });
            composite.pack();

            /*
             * The ragged-left Table Editor
             */
---         final TableEditor editor = new TableEditor(table);
            editor.minimumWidth = button.getSize().x;
            editor.horizontalAlignment = SWT.RIGHT;
            editor.grabVertical = true;
            editor.grabHorizontal = false;
            editor.setEditor(composite, item, 0);
            editor.layout();
        }

        // size
        Rectangle clientArea = shell.getClientArea();
        table.setBounds(clientArea.x, clientArea.y, 500, 300);

        // read & dispatch
        shell.pack();
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) display.sleep();
        }
        display.dispose();
    }
    //  (support stuff) :

    interface Toggleable {
        Toggleable toggle();
    }

    static enum ButtonText implements Toggleable {
        ON {
            @Override
            public ButtonText toggle() {
                return OFF;
            }
        },
        OFF {
            @Override
            public ButtonText toggle() {
                return ON;
            }
        }
    };
}

【问题讨论】:

  • 你能发个SSCCE吗?
  • 当然,here 是它。 (我也更新了问题)
  • 好的,那么想要的结果应该是什么样的?
  • @Baz:Composite 应该重新对齐,即它在TableItem 中的 x 偏移量应该变小,以便它的右边缘可以再次与列的右边缘重合。 (我正在尝试与听众一起玩,在我的脑海中摆弄一些问题,比如......在哪里附加听众?在编辑器上?按钮?复合?哪个事件监听器最好?等等 :)

标签: java layout swt


【解决方案1】:

你需要更新TableEditor的宽度,然后重新布局:

editor.minimumWidth = composite.computeSize(SWT.DEFAULT, SWT.DEFAULT).x; 
editor.layout();

这将计算所需的宽度,设置它并在父级上调用.layout()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-16
    • 1970-01-01
    • 1970-01-01
    • 2019-11-12
    • 2018-12-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多