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
Related
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.
I'm a beginner and I want to design the gui of the windows calculator in java. I have set some jpanels(Gridlayout) with some jbuttons in each. I set the size of the buttons this way:
jButton[i].setPreferredSize(new Dimension(42, 42));
but buttons in differen jpanels don't have the same size.
what should I do?
jButton[i].setPreferredSize(new Dimension(42, 42));
There are 3 easy ways to make buttons larger.
Use a big Icon for the button
Use a big Font for the button (preferably via a PLAF).
Set a large margin around the text and icon.
As to setting the preferred size. See Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing? (Yes.)
If you are still intent on going that route, take the advice of #trashgod and override the preferred size instead.
You should also set the same size for your jpanels
ie. jpanel.setBounds(startingWidth, startingHeight, actualWidth, actualHeight);
and then add these to your container panel.
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
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.
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)