Add a List<JToogleButton> to a JPanel - java

I have a List<JToggleButton> and I need to add all those buttons to a panel.
The panel needs to have only a vertical scrollbar. It has a fixed size of 600 x 600 px.
Buttons have different sizes and I need them look pretty compact (for example, some rows can have two big buttons, some four smaller). I need to add the buttons by order from list (first with index 0, then 1 and so on..).
How to achieve this layout?

You can try placing the JPanel with a FlowLayout in a JScrollPane and add all the buttons.

Related

How can I go about creating this design with Java's Swing GUI tools?

This is a mock up for what I have in mind as a layout for my project:
The way I tried to accomplish this is:
I set the entire frame to a border layout and then cut it horizontally with two panels, we'll call them north and south panels. The south panel is Panel 3 from the first picture.
I set the north panel to a border layout as well and cut it vertically with two panels. These become panel 1 and panel 2 in the first picture. The problem occurs when I try to resize the window. I would like the panels to scale proportionally to eachother so the size ratio's between the panels stay the same. The problem is, instead of resizing, the panels just move away from each other like so:
Any ideas for creating the desired design? Am I on the right track or is there another swing layout that is better suited to my needs?
I would like the panels to scale proportionally to eachother so the size ratio's between the panels stay the same.
Try using a horizontal BoxLayout. I believe it will allocate extra space proportionally up to the components maximum size if extra space is available.
Or if that doesn't work you can use a GridBagLayout. You can use the weightx constraint for each component you add to the panel. This will control how much extra space is given to each component.
Read the section from the Swing tutorial on Layout Managers for working example of each.

JFrame & JPanel sizing issue

I am attempting to code a JFrame containing a JPanel. Within the JPanel is an array of JTextField's. So, my GUI looks like:-
I am not using a layout manager, and have set this to null for the JFrame and the JPanel. I am sizing these components by hand.
You can see that the right hand portion of the JPanel is chopped off, even though I have used the same sizing as the containing JFrame.
The code appears as below:-
I have calculated the required width of the JPanel by multiplying the number of columns in the JTextField array by the width of the JTextField. Aside from that would need to be added the width of each gap between the JTextFields (there would be (columnNumber - 1) of them), as well as the two border gaps.
I have done this, yet the right hand side border gap is chopped off, as you can see from the diagram.
If I add some random amount to the panelWidth, then you can see the right hand gap there, but my question is what am I missing here? This ought to work surely, if the JFrame side and the JPanel size are identical, which they are as I have also printed them both out, and the print outs give the same number.
Jeremy
I want for any combination of row/column values to allow a constant vertical & horizontal distance between each JTextField, and for each of those text fields to maintain default sizing.
The GridLayout allows you to specify a horizontal/vertical gap between each component and allows you to control the size of the grid.
Then you can wrap the panel using a GridLayout in a panel that respects the size of the grid.
For example you could do:
JPanel grid = new JPanel( new GridLayout(...) );
JPanel wrapper = new JPanel( new GridBagLayout() );
wrapper.add(grid, new GridBagConstraints());
frame.add(wrapper, BorderLayout.CENTER);
If you pack the frame, the grid panel will be displayed at is preferred size.
If you resize the frame the grid panel will remain centered in the wrapper panel.
The top part of that GUI is well suited to a grid layout, the bottom part with 'Go / Cancel' buttons - a flow layout. Put the grid layout in the CENTER of a border layout, the flow layout in the PAGE_END, pack the top level container (for non-cropped, 'right size') & the job is done.
It might end up looking something like this:

GridBagLayout set JScrollPane relative to JButton height

I've created a GridBagLayout with a JScrollPane(JTextArea) in the first row (spanning 3 columns) and 15 JButtons in the other 5 rows (each button spans 1 column).
I have set the weights so that all of the components expand equally as the window is resized, and the frame minimum size to its preferred size. However, I'd like the height of the JScrollPane to be 5 * the height of each JButton (half of the total frame height). Is there a simple way to enforce this?
However, I'd like the height of the JScrollPane to be 5 * the height of each JButton (half of the total frame height). Is there a simple way to enforce this?
If you want the area to be half the height then use a different layout manager.
Create a JPanel that uses a GridLayout. This way each component will be the same size.
Then add the scrollpane to the panel. Then create a second panel that also uses a GridLayout. Add the buttons to this panel and then add this panel to the main panel.
The bottom line is you can use multiple panels each with different layout managers to achieve your desired result.

Why is the setPrefferedSize() method not working for JPanel?

I have created n JPanels and in each JPanel I have added 3 components. I added these JPanels to a new JPanel as rows. The layout for the n JPanels is FlowLayout and for the main panel is BorderLayout. The setPrefferedSize() method is working fine for the components which I have added in the n JPanels but it is not working for the n JPanels.
I am trying npanels[i].setPrefferedSize(new Dimension(300,25)),
I want the height of the JPanel to be equal to height the components added in it (which is 25).
Is there any constraint that the height of a JPanel should be some minimum value?
Please help I tried a lot of things but it's not working.....
Some layout managers tend to ignore the size setting...
Read somewhere that BorderLayout might tend to ignore the width for NORTH and SOUTH components,
height for EAST and WEST,
both height and width are ignored for CENTER...
Could this be the case?
Also, can you provide a screenshot or a diagram explaining whats happening?
The setPrefferedSize() method is
working fine for the components
There is generally no need to set a preferred size for components. Swing will calculate the preferred size automatically.
layout for n JPanels is FlowLayout...
which i have added in n JPanels but it
is not working for n JPanels
Again, there is no need to set the preferred size of each panel. The size will be calculated automatically based on the preferred size of all the components.
the main panel is BorderLayout
This does not make sense since you can't add "n" panels to the BorderLayout. You can only add one component to the North,Center and South so you can have a maximum of 3 different vertically display panels. In this case if you use frame.pack() then each panel will be displayed at its preferred size. On the other hand if you use frame.setSize(300, 400) then the hieght of the Center panel will be stretched.
Since it appears you want all panels the same size maybe you should be using a GridLayout, otherwise you can try a BoxLayout. Read the Swing tutorial. It explains all about using the layout managers.
If you need more help post your SSCCE.

Nested JPanel resizing problem

I have two JPanels (let's call these Panel1 and Panel2). These panels are of the same width, but varying heights.
I want to put these JPanels into one big JPanel (lets call it Panel0), and stack them on top of each other (I decided to set Panel0's layout as GridLayout(0,1)).
The problem, is that both nested panels (panels 1 and 2) end up having the same dimensions (those of the biggest between the two), instead of the setPreferredDimension and setDimension that I set to them.
Sorry, I can't really provide any code (there's a lot of crap added to the panel's, and it's all for something work-related). Any advice? Thanks!
GridLayout forces all components to be the same size; that's why it's called a grid.
Since you only have two panels, I'd suggest using a BorderLayout with one panel at NORTH and the other CENTER. If you allow resizing, then the one in CENTER will be the expand to fill any extra vertical space, so just be aware of that.

Categories