unable to resize components in java windowbuilder - java

I'm designing GUI using java swing with the help of windowbuilder. I found that in any layout it's not possible to resize components by using mouse drags (even though it shows points to pick and drag to resize). Specifically reducing size is what most important to do.
Resizing is allowed only in two layouts: one in Absolute Layout (which is not at all good for practical purpose, considering different screen-sizes with which GUI should be better displayed) and another is Group Layout (which is also not a good for design due to it's complex code).
Following is the sample where I have placed two JLabels and now trying to add JComboBox at the location indicated by Green box.
But when I place the JComboBox it's default size is to fill horizontally. Even if I change fill to 'None' and try to resize, I'm unable to resize it. Following is the result after addition of JComboBox:
In the Background there is JPanel with GridBagLayout with following properties:
I found that changing values in columnWidths and rowHeights properties of GridBagLayout, the size of grid columns/rows can be controlled. But I'm unable to understand Size of which columns/rows all those values represents?. (I found no direct relation between number of those values and number of columns/rows displayed on Panel)
Is there any way out to resize components? And can anybody explain what those values in columnWidths and rowHeights properties of GridBagLayout represent?

It's simple, you need to add grow in you WindowBuilder. It looks like this:
picture
Click on this with your right mouse button and click on 'grow':
picture
Only objects with 'grow' are resizable.

Related

Checkbox too easily selectable (Java JCheckbox in JPanel with GridLayout)

I have written a Swing UI that has a JPanel with numerous controls and on the right hand side a few columns of JCheckBoxes. This is all handled by making the JPanel use a GridLayout. The problem I am having is that a given checkbox toggles it's selection status no matter where in it's grid "cell" you click in. Note, I am not using a JTable approach. The "cell" is just the rectangular area of the screen the GridLayout gave to the checkbox. It can be much bigger than the checkbox. I can't figure out how to make sure the checkboxes are only selectable when you click in the tiny box of the drawn control (not the big box of the "cell" that the checkbox is basically centered in). I've googled a lot and everyone talks about JTables. Again, I am not using a JTable. This issue is causing headaches for my users as they click on the application window and accidentally select an option!
The GridLayout forces all UI components to fill their cell completely, so the actual checkbox only gives the illusion that it's smaller than the cell it occupies. The solution here, as with many other more complex UI designs, is to use multiple layouts nested inside one another.
In your case, try putting all your check boxes inside a BoxLayout and using glue to space them as needed. This BoxLayout should be placed side by side with your GridLayout in another enclosing container (either a JPanel or your ContentPane -- I can't say for sure because you only gave a brief description of your UI with no code or illustration).
Play around with the idea of nesting layouts until you get something you like, and don't forget to try resizing your window to see what the layout manager does under the circumstances. The final appearance isn't always exactly what you imagine it will be.

Best Layout Manager for creating two columns in a single panel?

I am trying to create a GUI for a program with an undefined number of rows but stay with two columns. Currently the only way I can get it to look how I want is using FlowLayout but the window of course needs to be smaller than desired. It needs to happen within one panel as well because it is being added to a tabbed pane.
What would be the best layout manager to solve the problem that is in the JDK? Or would just kind of brute forcing it with AbsoluteLayout be the best approach (as the user shouldn't really be resizing the window)?
I have attached the desired appearance.
Thanks for any help in advance.
Several layouts can do what you want. I'd suggest GridLayout (easy to use, but columns will be equal width) or GridBagLayout (harder to use, but you have lots of control). You could also use a BorderLayout and put all the fields in a sub-panel on the WEST and all the drop-downs in a sub-panel on the EAST. The difficulty with that is ensuring that the rows have the same height, since they won't be constrained by the layout itself.
The best thing to do would be to go through the Java tutorial on layouts and get up to speed on what the various layout managers can do.
Also, since you're using Swing, you could just use a JTable (as Gilbert Le Blanc suggests in his comment).

Resize gui elements by dragging the corners

I have a swing project and noticed that the elements inside the JFrame is statically located inside the JFrame and I do not have the option of resizing the elements as I want to.
My gui app inside the designer window looks like the following:
I want to resize the button (E.g.) by dragging the corners of the button, but I am not allowed to?
As you can see on the following picture, the dragging is not allowed per pixel, but only per section in the JFrame:
How can I disable the static placement of the elements/Enable the self-dragging of elements inside the designer window?
Most likely you will need to disable the LayoutManager. On Netbeans, Setting this to null would provide you full control over the location and dimension of the child elements, so I am assuming that something similar should work here (although you seem to be using Eclipse, if that is the case, please state what plugin you are using).
It is to be noticed however that usually you want to have a layout manager taking care of how your components are rendered. I would recommend you take a look here for some more information on layout managers prior to removing them completely.
Setting size and position of components in a GUI should be left to the JRE at run-time, using:
The preferred size of the component (with current content, in the current PLAF).
The borders/padding/insets set to the component.
The layout used.
The component spacing defined in the layout constructors.
The layout constraints used when adding components to the layout (especially important to e.g. BorderLayout and the much maligned GridBagLayout, as well as many 3rd party layouts).
Generally, complex effects are created by using a nested layout.

Swing layout - Using a grid while keeping component dimensions

I'd like to make a login bar for an application and I can't figure out how to organize a series of JLabels and JTextFields such that they are organized in a horizontal grid without these same components being resized to fit each cell. I also want to make sure that the group of components isn't resized below a certain width. How can this be achieved?
Edit: Thanks for the answers everyone. I'll have a look at MigLayout and SpringLayout later. Due to time constraints I'm going to have to make do with Visual Editor and use a null layout. The component placement and dimensions have to be adjusted by hand but at least they stay put. Here's a picture showing what I wanted to do.
bar http://img145.imageshack.us/img145/7356/bargw.png
Use MigLayout as your layout manager, it's extremely flexible, and supports what you're asking quite easily. You can set size constraints. If you need any further help, post some example code using Swing and MigLayout which shows what you're trying to do, and then I'll advise you on how to do what you want to achieve.
You probably want some additional cells which 'grow' to fill the remaining space. This can be achieved with column constraints, by inserting 'push' between the columns (specified by [..]) to expand the gap. You don't need any placeholder components in this case. (i.e., [pref!]10px[40px::]push[pref!]10px[40px::])
You have to use different layout. FlowLayout or BoxLayout will work in your case, but I would suggest MigLayout simply because it will cover all your needs and replace all others .
Check out the section from the Swing tutorial on Using Layout Managers.
The SpringLayout has an example that does exaclty this.
The GridBagLayout is more difficult to use but also support row/column type layout.
Finally, you can still use a GridLayout. Just add the text fields to a JPanel first, then the panel will grow but the text field won't.

Can a layout manager spawn several JPanels?

I have to build a rather large form with many controls. The controls are divided in basic controls/settings and extended controls/settings. The user can decide if he wants to see only the basic or both basic and extended controls.
I've dropped all extended controls onto their own JPanel so that I can easily switch between the two views by showing or hiding this panel.
Currently I'm using GroupLayout and what happens is that the controls on different panels are not aligned:
Label aaa: Text field
Label a: Text field
Label aaaaaa: Text field
----------------------------
Label b: Text field
Label bbb: Text field
Label bb: Text field
Unfortunatly I found now way to "synchronize" the layouts of the two panels (except using AbsoluteLayout and fixed control coordinates)
Is there any way to achive this?
Is my whole design flawed?
EDIT: If it is possible I would like to keep the GroupLayout manager.
As far as I know, no Swing LayoutManager (from JRE or open source) can span several panels.
I am currently working on such a feature (which I called "layouts synchronization") for my DesignGridLayout project, but it is not something easy to implements (I have started about 2 weeks ago and I still don't see exactly if and when I will get to something interesting, but I still have high hope for it;-))
One option you could check would be to add all components to the same panel (with just one GroupLayout then) and hide/show them based on user's selection. Hopefully, GroupLayout will adapt the size to the situation (after calling pack()).
If GroupLayout behaves well, then it would just be a matter of calling pack() each time after user changes his selection to show/hide extended fields.
Else you would have to manually set the size of your panel every time the user changes his selection.
Probably the easiest (good) way to do it is to add all the components to the main panel. Set the subpanels to non-opaque, and add the also to the main panel. The main panel the needs optimised drawing to be switched off.
Another technique is to add a spacer component. To the bottom panel add a component in the same column as the labels which dynamically takes the width component of its various size methods from the top labels. Do the same in reverse to the top panel.
I think there is no way to do it with the standard layout managers. You'll probably have to write your own layout manager, but it shouldn't be too hard if you subclass GroupLayout.
You could use GridLayout instead of GroupLayout which will give you uniform spacing between the columns
If you want to keep them in separate panels with separate layouts:
Iterate over all of the labels that you add, and find the maximum preferred width of each.
Iterate a second time, and set the preferred size to that each label's preferred height, but the maximum width.
This is the explanation of th GridLayout. This will set every component to the size, you expect it. With the GridData object you can specify how the components are ordere.
Examples
(source: sun.com)

Categories