I'm new to Java awt, so I am having trouble with setting up panels. I have one giant panel, which needs to hold 3 panels inside (photo is attached at the bottom). One will go on top(1), second one will be in the middle(3), and third goes on the bottom(2). Any remaining space has to be divided equally between (1)/(3) and (3)/(2). Also, the middle panel (3) is a table, so GridLayout has to be used.
How can I achieve this?
Thanks in advance!
P.S. I've tried to draw it in MS Paint (http://i45.tinypic.com/mwejkk.jpg)
I don't understand all, I suggest :
Use swing, not awt, so use JPanel
A BorderLayout, with your giant panel (jpanel) in middle, a jpanel at west ; for this jpanel
a BorderLayout, or BoxLayout, or GridLayout and put inside your 1 2 3 panels.
... or use netbeans and matisse.
This will help you a lot. It's a Sun tutorial on BoxLayout. It describes the stacked layout that you appear to need, and also how to make invisible components to add gaps in the extra space you mentioned. For the middle pannel, put a GridLayout in that panel to do the things you need.
Related
I'm having trouble getting my JButtons to appear where I want. For some reason all my buttons are appearing in the middle of the Panel when I want it to appear flushed with the top of the Panel, centered. I've tried manipulating different layout positioning such as BorderLayout.NORTH and GridBagConstraints.NORTH however I do not know if I utilizing it on the right components.
Currently my code has A Frame holding a JLayeredPane holding a JSplitPane with two JPanels in it so it has gotten quite confusing.
Do you have any suggestions on how I can get the buttons to appear on the top of the Panel?
I currently have the panel using a GridBagLayout
Read the section from the Swing tutorial on How to Use GridBagLayout for a working example and information about the constraints used by the layout manager.
For some reason all my buttons are appearing in the middle of the Panel
Specifically read the section on the weightx/y constraints, which states that the components will clump together in the center unless you use non-zero values.
I want to add more than 3 panels (GUI) in my project. I declared my panels as top(NORTH), center(CENTER), and bottom(SOUTH).
So my question is, can I add a 4th panel between top and center or center and south?
Is there a reason why you are using border layout? If there is no reason, I would recommend using a different layout.
Assuming that you are using swing, I would recommend using the GridLayout as it seem that you are simply putting panels in a single column. You can specify the GridLayout to have 4 rows, 1 columns,(1 panel/grid) that way you can add all 4 straight up and down. Of course, only you can decide what do use since we have no idea what you are building.
Go here for How to Use GridLayout for reference
Are you using Swing as the toolkit for your gui? If so, use a different layout manager to your frame.
Oracle Guide to layout managers
You're using BorderLayout which turns the available space into 5 zones. To get more, you can create a new JPanel, give it BorderLayout as well and add is as center of the outer container.
That way, you have again 5 zones inside of the outer CENTER zone.
If you want to have 5 vertical jpanels then you can go for Grid or box layouts. Have a look at the Orace documentation about how to use layouts and what they looks like.
Oracle Documentation
I think you should try BoxLayout. You are using BorderLayout which has only 5 zones. If you want to keep your NORTH and SOUTH panels intact then create a seperate panel say center_panel and give its layout like this
center_panel.setLayout(new BoxLayout(center_panel,BoxLayout.Y_AXIS));
and then add the panels to center_panel. They will be added in vertical way.
I have used borderlayout to specify where the content of my java GUI shall be placed, I have then chosen to place it on EAST and then made two boxlayouts to show two columns of buttons. I now have to place something underneath it and not beside it. How would you suggest or advice me to do so, using any layout but preferably boxlayout and not absolute layout(null). Thanks in advance.
Image:
The arrow points to the place I want another JPanel to be.
You could...
Wrap both of the button panels in a JPanel
Whatever component goes at the arrow, wrap in a JPanel with GrigbagLayout (just to center it).
Create another JPanel with BorderLayout that will hold the above panels. Use CENTER and SOUTH.
Give an EmptyBorder to the SOUTH panel, only specifying the top region and space it accordingly.
Really there are many ways to accomplish this. The key though is to nest JPanels and make use of the different layout managers with each, use EmptyBorders or stuts for empty spaces til you get your desired effect. The possibilities are endless. I don't think there's one right answer. Since we don't have a runnable example, I say just try the above, and mix and match will you get what you want.
So I was trying to google how to set a default size to JButtons so that they don't grow as the JFrame is resized. I didn't see a setDefaultSize method but the closest one I could find that does a similar job is setMaximumSize(). However, it doesn't seem to work in my situation and I'm guessing it's because I'm using Grid Layout for positioning my buttons in the frame, here's a small piece of my code:
rightPanel.add(ButtonA);
rightPanel.add(ButtonB);
rightPanel.add(ButtonC);
outerPanel.add(leftPanel);
outerPanel.add(rightPanel);
getContentPane().add(outerPanel);
Here's a picture of what happens:
I would also like to have my buttons in the middle of the right panel when I'm resizing (just like they are now but a lot smaller). Any idea of how I can fix this? I'm assuming that I have to use another layout or something.
Thanks
EDIT: I modified my code to use BoxLayout but it does not seem to put the buttons in the middle. The X Alignment is working but Y Alignment is not doing anything:
ButtonA.setAlignmentX(CENTER_ALIGNMENT);
ButtonA.setAlignmentY(CENTER_ALIGNMENT);
ButtonB.setAlignmentX(CENTER_ALIGNMENT);
ButtonB.setAlignmentY(CENTER_ALIGNMENT);
ButtonC.setAlignmentX(CENTER_ALIGNMENT);
ButtonC.setAlignmentY(CENTER_ALIGNMENT);
JPanel rightPanel = new JPanel();
rightPanel.setLayout(new BoxLayout(rightPanel, BoxLayout.Y_AXIS));
rightPanel.add(ButtonA);
rightPanel.add(ButtonB);
rightPanel.add(ButtonC);
outerPanel.add(leftPanel);
outerPanel.add(rightPanel);
getContentPane().add(outerPanel);
EDIT2: Fixed with vertical glue.
A GridLayout will always resize the components to fill the space available.
Try using a vertical BoxLayoutinstead. See the section from the Swing tutorial on How to Use Box Layout for more information and examples.
Encapsulate each JButton in a JPanel with a FlowLayout, and then add those FlowLayout JPanels to the rightPanel instead of the JButtons themselves. This will allow you to keep your evenly spaced buttons, but won't make them expand to take up the entire space that the parent container has available.
If you don't want them evenly spaced, but to be three consecutive buttons one after another top down, you can make the right panel have a BorderLayout, add a sub panel to the north area of the BorderLayout with the original GridLayout that the right panel had, and then add those FlowLayout panels containing the JButtons.
Can i combine Java layouts in same JPanel. I'm stuck with with placing my components on JPanel. It shoudl be like this: JLabel, JButton, JButton , JLabel and new line and same. I used BorderLayout but it wont go to the next row, keep adding components to same row and I need a new row. Ideal sit combined with cardlayout or some other good solution.
EDIT: Solved with GridLayout (0,4) It will do the job till i learn to use GridBaglayout. Thank you for trying to help me.
Yes you can combine layouts.
Using a JPanel you are able to embed other JPanels:
JPanel back = new JPanel(new BorderLayout());
JPanel rows = new JPabel(new GridLayout(3,3));
back.add(rows, BorderLayout.CENTER);
Without seeing your code though it's difficult to know exactly what you are trying to achieve!
Yes you can combine java layouts.
A common pattern I use is BorderLayout first on a frame. The central component expands out, while the other components shrink in. Inside these panels I might have a Flowlayout to show buttons evenly spaced horizontally on top.
Another common approach for forms is using a Gridbaglayout, then adding all the form elements at gridX and gridY positions. I then later can stretch and teak these cells using other constraints in the Gridbaglayout repetoire.
Can you add a screenshot so that we can see what you want to do?