Reputation: 3759
I want to make a GUI and do it through code rather than dragging everything around like in Netbeans. I would like to, if possible, set the coordinates for each thing I am to insert as it will provide me with the precision I desire. I have tried out GridBagLayout and have not found it to be to my liking but perhaps I don't know enough about it to know what to do. Perhaps MigLayout is a good option for me but I'd like to know if there's anything better for what I need.
In Summary: I want to make a GUI where I can directly specific coordinates of each object added. Which Layout do I use?
Upvotes: 2
Views: 773
Reputation: 72636
Take a look at CoordinateLayout :
Allows laying out components based on absolute positions/sizes that are adapted based on available space for the layout.
Upvotes: 2
Reputation: 8205
Specyfing coordinatates (i.e. using the NullLayout
) is always a bad idea. The LayoutManager
classes should always be used to handle your component placement, as it will handle all types of events you should not be handling (such as computing new coordinates when the container is resized).
I personally prefer to use a GridBagLayout
, as it allows you to specify all types of constraints (how to distribute free space, how much space a component will use, the insets, etc.). This tutorial will help you get started on the GridBagLayout
.
Other LayoutManager
exist, each having strength and weaknesses (e.g. Flow Layout
, MigLayout
, BoxLayout
). Depending on your needs, some managers might be too simple, or overkill. You should try and learn many of them so you can find the proper one for your needs.
Upvotes: 1
Reputation: 4597
Do both. Try WindowBuilder in Eclipse. It provides two-way design and code generation.
Upvotes: 1
Reputation: 35594
For each panel you can/should have different layout managers, according to the layout you are going to have inside. There is no "universal" layout manager, and especially when handcoding, it's elegant and useful to use the "most appropriate" layout manager for the task you are solving. For example, the otherwise very advanced GridBagLayout cannot emulate WrapLayout.
You may find this page useful as a list of hints for proper layout manager selection.
Note however that each layout manager has its quirks in Swing, so switching from one layout manager to another will possibly bring you some small troubles. For example, some of them respect Minimum/MaximumSize, some don't, etc.
To your summary: you shouldn't try to set the coordinates "manually". While this is possible, it's much better to express the relation between the visual elements in a logical way. This will help you a lot for making your page resizable, for addressing localization, for proper nesting and extracting controls, and at many other places which I don't remember at the moment.
Upvotes: 5