java - GridBagLayout fix space set by weight x/y -
i have frame following layout of 3 jpanels.
---------------------------- | | | | l | | | e | //toppanel | | f | | | t |----------------------| | p | | | n | //bottompanel | | l | | | | | ----------------------------
i achieved use of gridbaglayout
this code layout
public class uitests extends jframe{ public uitests(){ setlayout(new gridbaglayout()); //the 3 main panels jpanel leftpanel = new jpanel(); jpanel toppanel = new jpanel(); jpanel bottompanel = new jpanel(); leftpanel.setbackground(color.yellow); toppanel.setbackground(color.green); bottompanel.setbackground(color.pink); //position 3 main panels gridbagconstraints gbc = new gridbagconstraints(); gbc.gridx = 0; gbc.gridy = 0; gbc.gridheight = 2; gbc.gridwidth = 1; gbc.fill = gbc.both; gbc.weightx = .2; gbc.weighty = 1; getcontentpane().add(leftpanel, gbc); gbc = new gridbagconstraints(); gbc.gridx = 1; gbc.gridy = 0; gbc.gridheight = 1; gbc.gridwidth = 1; gbc.fill = gbc.both; gbc.weightx = .8; gbc.weighty = .5; getcontentpane().add(toppanel, gbc); gbc = new gridbagconstraints(); gbc.gridx = 1; gbc.gridy = 1; gbc.gridheight = 1; gbc.gridwidth = 1; gbc.fill = gbc.both; gbc.weightx = .8; gbc.weighty = .5; getcontentpane().add(bottompanel, gbc); setsize(600,600); setlocationrelativeto(null); setdefaultcloseoperation(jframe.exit_on_close); setvisible(true); } public static void main(string[] args){ new uitests(); } }
problem is, when add jtable
in top panel, bottom panel's height shrinked, of height taken top panel. here code table in top panel , added after setting background of 3 main panels.
object[][] contents = {{"haha","hehe", "hihi", "hoho", "huhu"}, {"haha","hehe", "hihi", "hoho", "huhu"}, {"haha","hehe", "hihi", "hoho", "huhu"}, {"haha","hehe", "hihi", "hoho", "huhu"}}; object[] heads = {"haha head", "hehe head", "hihi head", "hoho head", "huhu head"}; jtable table = new jtable(contents, heads); jscrollpane scrolltable = new jscrollpane(table); //add components toppanel gridbagconstraints gbc = new gridbagconstraints(); gbc.gridx = 0; gbc.gridy = 0; gbc.gridheight = 1; gbc.gridwidth = 1; gbc.weightx = 1; gbc.weighty = 1; gbc.fill = gbc.both; toppanel.setlayout(new gridbaglayout()); toppanel.add(scrolltable, gbc);
the frame like
---------------------------- | | | | l | | | e | //toppanel | | f | //new table created | | t | | | p | | | n | | | l |----------------------| | | //bottompanel | ----------------------------
so, how can make space created 'weightx' or 'weighty' fixed?
weightx/weighty
used distribute space. in fact container asks children preferred size , if it's smaller available pixels distributed according weights.
so can define preferred size of jscrollpane
holds jtable
Comments
Post a Comment