java - GridBagLayout aligning labels -
i have been messing around gridbaglayout , having little bit of trouble. first trying labels start in upper left accomplished with:
jpanel right = new jpanel(new gridbaglayout()); right.setpreferredsize(new dimension(333,600)); gridbagconstraints gbc = new gridbagconstraints(); gbc.anchor = gridbagconstraints.northwest; jlabel testlabel = new jlabel(); testlabel.settext("test"); gbc.gridx = 0; gbc.gridy = 0; gbc.weighty = 1; gbc.weightx = 1; right.add(testlabel, gbc);
now want add label directly under one:
jlabel test2label = new jlabel(); test2label.settext("test2"); gbc.gridx = 0; gbc.gridy = 1; gbc.weighty = 1; gbc.weightx = 1; right.add(test2label, gbc);
instead of putting directly under first 1 places halfway down panel. because of weighty , weightx think, if change these not place labels in upper left. how can make work? sorry if unclear english not best language let me know if need me clarify. -thanks
if understood correctly, following code should answer. weightx , weighty decide how distribute free space in component child components - bigger value, more space (read more here: weightx , weighty in java gridbaglayout) if left components value zero, centred.
so in case, best solution giving whole left space second component.
gridbagconstraints gbc = new gridbagconstraints(); gbc.anchor = gridbagconstraints.northwest; jlabel testlabel = new jlabel(); testlabel.settext("test"); gbc.gridx = 0; gbc.gridy = 0; gbc.weighty = 0; gbc.weightx = 0; right.add(testlabel, gbc); jlabel test2label = new jlabel(); test2label.settext("test2"); gbc.gridx = 0; gbc.gridy = 1; gbc.weighty = 1; gbc.weightx = 1; right.add(test2label, gbc);
Comments
Post a Comment