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

社会主义新天地 提交于 2019-11-28 12:13:54

问题


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.

Thanks in advance.


回答1:


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);


来源:https://stackoverflow.com/questions/32561924/how-to-stop-jface-combo-box-from-re-sizing-on-a-window-re-size

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!