swing - java boxlayout space after adding jpanel -
i have container jpanel in used boxlayout manager , add panels inside in added panel contains label , textfield using flowlayout manager. everytime add panel inside creates annoying big space after added panel. want reduce spacing of panels have tried using setsize , setpreferredsize method adjust it. here code:
jpanel global = new jpanel(); global.setlayout(new boxlayout(global, boxlayout.y_axis)); global.setpreferredsize(new dimension(500,312)); global.setsize(500,312); global.setbounds(8, 5, 500, 312); global.setborder(borderfactory.createlineborder(color.black)); global.setbackground(color.white); //elements of global jlabel label1 = new jlabel("global settings"); label1.setalignmentx(component.center_alignment); label1.setfont(new font("tahoma", font.bold, 17)); global.add(label1); global.add(new jseparator()); //name field jpanel c = new jpanel(); c.setsize(100, 1); c.setpreferredsize(new dimension(100,1)); c.setlayout(new flowlayout()); jlabel label = new jlabel("display name"); jtextfield text = new jtextfield(20); text.setpreferredsize(new dimension(20,25)); c.add(label); c.add(text); global.add(c); //hostname field jpanel c1 = new jpanel(); c1.setsize(100, 1); c1.setpreferredsize(new dimension(100,1)); c1.setlayout(new flowlayout()); jlabel label2 = new jlabel("host name"); jtextfield text1 = new jtextfield(20); text1.setpreferredsize(new dimension(20,25)); c1.add(label2); c1.add(text1); global.add(c1);
boxlayout
pretty aggressive layoutmanager
, doesn't honour preferred size of components within it. instead, must set maximum size of boxlayout
components prevent them being stretched. additionally, need add box
via box.createverticalglue()
- special component gets stretched (rather other components).
here rewritten code:
jpanel global = new jpanel(); global.setlayout(new boxlayout(global, boxlayout.y_axis)); global.setpreferredsize(new dimension(500, 312)); global.setsize(500, 312); global.setbounds(8, 5, 500, 312); global.setborder(borderfactory.createlineborder(color.black)); global.setbackground(color.white); // elements of global jlabel label1 = new jlabel("global settings"); label1.setalignmentx(component.center_alignment); label1.setfont(new font("tahoma", font.bold, 17)); global.add(label1); jseparator sep = new jseparator(); sep.setmaximumsize(new dimension((int) sep.getmaximumsize().getwidth(), 50)); global.add(sep); // name field jpanel c = new jpanel(); c.setmaximumsize(new dimension((int) c.getmaximumsize().getwidth(), 50)); jlabel label = new jlabel("display name"); jtextfield text = new jtextfield(20); text.setpreferredsize(new dimension(20, 25)); c.add(label); c.add(text); global.add(c); // hostname field jpanel c1 = new jpanel(); c1.setmaximumsize(new dimension((int) c1.getmaximumsize().getwidth(), 50)); jlabel label2 = new jlabel("host name"); jtextfield text1 = new jtextfield(20); text1.setpreferredsize(new dimension(20, 25)); c1.add(label2); c1.add(text1); global.add(c1); global.add(box.createverticalglue());
Comments
Post a Comment