如何阻止 JFace 组合框在调整窗口大小时调整大小? [英] How to stop JFace combo box from re sizing on a window re size?
问题描述
我有一个 Java Eclipse RCP 程序,其中 JFace 组合框内有一个长字符串.现在,当我在同一个视图中时,组合框会在它上面附加一个滚动条以显示全名.但是一旦我重新调整应用程序窗口的大小,组合框就会自行伸展以容纳长字符串.
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.
附言我使用的是一个 comboViewer,里面有一个 comboBox.
P.S. I am using a comboViewer and inside it a comboBox.
提前致谢.
推荐答案
如果您使用 GridLayout
和 GridData
作为布局,您可以在 >widthHint
字段来建议组合的宽度.
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);
如果用户使用大字体,使用这样的固定值可能会导致问题.所以另一种计算宽度的方法是使用 Dialog.convertWidthInCharsToPixels
:
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);
如果您的代码在 Dialog
中,您可以将其简化为:
If your code is in a Dialog
you can simplify this to just:
data.widthHint = convertWidthInCharsToPixels(charCount);
这篇关于如何阻止 JFace 组合框在调整窗口大小时调整大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!