dynamic - adding multiple jlabels at a specific position each time a jbutton is pressed -
private void btnactionperformed(java.awt.event.actionevent evt) { btn.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent e) { jlabel label=new jlabel("test label"); label.setbounds(0,0,135,14); panel.add(label); panel.repaint(); } }); }
i want add each label @ specific position in panel adding records table(suppose table has 4 columns). how achieve that.
i not sure if looking (tell me if it's not), here :
package test; import java.awt.borderlayout; import java.awt.color; import java.awt.graphics; import java.awt.gridlayout; import java.awt.event.actionevent; import java.awt.event.actionlistener; import javax.swing.*; public class testpanel extends jpanel{ jbutton button; jpanel innerpanel; testpanel() { setlayout (new borderlayout()); button = new jbutton("create label"); button.addactionlistener(l -> { jlabel label = new jlabel("test label"); label.setborder(borderfactory.createetchedborder()); innerpanel.add(label); innerpanel.revalidate(); }); add(button, borderlayout.north); innerpanel = new jpanel(); gridlayout layout = new gridlayout(0, 4); innerpanel.setlayout(layout); add(innerpanel, borderlayout.center); } public static void main (string[] args) { jframe frame = new jframe(); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.add(new testpanel()); frame.setsize(640, 480); frame.setvisible(true); } }
Comments
Post a Comment