有办法设置 JFace 的 ComboViewer 的长度吗?

Have a way to set the length of JFace's ComboViewer?

有一种情况,我有一个 ComboViewer,它在不同 time.Thus 中有不同的内容,有时需要重新布局 ComboViewer 以便它可以显示全部内容。有没有办法在开始时定义 ComboViewer 的最大长度?如果你能给我一些想法,我将不胜感激。

下面的代码是我根据rudiger.It在windows7中运行良好的demo,而在"abcedfgabcedfg"中切换到comboviewer的长度仍然很短linux .

    public class ComboViewerTest {

  /**
   * @param args
   */
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Comboviewer Test");
    shell.setLayout(new GridLayout(1, false));


    Composite composite = new Composite(shell,SWT.None);

    composite.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false, 1, 1));
    composite.setLayout(new GridLayout(2, true));
    final String[] txtStrings = {"a","abc"};
    final String[] txtStrings2 = { "abcedfg", "abcedfgabcedfg"};

    Label label = new Label(composite, SWT.NONE);
    label.setLayoutData(new GridData(SWT.LEFT, SWT.FILL, false, true, 1, 1));
    label.setText("comboviewer");

    final ComboViewer comboViewer = new ComboViewer(composite,SWT.NONE | SWT.READ_ONLY);
    Combo combo = comboViewer.getCombo();
    combo.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, true, 1, 1));
    comboViewer.setContentProvider(new ArrayContentProvider());
    comboViewer.setInput(txtStrings);
    comboViewer.setLabelProvider(new LabelProvider() {

      @Override
      public String getText(Object element) {
        return super.getText(element);
      }

    });
    Composite composite2 = new Composite(shell,SWT.None);
    composite2.setLayout(new GridLayout(2, true));

    Button btnNewButton = new Button(composite2, SWT.RADIO);
    btnNewButton.setBounds(0, 0, 84, 29);
    btnNewButton.setText("change the comboviewr");
    btnNewButton.addSelectionListener(new SelectionAdapter() {
      @Override
      public void widgetSelected(SelectionEvent e) {
        comboViewer.setInput(txtStrings2);
        comboViewer.getCombo().select(0);
      }});
    Button btnNewButton2 = new Button(composite2, SWT.RADIO);
    btnNewButton2.setBounds(0, 0, 84, 29);
    btnNewButton2.setText("reset");
    btnNewButton2.addSelectionListener(new SelectionAdapter() {
      @Override
      public void widgetSelected(SelectionEvent e) {
        comboViewer.setInput(txtStrings);
        comboViewer.getCombo().select(0);
      }});

    comboViewer.getCombo().select(0);

    setComboViewerLength(comboViewer);
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }

  private static void setComboViewerLength(ComboViewer comboViewer) {
    String string = "abcedfgabcedfg";
    Combo control = comboViewer.getCombo();
    GC gc = new GC( control );
    Point stringExtent = gc.stringExtent( string );
    gc.dispose();
    Rectangle bounds = control.computeTrim( 0, 0, stringExtent.x, stringExtent.y );
    GridData gridData = new GridData();
    gridData.widthHint = bounds.width;
    control.setLayoutData( gridData );
  }
}

如果我对你的问题理解正确,问题是双重的。

1.确定完整显示一定长度的字符串所需的大小

ComboViewer 使用 SWT 的 ComboCCombo 小部件来显示其数据。给定要显示的字符串,您可以按如下方式确定组合的必要大小(以像素为单位):

String string = "abc";
ComboViewer comboViewer = ...;
Combo control = comboViewer.getCombo();
GC gc = new GC( control );
Point stringExtent = gc.stringExtent( string );
gc.dispose();
Rectangle bounds = control.computeTrim( 0, 0, stringExtent.x, stringExtent.y );

返回的 bounds 描述了一个矩形,如果组合的边界设置为该矩形,则该矩形足够大以显示字符串和装饰(drop-down 按钮、边框等)。

2。配置布局以使用上面计算的组合框大小

如何控制小部件的大小取决于您使用的布局管理器。使用的布局管理器设置在组合的父级上。

一些(但不是全部)布局允许提供提示或明确设置控件的所需宽度和高度。

例如,如果您使用 GridLayout,请为组合定义 GridData 以控制其大小。

Combo control = comboViewer.getCombo();
GridData gridData = new GridData();
gridData.widthHint = bounds.width;
control.setLayoutData( gridData );

有关布局的更多信息和 SWT 标准布局的说明,我建议阅读 Understanding Layouts in SWT 文章。