I have dynamically created a bunch of buttons. I gave a fixed width to each of them. I want it to be added until space is available to its parents and when it overlaps with some other nodes, it will automatically go to the next line in order to avoid collision. I want it to be responsive at different screen sizes.
for(Data i:datas){
Button btn=new Button(i.getName());
anchorPane.getChildren().addAll(btn);
}
I want to arrange the buttons in a new line only when the buttons in the first line have covered its total width. How can I accomplish it? Any help is appreciated.
James_D provided a great resource—there's a built in layout for anything you need and FlowPane has what you want here.
Using your example, just swap out your AnchorPane for FlowPane:
FlowPane flowPane = new FlowPane();
for(Data i:datas){
Button btn=new Button(i.getName());
flowPane.getChildren().addAll(btn);
}
To make it responsive (i.e. expand and contract with the parent container/stage), bind the FlowPane's width and height to those of its parent:
flowPane.prefWidthProperty().bind(stage.widthProperty());
flowPane.prefHeightProperty().bind(stage.heightProperty());
In an SWT application, I have a composite with a GridLayout that has two columns. The first contains labels, the second texts and checkboxes. I use TextLayouts for some of them and paint them manually (in a paint listener), so I have to manually compute their sizes. I set the computed sizes for the widgets with setSize() and set widthHint and heightHint in their respective GridDatas. GridData objects for the labels (1st column of the grid) are created with (SWT.RIGHT, SWT.CENTER, false, false) and for the widgets in the 2nd column with (SWT.FILL, SWT.CENTER, true, false). The computeSize() for the widgets returns the sizes I expect (eg. no more then 68 pixels of width for the 1st column and 128 for the 2nd).
But the computeSize() for the Composite returns a width that is much larger than I expect to see (or want), namely 253, while I expect 68 + 128 + 3 * 4 + 2 * 2 = 212 or something near to it.
The pack() on the Composite, acccordingly, makes it grow much wider than I want. I can force the required width of the Composite with the setSize(), but subsequent actions on the shell containing my composite invoke the composite's pack() anyway, so its width gets spoiled. Setting of the widthHint of the composite's GridData won't work either.
The Composite itself is a child of a upper-level Composite with GridLayout and has a GridData with (SWT.LEFT, SWT.FILL, false, true).
Why the computeSize() decides that the width of the Composite should be 253, if the widths of the widgets in the columns are computed to be 68 and 128 pixels wide and their widthHints are set correspondingly? How does it work? What does it take into account, besides the computed sizes of the child widgets? Where can I find a description of its algorithm (the source of the GridLayout's layout (Composite composite, boolean move, int x, int y, int width, int height, boolean flushCache), which actually computes the size, is rather complicated and difficult to understand) ?
How can I make the layout managers keep the required width of the Composite while changing layout of the parent Composites, including the Shell (on resizing, font changes etc)?
GridLayout is full of surprises. If you are not dogmatically stuck to it, I recommend switching to FormLayout. This is much more powerful and gives you much much more control over the final result of your UI. It is a paradigm shift compared to GridLayout, but it is very much worth learning it.
With FormLayout, you can use the dimensions of the parent, or the position and dimensions of any sibling Controls (ie. Controls sharing the parent) for positioning and sizing controls. It's like declaring a couple of layouting constraints per control and the manager will do its best to satisfy your wishes.
Here is a tutorial for you to get you started. It introduces you to FormData, which declares four anchor points (top, bottom, right, left) and the FormAttachments which define the constraints in a FormData object.
For your problem, you can layout the first column as using eg. 40% of the width of the parent, with each row being placed below the label of the previous row. The controls of the second column are placed relative to their label (on their left) and the label or control of the previous row (on their top).
Now what seems to bother you is the width of some of the controls in the second column. In FormLayout, you can set an explicite constraint for the right anchor, either by declaring it to be eg. at 100% width of the parent (starting from the 40% right anchor of the first column) which will then rely on the layout of the parent's parent, or by setting the right anchor as an absolute margin in pixels counting from the right edge of the first column. This can be done by referencing the label in the first column, but switching the alignment. This is necessary because by default, a relation from the right anchor of Control A to a sibling B will automatically use the left side of B. In code, this second possibility will spell out like that:
FormData fd;
//row 1
Label lblC1R1;
Control ctrlC2R1;
//row 2
Label lblC1R2;
Control ctrlC2R2;
...
fd = new FormData(); //for ctrlC2R2;
fd.top = new FormAttachment(lblC1R1); //top edge clings to (y-coord of) bottom of row 1 label (could reference row 1 control, too)
fd.left = new FormAttachment(lblC1R2); //left edge clings to right side of row 2 label
fd.right = new FormAttachment(lblC1R2, 200, SWT.RIGHT); //right edge is defined as a distance of +200 px from *right* side of row 2 label (by default, it would use the left side)
//we don't set fd.bottom, so the control may grow or shrink with ie. font size
ctrlC2R2.setLayoutData(fd);
This will fix the width of the second column control to 200px starting from the right side of the first column, no matter what the control says about its preferred size.
I'm trying to create a Composite containing FlowLayout inside a ScrolledComposite so that the Composite inherits its width from the ScrolledComposite but grows in height dynamically. I've searched some snippets but I'm not getting them to work; I can only get it to grow in width which is not desirable.
What I'm trying to do is to create a View containing a bunch of thumbnails to fill the window/surrounding composite in width and scroll in height.
You will need to override the computeSize method of the composite, or the FillLayout to always report back the given widthHint, unless perhaps the width hint is smaller than a single thumbnail's width in which case you might want to show horizontal scroll).
There could also a possibility that you add a resize listener to ScrollComposite and keep calling ScrollComposite#setMinSize() to ensure the composite inside only grows vertically.
Also are you using the correct type on FillLayout?
I am trying to create buttons shaped as in the following picture:
This works pretty well by doing the following:
class ArrowButton extends Composite {
ArrowButton(Composite parent, int style) {
[...]
setRegion(customRegion);
}
}
addStuff(Composite parent) {
ArrowButton b = new ArrowButton(parent, SWT.NONE);
b.setBounds(x, y, width, height);
}
However, I want to use a layout manager to layout these buttons. Ideally, the buttons are layouted on a standard size, after which they can still paint outside of this region.
Is this at all possible? How can I do this?
Layout managers have two functions: calculating the size of the composite that they are applied to and setting the bounds of the composite's children. Given the relative simplicity of your requirements (line up buttons horizontally) I'd say that you're better off doing the layout yourself (rather than trying to hack an existing implementation). You can always put your code into a delegate behind the Layout interface, if that feels better.
I've got a real hard problem with a JTree.
I've implemented a JTree with a selfwritten Model (wich is extended from TreeModel).
fileSystemModel = new MyModel(new File(directory));
fileTree = new JTree(fileSystemModel);
fileTree.setEditable(true);
fileTree.setDragEnabled(true);
fileTree.addTreeSelectionListener(new TreeSelectionListener() {
public void valueChanged(TreeSelectionEvent event) {
File file = (File) fileTree.getLastSelectedPathComponent();
}
});
So i added it to a JPanel and this JPanel to a JFrame.
No i got this Problem: I start the Frame, and if i expand a single Node, the width of JTree is going to enlarge to the width which is needed to show the leafs.
I tried to set this:
fileTree.setMaximumSize(width, height);
But it failed and i also tried to embedd the JTree into JScrollPanel with a Maximum-Size, and this has also failed.
The JTree is still adjusting its width how many it needs.
So i tried now for two days and doesn't find anything, so i would be apprecciate if someone of you could help me.
Thanks!
The effect described is characteristic of the default FlowLayout of JPanel. You can see the change when you click on the left panel is this example.
Addendum: As seen in the example, GridLayout allows the component to expand; it works particularly well with setVisibleRowCount() followed by pack().