How do I resize the dropdown of a combobox? - java

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

Related

Libgdx how to set space between the check box and the text label

I've created a checkbox skin for libgdx Scene2d but the problem that there is no space between my checkbox and the text label , here is my code for the checkbox :
final CheckBox vSyncCheckBox = new CheckBox("vSync", skin);
vSyncCheckBox.setChecked(vSync());
vSyncCheckBox.getCells().get(0).size(30, 30);
and in the table layout i tried to use spaceRight(10); but nothing happens :
table.add(vSyncCheckBox).top().expandY().spaceRight(10);
here is the image on what the checkbox looks like for the moment:
Like you can see the checkbox and Vsync are stack together any help on how to provide some space between them ?
This is how you set space between the check box and the text label.
final CheckBox vSyncCheckBox = new CheckBox("vSync", skin);
vSyncCheckBox.getLabelCell().padLeft(10);
This is how I solved my similar problem:
table.add(yourButton).padLeft(Distance).OTHER_STUFF;
Next time please refer to the Wiki table page to solve any problems of or relating table layout
If you have any problems in understanding the concept turn on debugging
table.setDebug(true);
If your check box and your label is one single item, you can just simply put some space " vSync" in your declaration of the object instead of "vSync".
Could you please post the whole code of that table? It might be not working because of other settings made to the table.
Also, it can be helpful to activate debug mode by using
table.setDebug(true); // turn on all debug lines (table, cell, and widget)
Source: https://github.com/libgdx/libgdx/wiki/Table#debugging

When I click on a button it should select certain items according to the condition

I have one combo box,a jbutton and some labels.
When in the combo box item 1 is selected and i press okay(j button) it should change the color of some labels not all the labels(i.e. label 1, label 5 and label 3).
And when item 2 in combo box is selected it should change the color of label(2,4,6).
What should i do?
What condition i should apply?
"What should i do?"
Add an ItemListener to the combobox, and use label.setForeground(color); or label.setBackgound(color);, depending on what which you want changed.
"What condition i should apply?"
String selection = (String)comboBox.getSelectedItem();
if ("blue".equals(slection)) {
label.setForeground(Color.BLUE);
}
This may not be what you want exactly, but you haven't explained much or show what you have attempted. I can say that this seems like a rather simple and basic GUI task. If it is troubling you, I suggest you go though How to create GUIs with Swing

Wrap exceeding content of combo box in available space?

I have been working on an SWT project. I have a combo box whose values are set through database. I have set a fixed size for combo box but when a long string is set as an item for it, the width of combobox exceeds out of set size. I want the combo box to wrap the content by shifting the exceeding string to next line.
Here's my code:
String items[] = {"A","B","WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW","C"};
Combo combo =new Combo(comp, SWT.WRAP);
combo.setItems(items);
combo.select(0);
GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
combo.setLayoutData(gridData);
You will not be able to achieve WRAP in SWT Combo.
You may want to look at
http://www.eclipse.org/nebula/widgets/tablecombo/tablecombo.php
I am not sure TableCombo supports multi line items. But you can achieve that by using OwnerDrawLabelProvider on TableViewer.
Combo does not support SWT.WRAP. What you are describing is not possible through any of the standard widgets. This will have to be a completely custom control.
If you are making use of the GridLayout on the parent composite, GridData with widthHint can be set as layout data to the Combo control. However, dropdown/hints would still overflow the set width

2 column jcombobox

I had quite a lot use for a multicolumn jcombobox, but have not yet found any nor managed to make my own. I have tried several approaches found in web, but they have not worked. Afterwards I read somewhere that those (old) does not work under current Java version.
I have managed to make my own so far, that the combobox has a table as dropdown list and I can select an item with mouse, but the goal is that when the user starts to type into the edit box, drop down list opens and cursor moves based on the text the user has written. It seems that the events from e.g. JTextField editor = (JTextField) comboBox.getEditor().getEditorComponent() does not work.
Has anybody managed to make a two column combobox or have any ideas, how I could get the event when the user starts to type.
You are looking for autocomplete functionality (as I understand the question): it's supported in SwingX - and quite easy to use.
It boils down to implemneting a custom ObjectToStringConverter and configure the comboBox with an autoCompleteDecorator using that converter. Something like:
/**
* A converter which expects an item of an array type and returns
* a string representation of its first value.
*/
public static class ArrayToStringConverter extends ObjectToStringConverter {
#Override
public String getPreferredStringForItem(Object item) {
if (!(item instanceof Object[])) return DEFAULT_IMPLEMENTATION.getPreferredStringForItem(item);
Object[] array = (Object[]) item;
return DEFAULT_IMPLEMENTATION.getPreferredStringForItem(array[0]);
}
}
// usage
// assuming an model with items being arrays
JComboBox combo = new JComboBox(arrayModel);
// the renderer supporting multiple columns, f.i. a table
combo.setCellRenderer(new TabularListRenderer());
AutoCompleteDecorator.decorate(combo, new ArrayToStringConverter());
A complete working example (including the renderer and showing how-to force the width of the popup to be larger than the combo itself) is TableAsListRenderer in my incubator section
BTW: the autocomplete functionality is a standalone module, accessible via maven or manually downloadable from the maven rep at java.net, you would want the swingx-autocomplete-1.6.4.jar (plus the corresponding doc/sources, if interested)
Has anybody managed to make a two column combobox or have any ideas, how I could get the event when the user starts to type.
you can to put JTable to the JComboBox, but by default you can to select only value from entire JTables row, not directly from JTables Cell (required additional workaround, not tried yet)
i still searching for this answer too
here's i try so far..
i create Jpopup and put the Jtable in there..
then i use jlabel not jcombobox,
when user click jlabel, then the popup(Jtable) will show in that jlabel location..
when user select value on jtable,
then the popup will dispose,
then the jlabel will show the result..
for your case, you can use jtextfield not jlabel
EDIT :
heres related question
enter link description here

Changing the direction of a Combo box dropdown in SWT

I'm building an Eclipse plugin in SWT, and I have the following problem: one of my fields is a combo box, and in some cases it may have fairly long items as selection options. My plugin runs on the right side of the screen, so when you go to use the combo-box, the right side of the combo box is cut off. So, my question is: is there any way to change the dropdown's alignment relative to the combo control? It seems to be permanently left-aligned... and I'm pretty sure you can change the direction in Swing (though the only place I've seen it done is in the Substance UI demo. The Combo Box tab has boxes with North, South East, and West flyout directions... for my application, I need something like the West flyout)
Note: Setting actual text limits is a last-case-scenario option; it would be quite a bit of guesswork to set the text limit dynamically (since the widget's view can be resized).
Here's a picture (sorry, I can only have one link and no images :( ... I need some more rep :p)
Left side of the line: Proper width - the view is the wide enough for the combo dropdown to display all the text; you can see the scrollbars on the right side.
Right side of the line: Too small - Here, the view has been resized, and the combo dropdown scrollbar (as well as some of the text) is cut off by the right side of the screen. I always have more screen space available to the left (since this is always on the right hand side of the screen), but the combo dropdown always appears to the lower right.
Hopefully this is clear enough.
Now I understand your problem.
Use a CCombo instead of a Combo. It should automatically position the drop-down list so it fits.
Hi I came across the same problem. CCombo does the trick but it doesnt look good and it has bug like this one that wont be fixed.
combo.setOrientation(SWT.RIGHT_TO_LEFT); does the trick for you.

Categories

Resources