java - add multiple JPanels in the middle of the window -
i want create input jframe
program reads 3 fields (model, week , plan), , after inserting 1 line user can choose input new values on different row, done pressing jlabel
image add icon. expectation able add new jpanel
(called body in subclass) right under last 1 (jpanel lastcontent
global variable), , able remove or add new ones user needs. below code far:
package marquesina; import java.awt.container; import java.awt.dimension; import java.awt.toolkit; import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.jpanel; import javax.swing.jtextfield; import org.jdesktop.swingx.horizontallayout; import org.jdesktop.swingx.verticallayout; public class jmodificaciones extends container { private jpanel lastcontent; public static void main(string[] args) { //schedule job event-dispatching thread: //creating , showing application's gui. javax.swing.swingutilities.invokelater(() -> { jframe frame = new jframe("demo"); frame.setdefaultcloseoperation(jframe.exit_on_close); //set content pane. jmodificaciones mods = new jmodificaciones(); frame.setcontentpane(mods); //display window. frame.pack(); dimension dim = toolkit.getdefaulttoolkit().getscreensize(); frame.setlocation( dim.width / 2 - frame.getsize().width / 2, dim.height / 2 - frame.getsize().height / 2); frame.setvisible(true); }); } public jmodificaciones() { initcomponents(); system.out.println("new panel created"); } private void initcomponents() { jpanel jheader = new jpanel(); jlabel jlmod = new jlabel(); jlabel jlsem = new jlabel(); jlabel jlplan = new jlabel(); jpanel jfooter = new jpanel(); jbutton jguardar = new jbutton(); jbutton jcancelar = new jbutton(); setlayout(new verticallayout(10)); //header jheader.setlayout(new horizontallayout()); jlmod.settext("model"); jheader.add(jlmod); jlweek.settext("week"); jheader.add(jlweek); jlplan.settext("plan"); jheader.add(jlplan); add(jheader); //content add(new jcontent()); //footer jguardar.settext("save"); jfooter.add(jguardar); jcancelar.settext("cancel"); jfooter.add(jcancel); add(jfooter); } public class jcontent extends jpanel { jlabel jadd = new javax.swing.jlabel(); jlabel jdelete = new javax.swing.jlabel(); public jcontent() { jpanel body = new jpanel(new horizontallayout()); jtextfield jmodel = new jtextfield(); jtextfield jweek = new jtextfield(); jtextfield jplan = new jtextfield(); body.setlayout(new org.jdesktop.swingx.horizontallayout()); jmodel.setpreferredsize(new java.awt.dimension(100, 28)); body.add(jmodel); jweek.setpreferredsize(new java.awt.dimension(100, 28)); body.add(jweek); jplan.setpreferredsize(new java.awt.dimension(100, 28)); body.add(jplan); jadd.seticon( new javax.swing.imageicon( getclass().getresource("add.png"))); jadd.addmouselistener(new java.awt.event.mouseadapter() { @override public void mouseclicked(java.awt.event.mouseevent evt) { } }); body.add(jadd); jdelete.seticon( new javax.swing.imageicon( getclass().getresource("delete.png"))); jdelete.addmouselistener(new java.awt.event.mouseadapter() { @override public void mouseclicked(java.awt.event.mouseevent evt) { remove(lastcontent); } }); body.add(jdelete); add(body); } }
}
however i'm not able add new jpanel
(which want create when user clicks on jadd jlabel
), can't come way reference jpanel
want put new components , using add() or remove() in above code reference mouselistener
, not jpanel
created in sublcass...
Comments
Post a Comment