swing - Java: Add panel to existing panel after runtime -
im trying add new card existing jpanel(cardlayout) click of jbutton , goto new card null exception new card not registering.
ive tried searching google cant find , example , ive tried make 'save' layout using
.validate(); .refresh(); .repaint();
but nothing works, possible dynamically? , if know of example?
code below, jpanelsliding1.nextslidpanel moves selected jpanel in cardlayout. new panel created here causing null error panel not found. thanks:)
/* * change template, choose tools | templates * , open template in editor. */ package partnerships; import components.colorscheme; import components.utilities; import java.awt.borderlayout; import java.awt.color; import java.awt.gridlayout; import java.beans.propertychangeevent; import java.beans.propertychangelistener; import java.beans.propertychangesupport; import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jpanel; public class jslidingpanel extends javax.swing.jpanel implements propertychangelistener { public propertychangesupport pcs = new propertychangesupport(this); private colorscheme scheme; public jslidingpanel() { initcomponents(); pcs.addpropertychangelistener(this); scheme = new colorscheme(); object[][] object = { {pcs, "offered", "bt details", scheme.green()}, {pcs, "sold", "bt details", scheme.pink()}, {pcs, "other", "bt details", scheme.purple()} }; jpanelsliding1.add(makepanels(object)); } private void setupgui() { } private jpanel makepanels(object[][] objects) { jpanel panel = new jpanel(); panel.setlayout(new gridlayout(0, objects.length)); (object[] properties : objects) { panel.add(new objectpanel((propertychangesupport) properties[0], properties[1].tostring(), properties[2].tostring(), (color) properties[3])); } return panel; } private void newpanel(string str) { jpanel panel = new jpanel(); panel.setlayout(new borderlayout()); panel.add(new jbutton()); jpanelsliding1.add(panel); jpanelsliding1.refresh(); jpanelsliding1.validate(); jpanelsliding1.repaint(); validate(); repaint(); utilities.gettopframe().pack(); jpanelsliding1.nextslidpanel(10, panel); jpanelsliding1.refresh(); } @override public void propertychange(propertychangeevent evt) { if (this.isvisible() == true) { // makepanels(); newpanel(evt.getpropertyname()); } } public static void main(string args[]) { java.awt.eventqueue.invokelater(new runnable() { @override public void run() { jframe frame = new jframe(); jpanel panel = new jpanel(); jslidingpanel app = new jslidingpanel(); panel.add(app); frame.add(panel); frame.setdefaultcloseoperation(javax.swing.windowconstants.dispose_on_close); frame.setvisible(true); frame.pack(); } }); } /** * method called within constructor initialize form. * warning: not modify code. content of method * regenerated form editor. */ @suppresswarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="generated code"> private void initcomponents() { jpanelsliding1 = new apps.jpanelsliding(); setpreferredsize(new java.awt.dimension(900, 600)); setlayout(new java.awt.borderlayout()); jpanelsliding1.setlayout(new java.awt.cardlayout()); add(jpanelsliding1, java.awt.borderlayout.center); }// </editor-fold> // variables declaration - not modify private apps.jpanelsliding jpanelsliding1; // end of variables declaration }
if dealing changing card of window on event, following may help.
Comments
Post a Comment