java - How to fix gap in GridBagLayout -


i using gridbaglayout create jpanel, called 'preset' gets replicated several times in jframe. each preset have multiple rows (jpanels). goal 1 line (the first) show up, when edit button clicked show. right now, edit button works, there massive space between lines. want when lines collapsed, each preset directly above each other (no space). can see in following picture talking about.

this how looks: enter image description here

this how want look: enter image description here

i need gridbag don't know what. have read several tutorials , have written thought should be, no luck. in advanced.


package sscce;  import java.awt.borderlayout; import java.awt.component; import java.awt.dimension; import java.awt.flowlayout; import java.awt.gridbagconstraints; import java.awt.gridbaglayout; import java.awt.gridlayout; import java.awt.toolkit; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.io.ioexception; import javax.swing.jbutton; import javax.swing.jcombobox; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.jpanel; import javax.swing.jscrollpane; import javax.swing.jtextfield;  public class ui extends jframe{     private final static int height = 600;     private final static int width = 730;     private jpanel pane; //pane stores accounts     private jscrollpane scroller;     private preset[] branches;          public static void main(string[] args) {         jframe frame = new ui();     }          public ui(){         //make ui close when exit button clicked         this.setdefaultcloseoperation(jframe.exit_on_close);              //sets size of ui         this.setsize(width, height);          //set layout , add components         this.setlayout(new borderlayout());          //reads in settings , sets branches         populatebranches();          pane = new jpanel();         pane.setlayout(new gridlayout(branches.length,1));         (int = 0; < branches.length; i++){             pane.add(branches[i]);         }          //create center pane of borderlayout         scroller = new jscrollpane(pane);         scroller.createverticalscrollbar();         this.add(scroller,borderlayout.center);          //makes ui visible         this.setvisible(true);     }      private void populatebranches(){         //populates branches array based on read in, or not read in file         branches = new preset[15];      (int = 0; < branches.length; i++){         branches[i] = new preset();         branches[i].setenabled(false);     } }  public class preset extends jpanel{     private jtextfield ename;     private jbutton edit;     private jbutton activate;     private jcombobox printer;     private jpanel line1;     private jpanel line2;     private string branchname;     private string ipaddress;     private boolean enableall;      public preset(){         ename = new jtextfield(20);         edit = new jbutton("edit");         activate = new jbutton("activate");          jpanel namecontainer = new jpanel();         namecontainer.setlayout(new flowlayout());         namecontainer.add(ename);          printer = new jcombobox();          //         line1 = new jpanel();         line1.setlayout(new flowlayout());         line1.add(namecontainer);         line1.add(edit);         line1.add(activate);          //         line2 = new jpanel();         line2.setlayout(new borderlayout());         line2.add(printer, borderlayout.west);          gridbaglayout grid = new gridbaglayout();         gridbagconstraints cons = new gridbagconstraints();         cons.fill = gridbagconstraints.both;         this.setlayout(grid);          cons.ipady = 100;         cons.ipadx = 100;         cons.weighty = 0d;         cons.gridx = 0;         cons.gridy = 0;         cons.gridwidth = 2;         cons.gridheight = 1;         grid.setconstraints(line1, cons);         this.add(line1);          cons.ipady = 100;         cons.ipadx = 100;         cons.weighty = 0d;         cons.gridx = 0;         cons.gridy = 1;         cons.gridwidth = 2;         cons.gridheight = 1;         grid.setconstraints(line2, cons);         this.add(line2);          //enable components         enableall = true;     } } } 

remove every statement assigns padding in y axis gridbagconstraints of preset jpanel, i.e.

cons.ipady = 100; 

Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

VBA function to include CDATA -