How to stop JFace combo box from re sizing on a window re size?

前端 未结 1 1733
自闭症患者
自闭症患者 2020-12-22 06:36

I have a Java Eclipse RCP program in which I have a a long string inside a JFace combobox. Now when I am in the same view,The combobox attaches a scroll over it to show the

相关标签:
1条回答
  • 2020-12-22 07:17

    If you are using GridLayout and GridData as your layout you can specify a value in the widthHint field to suggest the width for the Combo.

    GridData data = new GridData(SWT.FILL, SWT.CENTER, true, false);
    
    data.widthHint = 100;
    
    preferredResourceCombo.setLayoutData(data);
    

    Using a fixed value like this might cause problems if the user uses a large font. So an alternative way of calculating the width is to use Dialog.convertWidthInCharsToPixels:

    GC gc = new GC(control);
    gc.setFont(control.getFont());
    FontMetrics fontMetrics = gc.getFontMetrics();
    gc.dispose();
    
    data.widthHint = Dialog.convertWidthInCharsToPixels(fontMetrics, charCount);
    

    If your code is in a Dialog you can simplify this to just:

    data.widthHint = convertWidthInCharsToPixels(charCount);
    
    0 讨论(0)
提交回复
热议问题