java - Wizard not redrawing/revalidating jframe -


i'd create wizard app in java. came point draw frame 3 different layouts, each containing label , 'next' button, when click button on first frame see empty frame instead.

import java.awt.borderlayout; import java.awt.event.actionevent; import java.awt.event.actionlistener;  import javax.swing.imageicon; import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jlabel;    public class mainframe extends jframe implements actionlistener{     private borderlayout layout;     public mainframe() {         super("mywizz");         setsize(500, 500);         setresizable(false);         setdefaultcloseoperation(jframe.exit_on_close);         setvisible(true);         setlocationrelativeto(null);     }     protected imageicon createimageicon(string path,                                                string description) {         java.net.url imgurl = getclass().getresource(path);         if (imgurl != null) {             return new imageicon(imgurl, description);         } else {             system.err.println("couldn't find file: " + path);             return null;         }     }     public void drawfirstpage(){         layout = new borderlayout();         setlayout(layout);                imageicon icon = createimageicon("/images/icon.png","desc");         add(new jlabel("text",icon,jlabel.center),borderlayout.center);         jbutton bn = new jbutton("next");         add(bn,borderlayout.page_end);         setvisible(true);         bn.setactioncommand("goto2");         bn.addactionlistener(this);         revalidate();     }     public void drawbrowsepage(){         system.out.println(getcontentpane().getcomponentcount());         getcontentpane().removeall();          system.out.println(getcontentpane().getcomponentcount());         repaint();         revalidate();         layout = new borderlayout();         jbutton bn = new jbutton("next");         add(new jlabel("text",jlabel.center),borderlayout.center);         add(bn,borderlayout.page_end);         bn.setactioncommand("goto3");         bn.addactionlistener(this);         setlayout(layout);         setvisible(true);         system.out.println(getcontentpane().getcomponentcount());         repaint();         revalidate();      }       public void drawloadingpage(){         getcontentpane().removeall();         repaint();         revalidate();         layout = new borderlayout();         setlayout(layout);         setvisible(true);         jbutton bn = new jbutton("next");         add(new jlabel("text",jlabel.center),borderlayout.center);         add(bn,borderlayout.page_end);         repaint();         revalidate();     }     public void actionperformed(actionevent e) {         if("goto2".equals(e.getactioncommand())) drawbrowsepage();         if("goto3".equals(e.getactioncommand())) drawloadingpage();     } } 

from few system.out.println functions, can see, drawbrowsepage() executes, old elements removed , new ones added. apprecieate can spare.

faq: no. can't use jwizz.

instead of remove , add , revalidate()/repaint(), can use cardlayout.

short explanation of mean :

simply create stack or list (using collections or array) of jpanels, each representing different views. @ click of next button, show next jpanel using cardlayout.show(container, "stringnameofthepanelyouwanttoshow"). more info can found on cardlayout api, small example help, see functioning :

import java.awt.*; import java.awt.event.*; import javax.swing.*;  /* here first declaring our class act  * base other panels or in other terms base cardlayout.  */  public class cardlayouttest {      private static final string card_jbutton =  "card jbutton";     private static final string card_jtextfield = "card jtextfield";         private static final string card_jradiobutton = "card jradiobutton";      private static void createandshowgui() {         jframe frame = new jframe("card layout test");         frame.setdefaultcloseoperation(jframe.exit_on_close);         frame.setlocationrelativeto(null);          // jpanel base cardlayout other jpanels.         final jpanel contentpane = new jpanel();         contentpane.setlayout(new cardlayout(20, 20));          /* here making objects of window series classes          * that, each 1 of them can added jpanel           * having cardlayout.           */         window1 win1 = new window1();         contentpane.add(win1, card_jbutton);         window2 win2 = new window2();         contentpane.add(win2, card_jtextfield);         window3 win3 = new window3();         contentpane.add(win3, card_jradiobutton);          /* need 2 jbuttons go next card          * or come previous card, , when          * desired user.          */         jpanel buttonpanel = new jpanel();          final jbutton previousbutton = new jbutton("previous");         previousbutton.setbackground(color.black);         previousbutton.setforeground(color.white);         final jbutton nextbutton = new jbutton("next");         nextbutton.setbackground(color.red);         nextbutton.setforeground(color.white);         buttonpanel.add(previousbutton);         buttonpanel.add(nextbutton);          /* adding actionlisteners jbutton,          * user can see next card or          * come previous card, desired.          */         previousbutton.addactionlistener(new actionlistener() {             @override             public void actionperformed(actionevent ae) {                 cardlayout cardlayout = (cardlayout) contentpane.getlayout();                 cardlayout.previous(contentpane);             }         });         nextbutton.addactionlistener(new actionlistener() {             @override             public void actionperformed(actionevent ae) {                 cardlayout cardlayout = (cardlayout) contentpane.getlayout();                 cardlayout.next(contentpane);                }         });          // adding contentpane (jpanel) , buttonpanel jframe.         frame.add(contentpane, borderlayout.center);         frame.add(buttonpanel, borderlayout.page_end);          frame.pack();         frame.setvisible(true);     }      public static void main(string... args) {         swingutilities.invokelater(new runnable() {             @override             public void run() {                 createandshowgui();             }         });     } }   class window1 extends jpanel {      /*      * here our first card of cardlayout,      * added contentpane object of jpanel,      * has layoutmanager set cardlayout.      * card consists of 2 jbuttons.      */       private actionlistener action;      public window1() {         init();     }      private void init() {         final jbutton clickbutton = new jbutton("click me");         final jbutton dontclickbutton = new jbutton("don\'t click me");               action = new actionlistener() {             @override             public void actionperformed(actionevent ae) {                 if (ae.getsource() == clickbutton) {                     joptionpane.showmessagedialog(null, "hello there dude!"                                                 , "right button", joptionpane.information_message);                 } else if (ae.getsource() == dontclickbutton) {                     joptionpane.showmessagedialog(null, "i told not click me!"                                                         , "wrong button", joptionpane.plain_message);                 }             }         };          clickbutton.addactionlistener(action);         dontclickbutton.addactionlistener(action);          add(clickbutton);         add(dontclickbutton);     } }  class window2 extends jpanel implements actionlistener {      /*      * here our second card of cardlayout,      * added contentpane object of jpanel,      * has layoutmanager set cardlayout.      * card consists of jlabel ,  jtextfield      * gridlayout.      */        private jtextfield textfield;      public window2() {         init();     }      private void init() {         setlayout(new gridlayout(1, 2));         jlabel userlabel = new jlabel("your name : ");         textfield = new jtextfield();         textfield.addactionlistener(this);          add(userlabel);         add(textfield);     }      @override     public void actionperformed(actionevent e) {                     if (textfield.getdocument().getlength() > 0)             joptionpane.showmessagedialog(null, "your name : " + textfield.gettext()                                                                             , "user\'s name : ", joptionpane.question_message);     } }  class window3 extends jpanel {      /*      * here our third card of cardlayout,      * added contentpane object of jpanel,      * has layoutmanager set cardlayout.      * card consists of 2 jlabels , 2 jcheckbox      * gridlayout.      */       private actionlistener state;      public window3() {         init();     }      public void init() {         setlayout(new gridlayout(2, 2));         jlabel malelabel = new jlabel("male", jlabel.center);         final jcheckbox malebox = new jcheckbox();         jlabel femalelabel = new jlabel("female", jlabel.center);         final jcheckbox femalebox = new jcheckbox();          state = new actionlistener() {             @override             public void actionperformed(actionevent ae) {                 if (malebox == (jcheckbox) ae.getsource()) {                     femalebox.setselected(false);                     joptionpane.showmessagedialog(null, "congrats male"                                                 , "gender : ", joptionpane.information_message);                                             } else if (femalebox == (jcheckbox) ae.getsource()) {                     malebox.setselected(false);                     joptionpane.showmessagedialog(null, "congrats female"                                             , "gender : ", joptionpane.information_message);                                         }             }         };          malebox.addactionlistener(state);         femalebox.addactionlistener(state);         add(malelabel);         add(malebox);         add(femalelabel);         add(femalebox);     } } 

Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -