swing - Prevent vertical spacing when resizing a Java GUI -
i have simple gui , trying create buttons , controls left side of window. right side has text area display content. left side contains buttons , controls user manipulate. have used collection of layout managers (and seem considerably picky) make have now.
i've looked on oracle's documentation on boxlayout left controls' container using, , don't see way prevent buttons spacing apart when window resized. i'd them smashed @ top , stay there without spacing out. boxlayout's 'glue' feature doesn't think does, should called rubber band.
my question is, how keep content on left separating wider , wider screen gets resized?
my gui:
public class testcode extends jframe{ jtextarea textarea = new jtextarea (); jcombobox <string> typecombobox; jtextfield searchfield; jtextfield filefield; public testcode() { system.out.println ("in constructor"); settitle ("gui test"); setsize (600, 300); setdefaultcloseoperation (jframe.exit_on_close); setvisible (true); jscrollpane scrollpane = new jscrollpane(textarea); add(scrollpane, borderlayout.center); jbutton readbutton = new jbutton("read file"); jbutton displaybutton = new jbutton("display"); jbutton searchbutton = new jbutton("search"); searchfield = new jtextfield(10); filefield = new jtextfield(15); typecombobox = new jcombobox <string> (); typecombobox.additem("index"); typecombobox.additem("type"); typecombobox.additem("name"); jpanel container = new jpanel(); container.setlayout(new boxlayout(container, boxlayout.y_axis)); jpanel filepanel = new jpanel(); filepanel.setlayout(new flowlayout(flowlayout.left)); filepanel.add(new jlabel("source file:", swingconstants.right)); filepanel.add(filefield); filepanel.add(readbutton); jpanel displaypanel = new jpanel(); displaypanel.setlayout(new flowlayout(flowlayout.left)); displaypanel.add(new jlabel("display data:", swingconstants.right)); displaypanel.add(displaybutton); jpanel searchpanel = new jpanel(); searchpanel.setlayout(new flowlayout(flowlayout.left)); searchpanel.add(new jlabel ("search target")); searchpanel.add(box.createhorizontalbox()); searchpanel.add(searchfield); searchpanel.add(typecombobox); searchpanel.add(box.createhorizontalbox()); searchpanel.add(searchbutton); container.add(filepanel); container.add(displaypanel); container.add(searchpanel); add(container, borderlayout.west); validate(); }
boxlayout uses preferredsize minumimu , maximum sizes to layout. in case panel grow preferred size maximum size more space available. prevent happening can do:
filepanel.setmaximumsize( filepanel.getpreferredsize() ); ... displaypanel.setmaximumsize( displaypanel.getpreferredsize() ); ... searchpanel.setmaximumsize( searchpanel.getpreferredsize() );
although better solution override getmaximumsize()
of each panel return getpreferredsize()
. never when might use application in different laf in case preferred size of each panel change.
Comments
Post a Comment