How would you create a layout like this? - java

I would like to create the main menu for my program and I have difficulties with Swing.
How would you code an alignment like this?
The middle elements would be the options like exit or settings and this window should be able to be resized and its contents should get bigger proportionally.

Single column GridLayout with vertical padding declared in the constructor. A grid layout will stretch components to fit the available space.
Add a large EmptyBorder to the JPanel that contains the 3 buttons, and that's the job done.

Related

How to add JButtons next to each other without gap/space like in Windows Calculator?

I'm making a calculator same as provided in windows 10 for practice purpose, but I am unable to remove the space between the JButtons. I'm using Netbeans designer view to do this. I have tried by doing margin as 0 even doing -2 of both the buttons but whenever I resize the button and drag it to the other one, the other button goes away automatically.
Here is the screen shot what I want to do:
Here is the design view:
Give the JPanel that holds the JButtons a GridLayout, one with proper rows and columns (call the constructor that uses 2 parameters, again for rows and columns) but that uses no more parameters -- so the layout's horizontal and vertical gap is set to the default size of 0. GridLayout API
Add your JButtons to this JPanel.
Be sure to pack() the JFrame (or other top-level window) after adding components
And calling setVisible(true) after packing
That's really all there is to this.

How to setSize JButtons in a JPanel

I'm trying to fix this issue in my JPanel.
I want to set sizes of 0 and = JButtons in JPanel. Other button sizes are the same but 0 and = are bit long vertically and horizontally (like MS windows Calculator). How do I set it?
I think you can achieve that by using GridBagLayout layout manager.
A GridBagLayout places components in a grid of rows and columns, allowing specified components to span multiple rows or columns. Not all rows necessarily have the same height. Similarly, not all columns necessarily have the same width.
See How to Use GridBagLayout.
If the layout used respects the preferred size of the buttons, a larger button can easily be had one of three ways:
Set a larger size of Font
Give the button a larger Icon
Call setMargin(Insets) with a large margin.
If you don't want a hassle, try using WindowsBuilder on Eclipse. You can edit it on a design interface, and the codes will be generated.

unable to resize components in java windowbuilder

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.

Java Swing FlowLayout Alignments

I'm fairly new to Java and I'm trying to create a GUI application with some labels, buttons, and textfields. The program is pretty simple and I just wanted to use a default layout, which is FlowLayout. I managed to place and size everything fine, but the only thing seem to be not working is the alignment. I want to place buttons and textfields with certain alignments, but whenever I set an alignment, it moves the text inside of whatever the object rather than the object itself. For example, I wrote:
button.setHorizontalAlignment(JButton.RIGHT);
but it seems like it aligns the text inside the button instead of the button itself.
Is there any way to align the button itself rather than the text inside of it?
I know the alignment stuff could be easier with some other type of layout (e.g. BoxLayout), but I just want to use the FlowLayout for this one, unless it is impossible to align them using the FlowLayout (which I don't think so).
Thanks in advance.
See the constructor FlowLayout(int align).
Constructs a new FlowLayout with the specified alignment and a default 5-unit horizontal and vertical gap. The value of the alignment argument must be one of FlowLayout.LEFT, FlowLayout.RIGHT, FlowLayout.CENTER, FlowLayout.LEADING, or FlowLayout.TRAILING.
It seems you are after a FlowLayout.RIGHT as seen in this answer (the combo and check box at the top).
I don't think you can do this with a FlowLayout alone.
My suggestions would be:
Consider switching to MigLayout which is a much more powerful layout mechanism. MigLayout basically lets you position you components within a flexible grid, and you can set the specific alignment of a component within each grid cell.
When you want alignment of subcomponents, it also often makes sense to put them inside a nested JPanel. You can then use a separate layout for this JPanel (BorderLayout perhaps?) which will enable you to get the exact alignment that you want.
setHorizontalAlignment of AbstractButton sets the horizontal alignment of the icon and text not the position of the button. AbstractButton's default is SwingConstants.CENTER.
If you want to align the button..set the position while adding it to the panel or frame..something like this....
p.add(button, BorderLayout.SOUTH);//using `BorderLayout`
Flow layouts are typically used to arrange buttons in a panel. It will arrange buttons left to right until no more buttons fit on the same line.

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