我正在尝试在SWT中的文本字段中显示历史记录。
历史记录生成一个具有两个复合材料的新shell。第一个显示标题部分,其中包含标签“历史”和下面的水平线。第二个是页脚部分,显示实际数据。这是有效的,但水平线在整个可用的水平空间内没有扩展(即:“填充”)(即:直到复合“结束”)。
public void mouseDown(final MouseEvent e) {
findSegmentText.setText("");
if(0 == lastSegmentIds.size()) return;
if(disableToolTipsButton.getSelection()) return; // context help is not desired
if(null != popupShell) popupShell.dispose();
popupShell = new Shell(Display.getDefault(), SWT.BORDER | SWT.ON_TOP | SWT.MODELESS);
//popupShell.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
popupShell.setLayout(createNoMarginLayout(1, false));
/* Header */
compositeSearchSegments1 = new Composite(popupShell, SWT.NONE);
compositeSearchSegments1.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
compositeSearchSegments1.setLayout(createNoMarginLayout(1, false));
/* Footer */
compositeSearchSegments2 = new Composite(popupShell, SWT.NONE);
compositeSearchSegments2.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, true));
compositeSearchSegments2.setLayout(createNoMarginLayout(2, false));
Label history = new Label(compositeSearchSegments1, SWT.NONE);
history.setText("History");
Label line1 = new Label(compositeSearchSegments1, SWT.SEPARATOR | SWT.SHADOW_OUT | SWT.HORIZONTAL);
for(int i = lastSegmentIds.size()-1 ; i>=0;i--) { // iterate backwards, higher indexes have newer selected links
final String lastSegmentId = lastSegmentIds.get(i);
l.setText(lastSegmentId);
Button b = new Button(compositeSearchSegments2, SWT.NONE);
b.setVisible(true);
b.setText(">>");
b.setSize(10,10);
b.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
findSegmentText.setText(lastSegmentId);
searchAndDisplayLink(Long.decode(findSegmentText.getText()));
}
});
}
Point point = optionsComposite.toDisplay(e.x + optionsComposite.getBounds().x +findSegmentText.getLocation().x, e.y + optionsComposite.getBounds().y+findSegmentText.getLocation().y );
popupShell.setLocation(point.x-125, point.y-10);
popupShell.pack();
popupShell.open();
findSegmentText.setFocus();
}
产生结果:
我想扩展该行以填充可用的水平空间。我该怎么办?谢谢。
答案 0 :(得分:3)
由于您使用的是GridLayout
,因此向分隔符GridData
添加Label
可以解决问题:
public static void main(String[] args)
{
Display display = new Display();
Shell shell = new Shell(display);
shell.setText("StackOverflow");
shell.setLayout(new GridLayout(1, false));
Label first = new Label(shell, SWT.NONE);
first.setText("short");
Label separator = new Label(shell, SWT.SEPARATOR | SWT.SHADOW_OUT | SWT.HORIZONTAL);
separator.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
Label second = new Label(shell, SWT.NONE);
second.setText("looooooooooooooooooooooooooooooooooooong");
shell.pack();
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
看起来像这样: