java - How to keep JPanel at top of parent container at runtime? -


i create parent container onto place several jpanel objects, each containing several jbutton objects.

i create parent panel, add gridbagconstraints add each child panel parent:

final jpanel options = new jpanel(new gridbaglayout());         options.setborder(new titledborder("select option"));         gridbagconstraints gbc = new gridbagconstraints();         gbc.gridwidth = gridbagconstraints.remainder;         gbc.fill = gridbagconstraints.horizontal;         gbc.weightx = 1.0;         gbc.anchor = gridbagconstraints.north;          options.add(findpanel, gbc);         options.add(addpanel, gbc);         options.add(changepanel, gbc);         options.add(droppanel, gbc);         gbc.weighty = 1.0;         options.add(new jpanel(), gbc); 

with options.add(new jpanel(), gbc); used take space under wanted panels. works great....until want change contents of parent after user interaction:

partnofai.addactionlistener(new actionlistener(){                 public void actionperformed(actionevent ae){                     options.remove(findpanel);                     options.remove(addpanel);                     options.remove(changepanel);                     options.remove(droppanel);                     options.add(partnofaiinp, gbc);                     gbc.weighty = 1.0;                     options.add(new jpanel(), gbc);                     frame.pack();                     frame.validate();                 }              } ); 

it's adding new panel, options.add(partnofaiinp, gbc); middle of parent when want @ top. why wouldn't gbc.anchor = gridbagconstraint.north; keep new panel in north of panel?

any appreciated.

you have think of container in terms of stack

when first setup panel using...

options.add(findpanel, gbc); options.add(addpanel, gbc); options.add(changepanel, gbc); options.add(droppanel, gbc); gbc.weighty = 1.0; options.add(new jpanel(), gbc); 

the container has list of components looking {findpanel, addpanel, changepanel, droppanel, jpanel}

when remove components using like...

options.remove(findpanel); options.remove(addpanel); options.remove(changepanel); options.remove(droppanel); 

the container has list of components looking {jpanel}

then when add new component using...

options.add(partnofaiinp, gbc); gbc.weighty = 1.0; options.add(new jpanel(), gbc); 

the container has list of components looking {jpanel, partnofaiinp, jpanel}

so, instead of adding "filler" component, specify insert point of panel when add it...

options.add(partnofaiinp, gbc, 0); frame.pack(); frame.validate(); 

the container has list of components looking {partnofaiinp, jpanel}


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -