How to setSize JButtons in a JPanel - java

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.

Related

How would you create a layout like this?

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.

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.

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.

Dividing space equally between unknown number of components in Swing?

I have a Java Swing application that has a bar of JButtons. The number of them is not known beforehand. I'd like to size the buttons equally to fit in the frame. Also, I need to be able to add new buttons and have them resized automatically.
For example in Android you can use the layout weight attribute to do this. Now, is there any Layout etc. in Swing that can do it for you?
The GridLayout arranges all its components with equal size by default

Resizing two ScrollPanes equally in Swing

I am designing a JFrame that contains two ScrollPanes ordered sequentially, one below the other. I am using the inbuilt form editor of NetBeans (Matisse?) which uses GroupLayout.
What I observe: When resizing the frame, first the ScrollPane at the bottom is resized until it vanishes then the buttons above this ScrollPane and finally the other Scrollpane.
What I would like: When resizing the Frame, both ScrollPanes should be resized equally, so if the frame changes size by X, I want both Scrollpanes to change their size by X/2.
Is this possible and how? (Solutions for GroupLayout preferred)
P.S.: The total Layout includes Buttons and Labels placed in a table like manner. So GroupLayout is already quite useful for it.
P.S.: It seems the best option beyond simple Layouts (Grid, Box) is MigLayout.
Let me cite from the white paper
Growing and Shrinking
What should happen when a component isn't given the preferred size is extremely customizable. There is the option to divide components into grow and shrink priority groups where a higher priority will shrink/grow before components/rows/columns with lower priorities are resized. Grow weight within those groups can be set to specify how the free space or shrinkage should be divided between the components/rows/columns in that priority group. ...
P.S.2: I am now using MigLayout and it is so powerful that I want to recommend it again. Btw. I find their cheat sheet very useful (all options explained). Also the demos on their website contain many example and the source code. The problem here and many others can be easily solved with MigLayout.
Is this possible and how?
by using GridLayout, GridBagLayout, BoxLayout (have to override all Min, Max and PreferredSize) and for MigLayout
Is this possible and how? (Solutions for GroupLayout preferred)
I woudln't be going this way, even could be possible
A grid layout ensures that the elements are evenly sized. Use:
JPanel panel = new JPanel(new GridLayout(2,1));
Since you want them vertically aligned, that is after each other, 2 denotes the number of rows, and 1 denotes the number of columns.
Then simply add the ScrollPanes to the panel:
panel.add(scrollPane1);
panel.add(scrollPane2);
For documentation on Grid Layout, look at this

Categories