java - JTextField keep filling the space of the JMenuBar -
im trying add buttons , textfields jmenubar , after set jtextfield's preferredsize, jtextfield keeps on filling space on jmenubar.
note: jmenubar added using method -
public static void setjpanelmenubar(jpanel parent, jpanel child, jmenubar menubar) { parent.removeall(); parent.setlayout(new borderlayout()); jrootpane root = new jrootpane(); parent.add(root, borderlayout.center); root.setjmenubar(menubar); root.getcontentpane().add(child); parent.putclientproperty("root", root); //if need later }
the code allows me add jmenubar in jpanel.
now on jmenubar code.
jmenubar x = jmenubar1; x.removeall(); jtextfield searchbar = txtsearch; jtextfield searchbar2 = new jtextfield(); searchbar2.setpreferredsize(new dimension(10,20)); x.add(lblsearch); x.add(searchbar); x.add(btnsearch); x.add(box.createhorizontalglue()); x.add(searchbar2);
note: jmenubar1
,lblsearch
,txtsearch
, , btnsearch
created using netbeans.
to test this, added jtextfield called searchbar2
, set preferred size both keeps on occupying space left in jmenubar after added box glue.
any ideas why?
from how use boxlayout tutorial:
when boxlayout lays out components top bottom, tries size each component @ component's preferred height. if vertical space of layout not match sum of preferred heights, boxlayout tries resize components fill space. components either grow or shrink fill space, boxlayout honoring minimum , maximum sizes of each of components. space appears @ bottom of container.
(emphasis mine)
obviously same applies widths in horizontal box. in opinion, above should in actual javadoc boxlayout, it's not.
in case, easiest solution remove explicit call setpreferredsize, , instead set each jtextfield's maximum size match preferred size:
searchbar.setmaximumsize(searchbar.getpreferredsize()); searchbar2.setmaximumsize(searchbar2.getpreferredsize());
boxlayout tries respect minimum , maximum sizes.
Comments
Post a Comment