The most specific type of layout is absolute positioning.
Warning: Absolute positioning should be used rarely, if ever. There are many reasons for this. Here is one of them: Absolute positioning (without layout manager) and absolute positioning in MiGlayout
- Thanks to brimborium for the good idea of ββadding a warning.
As said, here's how to use absolute positioning:
In the above code, instead of setting topPanel
layout to FlowLayout
set it to null
.
topPanel.setLayout(null);
Later in the code, before loading the components in topPanel
call the setBounds
container:
someJComponent.setBounds(x-coord, y-coord, width, height);
So, for example, you created an instance of JComboBox()
and named it roomTypeCombo
, the following code shows how to absolutely position roomTypeCombo
.
topPanel.setLayout(null);
The setBounds
method used above has four parameters:
int x-coord
- set the coordinate of roomTypeCombo
x relative to the parent, topPanel
.
int y-coord
- set roomTypeCombo
y-coordinate relative to the parent, topPanel
.
int width, int height
- The last two parameters determine the width and height of roomTypeCombo
, which is pretty clear.
I would just play around with the coordinates and see if you like something that comes out of it, the worst thing that can happen is to return to using the layout, which is probably better than absolute positioning. Or you can implement your own layout manager if you follow this hyperlink, the first answer talks about implementing your own layout manager and useful links.
Sorry if none of what I wrote to you helped anyway.
Additional information on absolute positioning