java - Setting Margin / Inset for cells in Tablelayout in Swing -
is there anyway set margin cell in tablelayout
(for swing application) insets
in gridbaglayout
.
i need space between cells in table. 1 solution adding rows or columns needed space.
if solution applicable netbeans, more glad.
there 2 tablelayout
managers. clear thoughts' tablelayout
, more recent esoteric softwares' tablelayout
.
i provide simple example using both managers. put 6 buttons on window , add space between them.
clear thoughts' solution
package com.zetcode; import info.clearthought.layout.tablelayout; import java.awt.eventqueue; import javax.swing.borderfactory; import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jpanel; public class tablelayoutgaps2 extends jframe { public tablelayoutgaps2() { initui(); settitle("gaps"); setdefaultcloseoperation(jframe.exit_on_close); setlocationrelativeto(null); } private void initui() { jpanel base = new jpanel(); base.setborder( borderfactory.createemptyborder(5, 5, 5, 5) ); double cols[] = { tablelayout.preferred, tablelayout.preferred, tablelayout.preferred }; double rows[] = { tablelayout.preferred, tablelayout.preferred }; tablelayout layout = new tablelayout(cols, rows); layout.sethgap(5); layout.setvgap(5); base.setlayout(layout); base.add(new jbutton("button"), "0, 0"); base.add(new jbutton("button"), "1, 0"); base.add(new jbutton("button"), "2, 0"); base.add(new jbutton("button"), "0, 1"); base.add(new jbutton("button"), "1, 1"); base.add(new jbutton("button"), "2, 1"); add(base); pack(); } public static void main(string[] args) { eventqueue.invokelater(new runnable() { @override public void run() { tablelayoutgaps2 ex = new tablelayoutgaps2(); ex.setvisible(true); } }); } }
the gaps added using following methods:
layout.sethgap(5); layout.setvgap(5);
esoteric softwares's solution
package com.zetcode; import com.esotericsoftware.tablelayout.swing.table; import java.awt.eventqueue; import javax.swing.jbutton; import javax.swing.jframe; public class tablelayoutgaps3 extends jframe { public tablelayoutgaps3() { initui(); settitle("gaps"); setdefaultcloseoperation(jframe.exit_on_close); setlocationrelativeto(null); } private void initui() { table table = new table(); getcontentpane().add(table); table.addcell(new jbutton("button")).pad(5, 5, 2.5f, 2.5f); table.addcell(new jbutton("button")).pad(5, 2.5f, 2.5f, 2.5f); table.addcell(new jbutton("button")).pad(5, 2.5f, 2.5f, 5); table.row(); table.addcell(new jbutton("button")).pad(2.5f, 5, 5, 2.5f); table.addcell(new jbutton("button")).pad(2.5f, 2.5f, 5, 2.5f); table.addcell(new jbutton("button")).pad(2.5f, 2.5f, 5, 5); pack(); } public static void main(string[] args) { eventqueue.invokelater(new runnable() { @override public void run() { tablelayoutgaps3 ex = new tablelayoutgaps3(); ex.setvisible(true); } }); } }
the gaps between components added gap()
method.
however, cannot recommend of these 2 layout managers. instead use 1 of following layout managers:
- miglayout
- grouplayout
- formlayout
they more flexible , powerful. these managers cope way better many challanges of layout management.
Comments
Post a Comment