Instead of making use of a layout manager such as borderLayout, flowLayout, gridLayout etc I use an absolute layout to place the component into my container with the coordinate that I specify. The downside is that it requires a lot of testing because you need to make sure that the component you place are in the right position. The other “downside” is that the position of the components never change even when you are resizing the JFrame, JDialog etc.. it is possible to resize the components when resizing the container but that will be discussed another time.
Anyway, this is how to create an absolute layout in Swing:
First set the layout to null so you don’t use any layout managers.
setLayout(null);
After that, create an component that you want to be placed on your container.
input1 = new JTextField();
input1.setBounds(0,0, 99,20);
Add the component:
getContentPane().add(input1);
And finally, set the size of the container etc
setSize(210,120);
setLocationRelativeTo(null);
setVisible(true);
Yes there are better ways to do this but for me this is the easiest way to do it using absolute positioning. If you have a suggestion or a tip about creating a layout with Swing/AWT im happy to hear about it.
Bookmark this page:
These icons link to social bookmarking sites where readers can share and discover new web pages.