java - Add Object to JPanel after button click -


i have created jscrollpane jpanel inside , want add jpanel/jlabel/other objects after pressing button. example after 3 button presses want this: desired result

i tried myjpane.add(testlabel) testlabel.setbounds()but no result, don't want use gridlayout because of unchangeable sizes. if added objects had different sizes - adjusted text content.

what should use , how?

thanks in advance. best regards, tom.

here jpanel inside jscrollpane adds jlabels when pressing button:

public class example extends jframe {      public example() {          jpanel boxpanel = new jpanel();         boxpanel.setlayout(new boxlayout(boxpanel, boxlayout.page_axis));          jtextfield textfield = new jtextfield(20);               jbutton sendbutton = new jbutton("send");         sendbutton.addactionlistener(new actionlistener() {              @override             public void actionperformed(actionevent e) {                  jlabel label = new jlabel(textfield.gettext());                 label.setopaque(true);                 label.setbackground(color.red);                 boxpanel.add(label);                 boxpanel.add(box.createrigidarea(new dimension(0,5)));                 textfield.settext("");                 boxpanel.revalidate(); //              pack();             }         });           jpanel southpanel = new jpanel();         southpanel.add(textfield);         southpanel.add(sendbutton);          add(new jscrollpane(boxpanel));         add(southpanel, borderlayout.page_end);          setdefaultcloseoperation(exit_on_close);         pack();         setlocationrelativeto(null);         setvisible(true);     }      public static void main(string[] args) {          new example();     } } 

the boxlayout stack labels on top of each other.

notes:

  • setopaque(true) must called on label honor background color.
  • box.createrigidarea used creating gaps. use wish.
  • the call revalidate() imperative in order display new components immediately.
  • calling pack() (on jframe) resize each time fit new components. put there demonstration since initial frame size small display initial components added.

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -