开发者

How do I resize the dropdown of a combobox?

Background: I am doing some UI work in an eclipse environment where I fill a combo control with some values. The string values have an different length and often a length greater than the combo width and the width of the parent composite.

Problem: When I open the dropdown list, the width of the list is greater than the width of the parent composite and the user could not see the complete value of the list entry.

I have tried to use the "setTextLimit" option, but without success. For me it is perfectly adequate if I could set the width of the dropdon list with a constant value.

Code example:

this.mComponentName = new Combo (lComponentComposite, SWT.BORDER);  
this.mComponentName.setTextLimit(COMBO_TEXT_LIMIT); 
GridData componentNameGridData = new GridData();
componentNameGridData.widthHint = 166;
this.mComponentName.setLayoutData(com开发者_Python百科ponentNameGridData);
this.mComponentName.addSelectionListener(this.mComboSelectionAdapter);
this.mComponentName.addKeyListener(this.mComboKeyAdapter);

Greetings dirk


While creating a combobox specify Horizontal scroll also

this.mComponentName = new Combo (lComponentComposite, SWT.BORDER|SWT.H_SCROLL);  

This will not let the text overflow


That is really a good question. After digging through developer forums and even the source code, I lean towards saying it is not possible.

I solved the problem temporarily by switching to a CCombo, but I do not like the solution as I believe one of SWT's strength is in using native widgets, and the CCombo does not look as good (at least on Win7 and OS X).


When you select a drop-down combo box or drop-down list box to size it, only the right and left sizing handles are active. Use these handles to set the width of the box as it is displayed initially. Choose the drop-down arrow at the right of the combo box. The outline of the control changes to show the size of the combo box with the drop-down area extended.

Use the bottom sizing handle to change the initial size of the drop-down area. Choose the drop-down arrow again to close the drop-down portion of the combo box.


Have you tried passing some other value as your second parameter in your Combo initialization (first line) ? Something other than SWT.BORDER?

I've tried finding out if it is possible to display strings in multiple rows (as a single combo box item), but with no success. This would reduce the width of your items. Try adding \n to a couple of strings that you add to the combo box, and see if it will work. If it works, you can automate this process later by parsing through the string, and checking whether the space character count gets to high and adding \n after every fifth or sixth blank character. I think codejammer's suggestion is the best one, but I can't upvote yet.


Partially solved the problem adding to item names some spaces (have set the number of spaces by trial and error). Now combo is wider and all texts are visible, however horizontal scroll is visible, too.

String p = "                                                               ";
combo.add("Long.... item name 1"+p);
combo.add("Long item name ..... 2"+p);
...

Another soultion is resize combo when it gain focus and restore size after lost focus. Example below.

final Point p = combo.getSize();
combo.addFocusListener(new FocusListener() {
    @Override
    public void focusLost(FocusEvent e) {
        ((Control) e.getSource()).setSize(p);
    }

    @Override
    public void focusGained(FocusEvent e) {
        ((Control) e.getSource()).setSize(new Point(400, p.y));
    }
});


combo.setPreferredSize(new Dimension(FIX_WIDTH, FIX_HEIGHT));


Try this

           JComboBox CB = new JComboBox();
           CB.setPreferredSize(new Dimension(int, int)); 
           p.add(CB);// which p is a JPanel
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜