Setting the position of GridBagLayout at the top of JPanel in Java Swing -
i have written following code add jlabel
jpanel
displays in center, while expected placed @ top of jpanel
.
here piece of code i'm referring to:
jpanel pnlprojects = new jpanel(); pnlprojects.setminimumsize(new dimension(10, 300)); gridbaglayout gridbaglayout = new gridbaglayout(); pnlprojects.setlayout(gridbaglayout); gridbagconstraints gridbagconstraints = new gridbagconstraints(); // add multiple label dynamically; (int count = 0; count < project.length; count++) { lblprojects[count] = new jlabel("project"+count ); lblprojects[count].sethorizontalalignment(swingconstants.left); lblprojects[count].sethorizontaltextposition(swingconstants.left); lblprojects[count].setborder(borderfactory.createbevelborder(0)); lblprojects[count].setpreferredsize(new dimension(100, 20)); gridbagconstraints.fill = gridbagconstraints.vertical; gridbagconstraints.gridx = 0; gridbagconstraints.gridy = count; pnlprojects.add(lblprojects[count], gridbagconstraints); } // add project panel in scorllpan jscrollpane jspprojectlist = new jscrollpane(pnlprojects);
would ablt explain me how change per requirement?
you can use next trick: add next code after loop:
gridbagconstraints.weighty=1; gridbagconstraints.gridy++; pnlprojects.add(new jlabel(" "), gridbagconstraints);
that dummy jlabel
grab space under project jlabel
's.
Comments
Post a Comment