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.
Related
I'm trying to put a Panel filled with a couple of small components into the corner of my frame, but I'm not sure how to get it there. I'm guessing that I need to set a certain layout on my frame that allows me to reference that section of it somehow? I haven't found much help searching online. It would be great if someone could send me in the right direction.
Try javax.swing.OverlayLayout, shown here.
use gridbag layout on frame to put your panel on corner or any area depending on remaining elemnts to be placed on frame....
Or
You can set layout of frame to null
Frame.setLayout(null)
And use setbounds method on panel and add on frame....
I would appreciate, If anyone here is kind enough to recommend me what are the layout managers to choose
and how to separate the attached GUI into Jpanels.
Thanks so much in advance.
What I have tried so far:
Jframe - BorderLayout
Map (the grid and clear button from another class extending jpanel) - so I have put it in jframe.CENTER
the buttons to the right: jframe.EAST
I put them in a jpanel in a gridlayout (but I cannot get the spacing between the components)
buttons at the bottom: jframe.SOUTH
I put them in a jpanel in a gridlayout (but I cannot get the spacing between the components)
When trying to determine what layout(s) you should use, you should start by trying to determine areas of responsibility...
For example...
Based on your needs, I might start with a GridBagLayout. This might seem complex, but if you break the UI down into seperate components, focusing on their individual needs, it should become simpler...
For the panel on the left...
I would be temptered to use a GridBagLayout, simply because it allows the components to use there preferred sizes, but still allows you to set up a grid like pattern...
For the arrow buttons...
This becomes a little more complicated, but I would use a GridLayout(2, 3) (2 rows, 3 columns). This will require to add a filler panel at the first and third position along the top row, but still maintain the buttons at a equal size...
For this panel...
I would be tempted to either use a GridBagLayout, because it will allow you to span the rows or even split it again into two separate panels, with the controls on the left in a GridLayout(2, 1) and the control on the right in something like a BorderLayout as required...
For "progress" panel...
I would be tempted to use...GridBagLayout. Mostly because it would allow you to provide more weight to the progress bars then the labels.
For the main panel...
I would probably be tempted to either use a BorderLayout, with the Clear Map on another panel of it's own, allowing it maintain it's preferred size, in the NORTH position and the map panel in the CENTER or even a GridBagLayout depending on what the invidual components are...
I create some panel with a lot of components as images text ... . Now I have one problem. Customers tell me that panel is too small. Is there some option to enlarge this whole panel or I have to enlarged components one by one ?
UPDATE:
from this:
to this:
everything depends of used LayoutManager,
Is there some option to enlarge this whole panel or I have to enlarged
components one by one
-> no, basically this is reason why LayoutManager exists there, most of them there are created especially for this reasons, GridLayout, GridBagLayout, BoxLayout (maybe without glue), BorderLayout and todays MigLayout are best way how to do it
it depends, if you have a GridLayout or some Layout witch set all the components with some order, for example, all the components at the same distance or all the components like a Matrix, if you enlarge the panel, all the components will enlarge with it. If not, you have to enlarge by hand.
I'm fairly new to Java and I'm trying to create a GUI application with some labels, buttons, and textfields. The program is pretty simple and I just wanted to use a default layout, which is FlowLayout. I managed to place and size everything fine, but the only thing seem to be not working is the alignment. I want to place buttons and textfields with certain alignments, but whenever I set an alignment, it moves the text inside of whatever the object rather than the object itself. For example, I wrote:
button.setHorizontalAlignment(JButton.RIGHT);
but it seems like it aligns the text inside the button instead of the button itself.
Is there any way to align the button itself rather than the text inside of it?
I know the alignment stuff could be easier with some other type of layout (e.g. BoxLayout), but I just want to use the FlowLayout for this one, unless it is impossible to align them using the FlowLayout (which I don't think so).
Thanks in advance.
See the constructor FlowLayout(int align).
Constructs a new FlowLayout with the specified alignment and a default 5-unit horizontal and vertical gap. The value of the alignment argument must be one of FlowLayout.LEFT, FlowLayout.RIGHT, FlowLayout.CENTER, FlowLayout.LEADING, or FlowLayout.TRAILING.
It seems you are after a FlowLayout.RIGHT as seen in this answer (the combo and check box at the top).
I don't think you can do this with a FlowLayout alone.
My suggestions would be:
Consider switching to MigLayout which is a much more powerful layout mechanism. MigLayout basically lets you position you components within a flexible grid, and you can set the specific alignment of a component within each grid cell.
When you want alignment of subcomponents, it also often makes sense to put them inside a nested JPanel. You can then use a separate layout for this JPanel (BorderLayout perhaps?) which will enable you to get the exact alignment that you want.
setHorizontalAlignment of AbstractButton sets the horizontal alignment of the icon and text not the position of the button. AbstractButton's default is SwingConstants.CENTER.
If you want to align the button..set the position while adding it to the panel or frame..something like this....
p.add(button, BorderLayout.SOUTH);//using `BorderLayout`
Flow layouts are typically used to arrange buttons in a panel. It will arrange buttons left to right until no more buttons fit on the same line.
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.