java - Close Gps on BoxLayout -
i have created setup of buttons using box.
the problem there gaps between buttons.
below mcve version of code. want accomplish buttons "one" , "two" touching side side, no gap, , buttons "one , "one" touching top bottom no gap, , go on throughout setup.
i have read glue , have tried utilize it, have not been able work out. not able utilize layout other box not fit in rest of project.
public class customers { public static jframe frame = new jframe(); public static void framegui(jpanel panel, string name){ frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setcontentpane(panel); frame.setsize(1200,500); frame.setvisible(true); } public static void scrollcustomersgui(){ box box = box.createverticalbox(); box.add(customerstableheadings(box)); box.add(customerstableheadings(box)); box.add(customerstableheadings(box)); box.add(customerstableheadings(box)); jscrollpane scroll = new jscrollpane(box); jpanel = new jpanel(new borderlayout()); all.add(scroll); framegui(all, "customers"); } public static jpanel customerstableheadings(box panel){ font font = new font("courier", font.bold,12); jpanel customerstable = new jpanel(); jbutton custid = new jbutton("one"); jbutton surname = new jbutton("two"); customerstable.add(custid); customerstable.add(surname); homecoming customerstable; } }
boxlayout
designed distribute unused space among components; struts, glue , filler won't alter this. can utilize approach suggested here , here alter preferred size of enclosing scroll pane. more generally, can implement scrollable
interface. in addition, swing gui objects should constructed , manipulated only on event dispatch thread.
import java.awt.dimension; import java.awt.eventqueue; import javax.swing.box; import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.jscrollpane; /** @se http://stackoverflow.com/a/26829171/230513 */ public class customers { private static final int n = 16; private void display() { box box = box.createverticalbox(); (int = 0; < n; i++) { box.add(customerstableheadings()); } jframe frame = new jframe(); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.add(new jscrollpane(box) { int w = box.getpreferredsize().width; int h = box.getpreferredsize().height; @override public dimension getpreferredsize() { homecoming new dimension(9 * w / 8, h / 3); } }); frame.pack(); frame.setlocationrelativeto(null); frame.setvisible(true); } private jpanel customerstableheadings() { jpanel customerstable = new jpanel(); jbutton custid = new jbutton("one"); jbutton surname = new jbutton("two"); customerstable.add(custid); customerstable.add(surname); homecoming customerstable; } public static void main(string[] args) { eventqueue.invokelater(() -> { new customers().display(); }); } }
java swing layout-manager boxlayout
No comments:
Post a Comment