我在SWT TableEditor
中注入了 Table
,左对齐(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()
的所有组合都不会影响编辑的新职位。
知道什么可能会有所帮助?
以下是视觉演示:
这是一个片段 (更新:我突出显示了@Baz's answer的补丁编辑,这些编辑是为了让事情按预期工作!):</ p>
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;
}
}
};
}
答案 0 :(得分:1)
您需要更新TableEditor
的宽度,然后重新布局:
editor.minimumWidth = composite.computeSize(SWT.DEFAULT, SWT.DEFAULT).x;
editor.layout();
这将计算所需的宽度,设置它并在父级上调用.layout()
。