java - What is an advisable design pattern for switching between GUI pages? -



don't code below is:

  1. getters needed every jbutton on each page
  2. the actionperformed method can become bloated if-else statements

so, there better way control gui actions single class?

if define actionperformed method within each respective page (jpanel), each page need access instances of page(s) switched to, , trying avoid using singleton pattern each page...


here code:

import java.awt.event.actionevent; import java.awt.event.actionlistener;  /**  *   * @author ian a. campbell  *  */ public class controller implements actionlistener {      /**      * instance variables:      */     private frame frame;     private optionpage firstpage;     private firstoptionpage firstoption;     private secondoptionpage secondoption;      /**      *       */     public controller() {          // instantiating frame here:         this.frame = new frame();          /*          *  instantiating pages here:          *            *  note: passing "this" because class          *  handles events these pages          */         this.firstpage = new optionpage(this);         this.firstoption = new firstoptionpage(this);         this.secondoption = new secondoptionpage(this);     }      /**      *       */     public void start() {         this.frame.add(this.firstpage); // adding first page          // note: these lines prevent blank loading , flickering pages!         this.frame.validate();         this.frame.repaint();         this.frame.setvisible(true);     }      /**      *       * @return jframe instantiated class frame      */     public frame getframe() {         return this.frame;     }      @override     public void actionperformed(actionevent e) {          // "first option" button optionpage:         if (e.getsource() == this.firstpage.getfirstbutton()) {             this.frame.getcontentpane().removeall();             this.frame.getcontentpane().add(this.firstoption);          // "second option" button optionpage:         } else if (e.getsource() == this.firstpage.getsecondbutton()) {             this.frame.getcontentpane().removeall();             this.frame.getcontentpane().add(this.secondoption);         }          // note: these lines prevent blank loading , flickering pages!         this.frame.validate();         this.frame.repaint();         this.frame.setvisible(true);     } } // end of controller 

use card layout. card layout actions adds features might find helpful.


Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -