java - Grouplayout is a bit messed up -
there problem in grouplayout of following code, kindly me figure out
i have uploaded output file, not sure if problem alignment. kingly have , me figure out.
package javaapplication1; import java.awt.borderlayout; import java.awt.gridlayout; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.net.inetaddress; import javax.swing.*; import javax.swing.grouplayout.alignment; import javax.swing.jcheckbox; import javax.swing.jcomponent; import javax.swing.jframe; public class progress extends jframe implements actionlistener { static jframe frame; public jlabel clientip; jtextfield ip; jlabel clientpassword; jtextfield pass; jlabel videoname; jtextfield vname; jlabel perccomplete; jtextfield percent; jlabel packetssent; jtextfield pacsent; jlabel connectiontype; jtextfield conntype; jlabel noofvideossent; jtextfield videosend; jbutton disconnect; jbutton refresh; jbutton ok; public progress() { clientip = new jlabel("client's ip:"); clientpassword = new jlabel("clients password:"); videoname = new jlabel("video being transfered:"); perccomplete = new jlabel("% of transfer complete:"); packetssent = new jlabel("no of packets sent:"); connectiontype = new jlabel("connection type:"); noofvideossent = new jlabel("no of videos sent:"); ip = new jtextfield(ipad); ip.setcolumns(20); pass = new jtextfield(ipass); pass.setcolumns(20); vname = new jtextfield(iselvid); vname.setcolumns(20); percent = new jtextfield("10%"); percent.setcolumns(20); pacsent = new jtextfield(string.valueof(rtspseqnb)); pacsent.setcolumns(20); string c; if (clientconnected == true) { c = "tcp"; } else { c = "not connected"; } conntype = new jtextfield(c); conntype.setcolumns(20); videosend = new jtextfield(string.valueof(videocount)); videosend.setcolumns(20); //tell accessibility tools label/textfield pairs. clientip.setlabelfor(ip); clientpassword.setlabelfor(pass); videoname.setlabelfor(vname); perccomplete.setlabelfor(percent); packetssent.setlabelfor(pacsent); connectiontype.setlabelfor(conntype); noofvideossent.setlabelfor(videosend); //lay out labels in panel. grouplayout layout = new grouplayout(getcontentpane()); getcontentpane().setlayout(layout); layout.setautocreategaps(true); layout.setautocreatecontainergaps(true); // turn on automatically creating gaps between components touch // edge of container , container. layout.setautocreatecontainergaps(true); grouplayout.sequentialgroup hgroup = layout.createsequentialgroup(); hgroup.addgroup(layout.createparallelgroup(alignment.leading). addcomponent(clientip).addcomponent(clientpassword).addcomponent(videoname).addcomponent(perccomplete).addcomponent(packetssent).addcomponent(connectiontype).addcomponent(noofvideossent)); hgroup.addgroup(layout.createparallelgroup(alignment.leading). addcomponent(ip).addcomponent(pass).addcomponent(vname).addcomponent(percent). addcomponent(pacsent).addcomponent(conntype).addcomponent(videosend)); layout.sethorizontalgroup(hgroup); grouplayout.sequentialgroup vgroup = layout.createsequentialgroup(); vgroup.addgroup(layout.createparallelgroup(alignment.leading).addcomponent(clientip)).addgroup(layout.createparallelgroup(alignment.leading).addcomponent(clientpassword)).addgroup(layout.createparallelgroup(alignment.leading).addcomponent(videoname)).addgroup(layout.createparallelgroup(alignment.leading).addcomponent(perccomplete)).addgroup(layout.createparallelgroup(alignment.leading).addcomponent(packetssent)).addgroup(layout.createparallelgroup(alignment.leading).addcomponent(connectiontype)).addgroup(layout.createparallelgroup(alignment.leading).addcomponent(noofvideossent)); vgroup.addgroup(layout.createparallelgroup(alignment.leading).addcomponent(ip)).addgroup(layout.createparallelgroup(alignment.leading).addcomponent(pass)).addgroup(layout.createparallelgroup(alignment.leading).addcomponent(vname)).addgroup(layout.createparallelgroup(alignment.leading).addcomponent(percent)).addgroup(layout.createparallelgroup(alignment.leading).addcomponent(pacsent)).addgroup(layout.createparallelgroup(alignment.leading).addcomponent(conntype)).addgroup(layout.createparallelgroup(alignment.leading).addcomponent(videosend)); layout.setverticalgroup(vgroup); // jpanel buttonpane = new jpanel(new gridlayout(0,1)); // disconnect = new jbutton("disconnect client"); // disconnect.setactioncommand("disconnect"); // disconnect.addactionlistener(this); // refresh = new jbutton("refresh details"); // refresh.setactioncommand("refresh"); // refresh.addactionlistener(this); // ok = new jbutton("ok"); // ok.setactioncommand("ok"); // ok.addactionlistener(this); // buttonpane.add(refresh); // buttonpane.add(disconnect); // buttonpane.add(ok); // add(buttonpane, borderlayout.after_last_line); settitle("find"); pack(); } private static void createandshowgui() { //create , set window. //frame = new jframe("connected client's details"); //frame.setdefaultcloseoperation(jframe.exit_on_close); new progress().setvisible(true); //add contents window. // frame.add(new progress()); // //display window. // frame.pack(); // frame.setvisible(true); } public static void main(string args[]) { //schedule job event dispatch thread: //creating , showing application's gui. swingutilities.invokelater(new runnable() { public void run() { //turn off metal's use of bold fonts uimanager.put("swing.boldmetal", boolean.false); createandshowgui(); } }); } public void actionperformed(actionevent e) { if ("disconnect".equalsignorecase(e.getactioncommand())) { clientipaddr = null; joptionpane.showmessagedialog(frame, "eggs not supposed green."); } if ("refresh".equalsignorecase(e.getactioncommand())) { string ipad = null, ipass, iselvid; if (clientipaddr == null || clientipaddr.equals("")) { ipad = "not connected"; } else { ipad = clientipaddr.tostring(); } if (vfilepassword == null || vfilepassword.equals("")) { ipass = "no password"; } else { ipass = vfilepassword; } if (selected_video == null || selected_video.equals("")) { iselvid = "not selected"; } else { iselvid = selected_video; } ip.settext(ipad); pass.settext(ipass); vname.settext(iselvid); percent.settext("10%"); pacsent.settext(string.valueof(rtspseqnb)); string c; if (clientconnected == true) { c = "tcp"; } else { c = "not connected"; } conntype.settext(c); videosend.settext(string.valueof(videocount)); } if ("ok".equalsignorecase(e.getactioncommand())) { frame.dispose(); } } }
grouplayout
constraints-based layout. base principle determine positionning constraints between components, on x , y axis independently.
there 2 constraints : either components in sequence, or in parallel.
in case, layout simple :
- on x axis, there labels in parallel, , textfields in parallel.
- on y axis, there groups of [label , textfield], in parallel
the code following :
import javax.swing.grouplayout; import javax.swing.grouplayout.alignment; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.jpanel; import javax.swing.jtextfield; import javax.swing.swingutilities; import javax.swing.uimanager; public class progress extends jpanel { private jlabel clientip; private jtextfield ip; private jlabel clientpassword; private jtextfield pass; private jlabel videoname; private jtextfield vname; private jlabel perccomplete; private jtextfield percent; private jlabel packetssent; private jtextfield pacsent; private jlabel connectiontype; private jtextfield conntype; private jlabel noofvideossent; private jtextfield videosend; public progress() { clientip = new jlabel("client's ip:"); clientpassword = new jlabel("clients password:"); videoname = new jlabel("video being transfered:"); perccomplete = new jlabel("% of transfer complete:"); packetssent = new jlabel("no of packets sent:"); connectiontype = new jlabel("connection type:"); noofvideossent = new jlabel("no of videos sent:"); ip = new jtextfield(); pass = new jtextfield(); vname = new jtextfield(); percent = new jtextfield(); pacsent = new jtextfield(); conntype = new jtextfield(); videosend = new jtextfield(); ip.setcolumns(20); pass.setcolumns(20); vname.setcolumns(20); percent.setcolumns(20); pacsent.setcolumns(20); conntype.setcolumns(20); videosend.setcolumns(20); //tell accessibility tools label/textfield pairs. clientip.setlabelfor(ip); clientpassword.setlabelfor(pass); videoname.setlabelfor(vname); perccomplete.setlabelfor(percent); packetssent.setlabelfor(pacsent); connectiontype.setlabelfor(conntype); noofvideossent.setlabelfor(videosend); grouplayout layout = new grouplayout(this); setlayout(layout); layout.setautocreategaps(true); layout.setautocreatecontainergaps(true); layout.sethorizontalgroup( layout.createsequentialgroup() .addgroup(layout.createparallelgroup(alignment.leading) .addcomponent(clientip) .addcomponent(clientpassword) .addcomponent(videoname) .addcomponent(perccomplete) .addcomponent(packetssent) .addcomponent(connectiontype) .addcomponent(noofvideossent)) .addgroup(layout.createparallelgroup(alignment.leading) .addcomponent(ip) .addcomponent(pass) .addcomponent(vname) .addcomponent(percent) .addcomponent(pacsent) .addcomponent(conntype) .addcomponent(videosend))); layout.setverticalgroup( layout.createsequentialgroup() .addgroup(layout.createparallelgroup(alignment.baseline) .addcomponent(clientip) .addcomponent(ip)) .addgroup(layout.createparallelgroup(alignment.baseline) .addcomponent(clientpassword) .addcomponent(pass)) .addgroup(layout.createparallelgroup(alignment.baseline) .addcomponent(videoname) .addcomponent(vname)) .addgroup(layout.createparallelgroup(alignment.baseline) .addcomponent(perccomplete) .addcomponent(percent)) .addgroup(layout.createparallelgroup(alignment.baseline) .addcomponent(packetssent) .addcomponent(pacsent)) .addgroup(layout.createparallelgroup(alignment.baseline) .addcomponent(connectiontype) .addcomponent(conntype)) .addgroup(layout.createparallelgroup(alignment.baseline) .addcomponent(noofvideossent) .addcomponent(videosend))); } public static void main(string[] args) throws exception { swingutilities.invokelater(new runnable() { public void run() { uimanager.put("swing.boldmetal", boolean.false); jframe f = new jframe("test frame"); f.setcontentpane(new progress()); f.setdefaultcloseoperation(jframe.exit_on_close); f.settitle("find"); f.pack(); f.setvisible(true); } }); } }
a grouplayout
relatively easy understand, when code indented. replaced alignement.leading
alignement.baseline
, in order align on baseline (the text of labels , of textfields on same line).
Comments
Post a Comment