How to prevent resizing of a SWT combo? - java

When I call setItems(String[]) the combo box will become wider to fit the length of the new items.
How to make it not resizable no matter how wide the new items are?
I've tried it with ControlListener. I saved the initial size of the combo and set it each time the controlResized() was called. As the result the combo width remains constant but the rest of the UI looks ugly when I resize it. It is a wizard. I'm using GridLayout

If you are using GridLayout for your layout you can specify a widthHint in the layout data for the combo.
GridData data = new GridData(....);
data.widthHint = 100;
combo.setLayoutData(data);
If this is in a dialog you can use convertWidthInCharsToPixels(chars) to specify a width in characters rather than pixel. Dialog also has a static version of this method you can use outside of a dialog.

Related

buttons size increase controlsfx

is possible increase the size of the buttons in dialog.
I need to be bigger.
Thanks
You can achieve it by accessing the ButtonBar of the dialog you created and then customize the buttons in it.
First get the ButtonBar instance:
ButtonBar buttonBar = (ButtonBar)yourDialogInstance.getDialogPane().lookup(".button-bar");
Then get all buttons applied to the dialog pane and set the style for them via a string containing css. For example:
buttonBar.getButtons().forEach(b->b.setStyle("-fx-font-size: 18px;"));
A lot more detail: Customizing Dialogs

How to get a Label with wrapped text?

I ave tried the following code:
Label label = new Label(reallyLongString, skin);
label.setWrap(true);
label.setWidth(100); // or even as low as 10
table.add(label);
...
and yet all I get is a very wide line that draws off the screen. How to get a Label with wrapped text?
This is the same issue as seen in slider always has default width or "Slider always has default width".
You need to put that label into a table and add the right size to the cell of the table where the label is.
UI widgets do not set their own size and position. Instead, the parent widget sets the size and position of each child. Widgets provide a minimum, preferred, and maximum size that the parent can use as hints. Some parent widgets, such as Table, can be given constraints on how to size and position the children. To give a widget a specific size in a layout, the widget's minimum, preferred, and maximum size are left alone and size constraints are set in the parent.
Source: From the libgdx wiki Scene2D
The solution:
Label label = new Label(reallyLongString, skin);
label.setWrap(true);
label.setWidth(100); // or even as low as 10
table.add(label).width(10f);// <--- here you define the width
I've found that the following code can solve the issue without any table or wrapping container (for libgdx-1.9.6):
label = new Label("Some label", skin);
label.setPosition(600, 50);
label.setWidth(200);
label.setHeight(50);
label.setWrap(true);
stage.addActor(label);
If you just want to specify the preferred size for a single widget, wrap it with a Container.
https://github.com/libgdx/libgdx/wiki/Scene2d.ui#layout-widgets
https://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/scenes/scene2d/ui/Container.html
Something like:
Container<Label> labelContainer = new Container(myLabel);
labelContainer.prefWidth(200.0f);
Keep in mind that the actual size will vary depending on the container hierarchy - for example, the labelContainer above will display differently if placed in another layout object.
The size will also vary depending on the viewport, etc.

SWT - computingSize for Multi-line textfield inside ScrolledComposite

I have a composite(innerComposite) within a ScrolledComposite(sc). At runtime, additional UI components can be added. So I'm using the code below to set the minimum size of the sc to enable scrolling, in case that the additional UI components "overflow" the sc.
sc.setMinSize(
innerComposite.computeSize(
innerComposite.getSize().x, SWT.DEFAULT
)
);
One issue is with the SWT Multi-Line textfield inside this sc/innerComposite.
textBox = new org.eclipse.swt.widgets.Text(parent, SWT.MULTI | SWT.BORDER | SWT.V_SCROLL);
When I enter a long text into this multi-line textfield, the vertical scrollbar will appear, (which is what I wanted!). However, when I call the code above to recompute the size and setting the sc.setMinSize()...the multi-line textfield's height will expand to fit the length of the text entered. This is not what I wanted. I want this height to remain on with the scrollbar and not resize to fit the text entered.
I know computeSize will cast its children to resize to the preferred size. I don't want this to happen to the multiline textfield as it has the capability of scrolling the vertical bar.
How can I prevent this from happening?
An alternative way of solving your problem is writing your own custom layout and setting that to your inner composite. This way you can control exactly how the controls will appear.
The solution is to use GridLayout in the content Composite. This allows you to set a GridData as layoutData on the text which contains both a wHint+hHint (which is used when doing computeSize() on the composite ) but still activate verticalGrab which will attribute any additional space to this composite.
In other words: the preferred size of the GridLayouted Composite uses the hHint/wHint from the GridData's of its children. This allows you to set the preferred size of you Text while still expanding the text using GridData.verticalGrab if extra space is available.
i solved this by setting the width/height!

How to apply another LayoutManager to a JComboBox? (multi-column JComboBox attempt)

I'm trying to make a multi-column JComboBox. I've looked around quite a bit and it seems to be a very tricky thing to do. Unless many people, I'm not interested in having a table (where you select a row): I need to eliminate the scroll bar in the JComboBox and, in order to achieve this, I want to lay its items in a multi-column list instead of having them in only one column.
My best bet so far was to do this:
JComboBox dropdown = new JComboBox(validValues);
CellRendererPane crp = (CellRendererPane) dropdown.getComponent(1);
crp.setLayout(new GridLayout(4, 4)); // for 16 items...
But it doesn't work. It still lays cells in a single column. I tried adding items after setting the LayoutManager, but it doesn't affect the result.
Anyone has a clue about how to achieve this?
So far, I've seen the ListCellRenderer as useless to play with. It only specifies how to draw a cell (one at a time), not how to lay all of them (what is their relative position to each other).
Any help is welcome!
Thanks!
MJ
A combobox uses a JList to render the items in a popup. By default each item is displayed in a single row. You can access this list directly using:
Object child = comboBox.getAccessibleContext().getAccessibleChild(0);
BasicComboPopup popup = (BasicComboPopup)child;
JList list = popup.getList();
Now that you have acess to the list you should be able to change the default display by using:
list.setLayoutOrientation(JList.HORIZONTAL_WRAP);
Hopefully the items will now wrap when the width of the dropdown is reached. The width of the dropdown is controlled by the width of the combo box so you may need to play with the width of the combo box by using:
list.setPrototypeDisplayValue(....);
Edit:
Actually, forget about using setPrototypeDisplayValue(...), I think you will need to manually set the size of the popup.
By default the width of the popup is always equal to the width of the combo box. You can modify this behaviour by using a PopupMenuListener to override the size of the popup. To get you started you can look at the Combo Box Popup entry. Your code will be much simpler since all you will need to do is hardcode the desired width of your popup.

Auto-Resizing JTextArea

I want my JTextArea to resize itself (expand vertically) when the last line (that the text area's height can offer) is reached and the user wants to start a new line. You know, like the textbox in MSWord.
I have an idea to use getLineCount() and determine (if necessary) the new height of the JTextArea. Do you have, or know of better approaches for implementing this?
Actually, the JTextArea always has the correct size so all lines of text are visible. What you experience is probably that you wrapped the text area in a JScrollPane. Just omit the scroll pane and make the text area a direct child of the container.
Another solution is to listen to resize events of the text area and size the scroll pane accordingly. This way, you can grow to a certain size and then start to display scroll bars (for example, when someone pastes 500KB of text into the text area).
I had the same problem. From my tests, I do not believe that the JTextArea sets its size dynamically. Instead, its size seems to be limited by its container (a JPanel in my case). However, the JTextArea does change its preferred size based on the text it contains. From the documentation:
java.awt.TextArea has two properties rows and columns that are used to determine the preferred size. JTextArea uses these properties to indicate the preferred size of the viewport when placed inside a JScrollPane to match the functionality provided by java.awt.TextArea. JTextArea has a preferred size of what is needed to display all of the text, so that it functions properly inside of a JScrollPane. If the value for rows or columns is equal to zero, the preferred size along that axis is used for the viewport preferred size along the same axis.
Go to JTextArea "Properties" - checklist "lineWrap".
I had the same problem,I put the JTextArea into a JScrollPane and set the preferred size of JTextArea, and I believe that's the cause of the problem.
So the right solution is to put the JTextArea into a JScrollPane, and don't touch the preferred size of JTextArea, set JScrollPane's instead.

Categories