Java GUI LayoutManagers
I'm busy with an asignment where i have to make a graphical interface for a simple program. But i'm strugling with the layout.
This is the idea:
What is the easiest way to accomplish such a layout?
And what method do you use to make layouts in java. Just code it, or use an IDE like netbeans?
Asked by: Vanessa638 | Posted: 23-01-2022
Answer 1
And what method do you use to make layouts in java. Just code it, or use an IDE like netbeans?
NetBeans for GUI developers is like a calculator for grade schoolers: you really shouldn't use it until you know how to do things without it, but then it will save you a lot of time.
(I'd love to answer your primary question, but the firewall I'm behind is blocking the picture.)
Answered by: Carina150 | Posted: 24-02-2022Answer 2
Well considering how simple the layout is I would suggest you use a BorderLayout with NORTH set to the top section in a container and the JTable in the CENTER of the BorderLayout. For the Top it appears to be a simple BorderLayout again with NORTH as the Instruction: south as the black box (possibly in a container with a FlowLayout). The center of the top pane appears to be 2 Containers of GridLayouts with 2 rows and 2 columns, so put thos in another container with a GirdLayout.
So in pseudo:
Container(BorderLayout)
{
@NORTH
Container(BorderLayout)
{
@NORTH
Label(Instruction);
@CENTER
Container(GridLayout(2,1))
{
Container(GirdLayout(2,2))
{
Label() TextField()
Label() TextField()
}
Container(GirdLayout(2,2))
{
Label() TextField()
Label() TextField()
}
}
@SOUTH
Container(FlowLayout())
{
JButton() //shaded thing?
}
}
@CENTER
{
JTable
}
}
Answered by: Kelsey845 | Posted: 24-02-2022
Answer 3
I build everything by hand. Like Christian, I've had bad experiences with GUI builders; they always either refused to configure a couple of components quite right, or they generated huge amounts of unnecessary code which made later maintenance impractical, or both.
I used to do build a lot of UIs using GridBagLayout, but for years, I've never seen an office-environment UI that couldn't be built with nested BorderLayouts, GridLayouts, and the occasional BoxLayout or FlowLayout. About 98% of the stuff I've seen is doable with nested BorderLayouts.
In your case, the layout organization will be as bmeck says. Speaking from memory, using CENTER for the JTable (remember to put it in a JScrollPane!) and NORTH for everything else ensures that if you resize your JFrame, the JTable will get all of the extra space, and that should be exactly what you want. For the top labels and fields, the nested GridLayouts should ensure that each "column" of labels and fields will take up equal horizontal space. (They'll get only enough vertical space to be completely visible, and no more, since the JTable is taking up everything else.)
Everything else is just a matter of adding borders and setting the GridLayout padding reasonably.
Answered by: Lana605 | Posted: 24-02-2022Answer 4
For myself gui-builders for swing or swt never worked that well that's why i code layouts myself using layout managers. Your question doesn't mention which gui-system you are using but i assume you want to use swing. If that's the case I would recommend to use GridBagLayout for your layout. It is not that easy to use in the beginning but as soon as you know how it works you can do most layouts in the way you want it to be and i think it is also the layoutmanager of choice for the layout you want to do.
Answered by: Elise469 | Posted: 24-02-2022Answer 5
I've used GUI layout generating tools for super rapid development (maybe get the first 2 or 3 iterations of an interface out of the way). I've ultimately found that using a simple fixed layout (no layout manager) with these tools is the best approach. Once we are starting to hone in on a design, we switch to manual layout.
Whenever I've tried to use GUI generators to create code for layout managers, I've almost always been bitten eventually where the layout would just stop working and I spent more time debugging the impossible to read auto-generated code than if I'd done the layout by hand anyway. For what it's worth, when we are doing the early phase of layouts, we use the Jigloo plugin for Eclipse. It's very inexpensive, and does a good job.
I'm a big fan of MiGLayout. I've found that it is incredibly easy to use for simple layouts, and is capable of doing extremely complicated layouts. All without the need to resort to nested panels, etc... JGoodies Forms is also good, but harder to use.
Answered by: Lenny786 | Posted: 24-02-2022Answer 6
I wrote an article a while back on layout managers:
http://developer.java.sun.com/developer/onlineTraining/GUI/AWTLayoutMgr
It describes how nesting (as bmeck above demonstrates) can be used very effectively for many UI designs.
Answered by: Blake419 | Posted: 24-02-2022Answer 7
Try table layout. Works great.
https://tablelayout.dev.java.net/
Answered by: Oliver369 | Posted: 24-02-2022Answer 8
Use GroupLayout
:)
All the alignments are pretty easy to do
Answered by: Roman158 | Posted: 24-02-2022Answer 9
I used to love Motif's XmForm for this sort of thing. In Java, I usually put Boxes inside of boxes. So I have a vertical box. First row of the box contains a JLabel for the Instruction. Second row contains something for the label/result stuff, possibly some sort of grid. Third row contains whatever that blacked out thing, Fourth row contains the JTable. Then I'd spend some time to try to figure out how to do the lable/result stuff. Then I'd probably end up saying "dammit", and doing it as a GridBagLayout.
Answered by: Kellan221 | Posted: 24-02-2022Similar questions
java - Are the Swing LayoutManagers adequate?
Closed. This question is opinion-based. It is not c...
java - custom Swing components that resize dynamically but interoperate with LayoutManagers
I have a subclassed JPanel that acts as a view for some information.
When the information changes, the min/max/preferred size of the JPanel may change as well.
How do I handle this event properly so that it plays nicely with a LayoutManager?
I tried overriding getPreferredSize() but it only seems to be called once.
swing - Java LayoutManagers with JPanels
I'm really struggling with creating a complicated layout. Here is a picture of what I want in the end:
(source: fb...
Still can't find your answer? Check out these amazing Java communities for help...
Java Reddit Community | Java Help Reddit Community | Dev.to Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)