Creating Buttons Dynamically in Java - java

I want to create specified number of buttons dynamically, but the size of the buttons should match the window.
Eg. First I am creating 10 buttons dynamically then it should create 10 buttons on the window. But in the time When I am specifying 5 buttons then it should create 5 buttons and it should match the window, that is according to the number of buttons the size of the button should adjust with the window size.

Which toolkit are you using? I'm going to assume you're using Swing or AWT.
In Swing and AWT, there is a thing called a layout manager that specifies how components are laid out. In this case, BoxLayout sounds perfect for what you're seeking to do.

Related

How do I get jframe buttons to show up next to each other? [duplicate]

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 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.

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

Adjustable Panels In a Display using Java Swing

I have an application that uses Swing. The display I am working on, uses a Box with a Horizontal Layout as the top container. In it, are three other boxes (which actually contain the content). The appearance to the user is a window with three panes, arranged horizontally across the screen. What I want to do is give the user the ability to change the pane sizes (the width of the panes). I tried putting the interior boxes in a JTable, that failed miserably. Any other ideas?
Thank you
Try using a nested JSplitPane.

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