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:
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 jlabel
s 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 onlabel
honor background color.box.createrigidarea
used creating gaps. use wish.- the call
revalidate()
imperative in order display new components immediately. - calling
pack()
(onjframe
) resize each time fit new components. put there demonstration since initial frame size small display initial components added.
Comments
Post a Comment