1

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 full name. but as soon as I re size the window of the application, the combobox stretches itself to accommodate the lengthy string.

How do i make the combobox stay the same size. Like the size of it should remain fixed even after I resize the window. Here are two screen shots to demonstrate the issue.

P.S. I am using a comboViewer and inside it a comboBox.

desired behaviour

enter image description here

Thanks in advance.

Saras Arya
  • 3,022
  • 8
  • 41
  • 71
  • What Layout are you using? – greg-449 Sep 14 '15 at 12:19
  • I am using GridData. `preferredResourceCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));` – Saras Arya Sep 14 '15 at 12:26
  • You could try setting the `widthHint` of the `GridData` – greg-449 Sep 14 '15 at 13:19
  • After some research I found out I am using Grid Data too. Grid Data's constructor has following argments `int horizontalAlignment, int verticalAlignment, boolean grabExcessHorizontalSpace, boolean grabExcessVerticalSpace, int horizontalSpan, int verticalSpan` in which I set `grabExcessHorizontalSpace` to false. Still If I resize I get the same result. What else could I try?? – Saras Arya Sep 14 '15 at 13:21
  • Thanks a lot greg that worked... :) – Saras Arya Sep 14 '15 at 13:28
  • @greg-449 Write a proper answer please. Comments are not made for eternity. – hiergiltdiestfu Sep 15 '15 at 06:51

1 Answers1

3

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);
greg-449
  • 109,219
  • 232
  • 102
  • 145