Bind JPanel side with another JPanel Center - java

Hi I am building a UI for a project and I have a JPanel which I want to divide into two equal parts. I am trying to find a way to add two small JPanel inside the big one but I don't know how to bind one side of a JPanel with the center of a bigger one. Any idea ?

I am trying to find a way to add two small JPanel inside the big one
Use a GridLayout on the big panel. The layout manager will automatically make both panels the same size. Read the section from the Swing tutorial on How to Use GridLayout.

Related

Two JPanels in one JFrame - the second overrides the first

I am trying to create a GUI where I add two JPanels to one JFrame, but the second JPanel I add override the first. In my first JPanel I have a sudoku box, and in the second I want a button. But, since the first one I add always overrides the second, this doesn't work.
My sudoku JPanel uses GridLayout, and this alone works perfectly. The problem is when I try to add the second JPanel (which has a JButton). Since the button needs to be another size than the squares in the sudoku box, I can't add this button to the first JPanel.
Is it possible to solve this using two JPanels, or do I need a different layout? I have read some about GridBagLayout and think this might be a solution, but it's a bit boring to change the whole code for my JPanel which has the sudoku in it.
You could just make another JPanel with an appropriate layout manager, and add the two smaller panels inside it. Having panels inside of panels is a good way to break up your layout into less complex pieces while maintaining better control of resize behavior, etc.

Java GUI place layout under boxlayout

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.

Placing components on Jpanel

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?

Simple java layout question

I have a somewhat simple question, but I have been looking for a solution and not finding it, since I am having some time problems I will ask here
I am programming a simple game in java(since I am still learning), and in the using a borderlayout, since it fits perfectly what I want, 3 buttons on the bottom and a internalframe in the north with the game screen, however I also require a toolbar on the top,is there anyway I can change the layout so it allows me to do this, or a any simple workaround? This type of layout is perfect for what I want and the others ones don't really please me.
I know this is probably a silly question but any help would be welcome
What if you add a JPanel that contains two other panels: one for your buttons and one for the content you want above north? Or you can similarly split up other regions by putting a panel that contains other panels or components.
Edit: You can put your toolbar in the north and the rest of your components can sit in a new panel which can be placed in the center.
If I am not mistaken, you may add a menu to a JFrame and still have the borderlayout be in the frame.
The menu would be above the top internal panel and the three buttons (having to be in a panel themselves) could be put into the south section of the frame.
All of this should work without any problems.
Just wondering, how bug is the internal panel? Is it the entire rest of the screen or only the top sliver? If its the rest of the screen, then put it in center, and for a sliver, put it in the north section.

Help with setting up panels in Java

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.

Categories