java - How to a new element to JFrame and have it show -
i have gui scroll pane in it. scroll pane scrolling though jpanel of jpanels. ability add 1 more list of subjpanels , have jframe update it.
right have array updating, not jframe.
package sscce; import java.awt.borderlayout; import java.awt.dimension; import java.awt.flowlayout; import java.awt.gridlayout; import java.awt.toolkit; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.io.ioexception; import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.jpanel; import javax.swing.jscrollpane; import javax.swing.jtextfield; public class add extends jframe{ private jpanel[] branches; private jpanel pane; //pane stores accounts private jscrollpane scroller; private jbutton newbranch; public static void main(string[] args) { jframe frame = new add(); } public add(){ this.setdefaultcloseoperation(jframe.exit_on_close); this.setsize(500, 500); this.settitle("how add?"); this.setlayout(new borderlayout()); this.add(statusbar(), borderlayout.north); populatebranches(); pane = new jpanel(); pane.setlayout(new gridlayout(branches.length,1)); (int = 0; < branches.length; i++){ pane.add(branches[i]); } scroller = new jscrollpane(pane); scroller.createverticalscrollbar(); this.add(scroller,borderlayout.center); this.setvisible(true); } private jpanel statusbar(){ jpanel statusbar = new jpanel(); statusbar.setlayout(new flowlayout()); newbranch = new jbutton("new branch"); newbranch.addactionlistener(new buttoneventhandler()); statusbar.add(newbranch); return statusbar; } private void populatebranches(){ branches = new jpanel[2]; (int = 0; < branches.length; i++){ branches[i] = new jpanel(); branches[i].setlayout(new flowlayout()); branches[i].add(new jtextfield(20)); } } private void newbranch(){ jpanel[] tempbranches = new jpanel[branches.length + 1]; system.out.println(tempbranches.length); (int = 0; < branches.length; i++){ tempbranches[i] = branches[i]; } tempbranches[branches.length] = new jpanel(); tempbranches[branches.length].setlayout(new flowlayout()); tempbranches[branches.length].add(new jtextfield(20)); branches = tempbranches; pane = new jpanel(); pane.setlayout(new gridlayout(branches.length, 1)); (int = 0; < branches.length; i++){ pane.add(branches[i]); } pane.repaint(); pane.validate(); scroller = new jscrollpane(pane); this.add(scroller, borderlayout.center); this.repaint(); this.validate(); } private class buttoneventhandler implements actionlistener { @override public void actionperformed(actionevent event) { string btnclkd = event.getactioncommand(); if (btnclkd.equals("new branch")){ newbranch(); } } } }
if allow gridlayout grow, can add more components it:
// 0 means new rows added needed pane.setlayout(new gridlayout(0, 1)); // ... private void newbranch(){ // create component jpanel branch = new jpanel(); branch.add(new jtextfield(20)); // + additional subcomponents // , add others pane.add(branch); pane.revalidate(); }
Comments
Post a Comment