java - how to remove or make invisible parent window using JDialog class -


i creating sudoku game , wondering if there way remove (or make invisible) parent window when create jdialog objects when user requests new sudoku board. when create new board using jdialog object (via inner classes dialog1 , dialog2) jdialog objects stacked on top of each other. specifically, in dialog2 class, want underlying window contains previous game disappear when "set givens" button pressed.

i post separate classes if want visually observe problem describing, first class should relevant issue (sudokumain).


edit

never mind, apparently there limit characters can enter here. well, here sudokumain irrelevant sections of code removed:

// allow short name access following classes import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.*;  public class sudokumain extends jcomponent {    /**    * application method.    *     * @param args command-line arguments.    */   public static void main(string[] args) {     new sudokumain();   }    // field refers sudokubase class access information   // of board.   private sudokubase board;   // field refers sudokuview object access information   // , provide output   private sudokuview view;    // window components contained in   private jframe win;   // center jpanel object in window   private jpanel center;   // left jpanel object in window   private jpanel west;   // right jpanel object in window   private jpanel east;   // jpanel object hold graphic "buttons"   private jpanel symbols;    // first set-up window (cannot changed once   // instantiated)   private final dialog1 setwin1;    /**    * constructs sudokumain object.    */   public sudokumain() {      // start game     board = makeboard();     view = new sudokuview(board);      win = new jframe("sudoku game");     center = new jpanel();     west = new jpanel();     east = new jpanel();      // graphic "buttons" current sudoku board     symbols = new setsymbols(view);     // first set-up window     setwin1 = new dialog1(this, "new game", true);      // create menu bar     final menuattop menubar = new menuattop(this);     win.setjmenubar(menubar);      // display game mode     jlabel mode = new jlabel("normal play mode");     mode.sethorizontalalignment(jlabel.center);     font modefont = new font("arial", font.bold, 14);     mode.setfont(modefont);      // set selected cell @ (0, 0)     view.setselected(0, 0);      win.setlayout(new borderlayout());     west.setlayout(new borderlayout());     east.setlayout(new borderlayout());     center.setlayout(new flowlayout());      west.add(symbols);     east.add(view, borderlayout.center);     center.add(west);     center.add(east);      win.add(center, borderlayout.center);     win.add(mode, borderlayout.south);      win.setdefaultcloseoperation(jframe.dispose_on_close);     win.pack();     win.setvisible(true);    }    // inner class constructs graphic "buttons" set desired   // cells of board   class sudokucontrolbutton extends jpanel {      // row of selected cell     private int selrow;     // column of selected cell     private int selcol;      // value corresponds desired symbol     private int value;      /**      * constructs sudokucontrolbutton object; graphic "button"      * control board.      *       * @param view sudokuview object controlled.      * @param v value corresponds desired symbol.      */     public sudokucontrolbutton(final sudokuview view, int v) {       // set characteristics of graphic "button"       setpreferredsize(new dimension(50, 50));       setbackground(color.light_gray);        value = v;        addmouselistener(new mouselistener() {          /**          * method selects "button" , puts in focus when mouse          * clicked on it.          *           * @param event captures information on mouse button being          *              clicked (pressed , released) on component.          */         public void mouseclicked(mouseevent e) {           selrow = view.getselectedrow();           selcol = view.getselectedcolumn();            if(!board.isgiven(selrow, selcol)) {             board.setvalue(selrow, selcol, value);             view.new sudokucell(selrow, selcol, board);             // set "highlighted" color             setbackground(color.white);             view.repaint();           }           else {  // have system beep sound             gettoolkit().beep();           }            repaint();         }          /**          * method handles behavior when mouse enters graphic          * "button".          *           * @param event captures information on mouse button being          *              entered on component.          */          public void mouseentered(mouseevent e){           // set "highlighted" color           setbackground(color.white);            repaint();         }          /**          * method handles behavior when mouse exits graphic          * "button".          *           * @param event captures information on mouse button being          *              exited component.          */         public void mouseexited(mouseevent e){           // set default color           sudokucontrolbutton button = (sudokucontrolbutton) e.getsource();            setbackground(color.light_gray);            repaint();         }          /**          * method handles behavior when mouse pressed on          * graphic "button".          *           * @param event captures information on mouse button being          *              pressed on component.          */         public void mousepressed(mouseevent e){           // set "active" color           setbackground(color.yellow);            repaint();         }          /**          * method handles behavior when mouse released on          * graphic "button".          *           * @param e captures information on mouse button being          *              released on component.          */         public void mousereleased(mouseevent e){         }        });      }      /**      * method draws graphic "button" associated      * each numeric value, 0 12.      *       * @param g drawing mechanism.      */     public void paintcomponent(graphics g) {       super.paintcomponent(g);        switch(value) {         case 0:           drawsymbol(g, 0);           break;         case 1:           drawsymbol(g, 1);           break;         case 2:           drawsymbol(g, 2);           break;         case 3:           drawsymbol(g, 3);           break;         case 4:           drawsymbol(g, 4);           break;         case 5:           drawsymbol(g, 5);           break;         case 6:           drawsymbol(g, 6);           break;         case 7:           drawsymbol(g, 7);           break;         case 8:           drawsymbol(g, 8);           break;         case 9:           drawsymbol(g, 9);           break;         case 10:           drawsymbol(g, 10);           break;         case 11:           drawsymbol(g, 11);           break;         case 12:           drawsymbol(g, 12);           break;       }      }      /**      * method draws symbol corresponds       * specified value (0-12).      *       * @param g drawing mechanism.      * @param value specified value.      */     public void drawsymbol(graphics g, int value) {        if(value < 0 || value > 12) {         string msg = "value cannot less 0 or greater 12.";         throw new illegalargumentexception(msg);       }        // enable drawing "thick" lines       graphics2d g2 = (graphics2d) g;       g2.setstroke(new basicstroke(3));        switch(value) {         case 0:           // draw borders           g.drawrect(0, 0, 50, 50);           break;         case 1:           // draw borders           g.drawrect(0, 0, 50, 50);           // draw symbol           g2.drawline(5, 5, 5, 45);           break;         case 2:           // draw borders           g.drawrect(0, 0, 50, 50);           // draw symbol           g2.drawline(5, 5, 5, 45);           g2.drawline(10, 5, 10, 45);           break;         case 3:           // draw borders           g.drawrect(0, 0, 50, 50);           // draw symbol           g2.drawline(5, 5, 5, 45);           g2.drawline(10, 5, 10, 45);           g2.drawline(15, 5, 15, 45);           break;         case 4:           // draw borders           g.drawrect(0, 0, 50, 50);           // draw symbol           g2.drawline(5, 5, 5, 45);           g2.drawline(10, 5, 10, 45);           g2.drawline(15, 5, 15, 45);           g2.drawline(20, 5, 20, 45);           break;         case 5:           // draw borders           g.drawrect(0, 0, 50, 50);           // draw symbol           g2.drawline(5, 5, 5, 45);           g2.drawline(10, 5, 10, 45);           g2.drawline(15, 5, 15, 45);           g2.drawline(20, 5, 20, 45);           g2.drawline(25, 5, 25, 45);           break;         case 6:           // draw borders           g.drawrect(0, 0, 50, 50);           // draw symbol           g2.drawline(5, 5, 5, 45);           g2.drawline(10, 5, 10, 45);           g2.drawline(15, 5, 15, 45);           g2.drawline(20, 5, 20, 45);           g2.drawline(25, 5, 25, 45);           g2.drawline(30, 5, 30, 45);           break;         case 7:           // draw borders           g.drawrect(0, 0, 50, 50);           // draw symbol           g2.drawline(5, 5, 5, 20);           g2.drawline(10, 5, 10, 20);           g2.drawline(15, 5, 15, 20);           g2.drawline(20, 5, 20, 20);           g2.drawline(25, 5, 25, 20);           g2.drawline(30, 5, 30, 20);           g2.drawline(5, 30, 5, 45);           break;         case 8:           // draw borders           g.drawrect(0, 0, 50, 50);           // draw symbol           g2.drawline(5, 5, 5, 20);           g2.drawline(10, 5, 10, 20);           g2.drawline(15, 5, 15, 20);           g2.drawline(20, 5, 20, 20);           g2.drawline(25, 5, 25, 20);           g2.drawline(30, 5, 30, 20);           g2.drawline(5, 30, 5, 45);           g2.drawline(10, 30, 10, 45);           break;         case 9:           // draw borders           g.drawrect(0, 0, 50, 50);           // draw symbol           g2.drawline(5, 5, 5, 20);           g2.drawline(10, 5, 10, 20);           g2.drawline(15, 5, 15, 20);           g2.drawline(20, 5, 20, 20);           g2.drawline(25, 5, 25, 20);           g2.drawline(30, 5, 30, 20);           g2.drawline(5, 30, 5, 45);           g2.drawline(10, 30, 10, 45);           g2.drawline(15, 30, 15, 45);           break;         case 10:           // draw borders           g.drawrect(0, 0, 50, 50);           // draw symbol           g.drawline(5, 5, 5, 20);           g.drawline(10, 5, 10, 20);           g.drawline(15, 5, 15, 20);           g.drawline(20, 5, 20, 20);           g.drawline(25, 5, 25, 20);           g.drawline(30, 5, 30, 20);           g.drawline(5, 30, 5, 45);           g.drawline(10, 30, 10, 45);           g.drawline(15, 30, 15, 45);           g.drawline(20, 30, 20, 45);           break;         case 11:           // draw borders           g.drawrect(0, 0, 50, 50);           // draw symbol           g.drawline(5, 5, 5, 20);           g.drawline(10, 5, 10, 20);           g.drawline(15, 5, 15, 20);           g.drawline(20, 5, 20, 20);           g.drawline(25, 5, 25, 20);           g.drawline(30, 5, 30, 20);           g.drawline(5, 30, 5, 45);           g.drawline(10, 30, 10, 45);           g.drawline(15, 30, 15, 45);           g.drawline(20, 30, 20, 45);           g.drawline(25, 30, 25, 45);           break;         case 12:           // draw borders           g.drawrect(0, 0, 50, 50);           // draw symbol           g.drawline(5, 5, 5, 20);           g.drawline(10, 5, 10, 20);           g.drawline(15, 5, 15, 20);           g.drawline(20, 5, 20, 20);           g.drawline(25, 5, 25, 20);           g.drawline(30, 5, 30, 20);           g.drawline(5, 30, 5, 45);           g.drawline(10, 30, 10, 45);           g.drawline(15, 30, 15, 45);           g.drawline(20, 30, 20, 45);           g.drawline(25, 30, 25, 45);           g.drawline(30, 30, 30, 45);           break;       }      }    }    // inner class provides jmenubar object @ top of   // board   class menuattop extends jmenubar implements actionlistener{     // sudokumain object dealing     private sudokumain main;      // "file" menu     private jmenu filemenu;     // "new game" option in "file" menu     private jmenuitem newgame;      // jdialog object create dialog box prompt     // user new game information     private jdialog createnewwin;       /**      * constructs menuattop object.      *       * @param m sudokumain object referred to.      */     public menuattop(final sudokumain m) {        main = m;        // instantiate , bind reference       filemenu = new jmenu("file");       add(filemenu);       // instantiate , bind reference       newgame = new jmenuitem("new game");       newgame.setaccelerator(keystroke.getkeystroke(keyevent.vk_n,                                                     actionevent.ctrl_mask));       filemenu.add(newgame);        // add action listener "newgame" option       newgame.addactionlistener(this);      }      /**      * method handles action of activating "new game"      * option.      *       * @param e captures information event occurred.      */     public void actionperformed(actionevent e) {       setenabled(false);       // create dialog box prompting new board information       createnewwin = new dialog1(main, "create new board", true);       // make visible       createnewwin.setvisible(true);     }    }    // inner class provides dialog box prompt user   // new board information   class dialog1 extends jdialog {      // rows new game     private jtextfield rows;     // cols new game     private jtextfield cols;     // button create new board     private jbutton createboard;     // button cancel new board , return     // previous game     private jbutton cancel;     // labels rows per region     private jlabel rowlabel;     // label columns per region     private jlabel collabel;     // label dislayed when error occurs     private jlabel errormes;      // jpanel object house error message     private jpanel center;     // jpanel object house rows , columns prompt     private jpanel north;     // jpanel object house create new board , cancel buttons     private jpanel south;     // jdialog object create window new game     private jdialog createwin2;      /**      * constructs dialog1 object.      *       * @param win window containing dialog box.      * @param header title of dialog box.      * @param modal whether dialog box modal or not.      */     public dialog1(final sudokumain win, string header, boolean modal) {       // call superclass constructor       super();        // instantiate , bind references       rows = new jtextfield(2);       cols = new jtextfield(2);       createboard = new jbutton("create new board");       cancel = new jbutton("cancel");       rowlabel = new jlabel("rows per region: ");       collabel = new jlabel("columns per region: ");       errormes = new jlabel();        north = new jpanel(new flowlayout());       center = new jpanel(new flowlayout());       south = new jpanel(new flowlayout());        // set characteristics       setdefaultcloseoperation(jframe.hide_on_close);       settitle(header);       setmodal(modal);       setlayout(new borderlayout());        // set characteristics of error message       errormes.setforeground(color.red);       errormes.setfont(new font("arial", font.italic, 12));       errormes.setvisible(false);        // keep track of "old" board       final sudokubase oldboard = board;        // add action listener "cancel" button       cancel.addactionlistener(new actionlistener() {          /**          * method handles action of activating          * "cancel" button make dialog box "invisible".          *           * @param e captures information event occurred.          */         public void actionperformed(actionevent e) {           setvisible(false);         }       });        // add action listener "create board" button       createboard.addactionlistener(new actionlistener() {          /**          * method handles action of activating          * "cancel" button make dialog box "invisible".          *           * @param e captures information event occurred.          */         public void actionperformed(actionevent e) {           int newrows;           int newcols;           int newsize;            // handles potential exception when converting string input           // int           try{             newrows = integer.parseint(rows.gettext());             newcols = integer.parseint(cols.gettext());           } catch (numberformatexception exc) {             newrows = 0;             newcols = 0;           }            newsize = newrows * newcols;           // input validation           if(newsize <= 0 || newsize > 12) {             errormes.settext("rows times columns cannot less one" +                              " or greater 12!");             errormes.setvisible(true);             pack();            } else {             errormes.setvisible(false);             setvisible(false);              // update board new board             board = new sudokuboard(newrows, newcols);             createwin2 = new dialog2(win, oldboard, view, symbols, newrows,                                      newcols, "new sudoku game", true);           }         }});         // place error message in center       center.add(errormes);        // place labels rows , columns @ top        north.add(rowlabel);       north.add(rows);       north.add(collabel);       north.add(cols);       // place both buttons @ bottom       south.add(createboard);       south.add(cancel);        add(center, borderlayout.center);       add(north, borderlayout.north);       add(south, borderlayout.south);        pack();        if(!win.win.isvisible()) {         dispose();       }      }   }    // inner class dialog box house new game   class dialog2 extends jdialog {      // view used     private sudokuview view;      // panel house board (view) , both     // "set givens" , "cancel" buttons     private jpanel panel;     // panel placed within "panel" houses both "set givens"     // , "cancel" buttons     private jpanel northpanel;     // panel house graphic "buttons"     private jpanel symbols;      // "set givens" button     private jbutton setgivencells;     // "cancel" button     private jbutton cancel;      /**      * constructs dialog2 object.      *       * @param win window containing dialog box.      * @param oldboard "old" sudokuboard keep track of.      * @param oldview "old" sudokuview keep track of.      * @param oldsymbols "old" graphic "buttons" keep track of.      * @param rows rows of new sudoku board created.      * @param cols columns of new sudoku board created.      * @param header title of dialog box.      * @param modal whether dialog box modal or not.      */     public dialog2(final sudokumain mainwin, final sudokubase oldboard,                    final sudokuview oldview, final jpanel oldsymbols,                    int rows, int cols, string header, boolean modal) {       // call superclass constructor       super();        // instantiate , bind references       view = new sudokuview(board);       panel = new jpanel();       northpanel = new jpanel();       setgivencells = new jbutton("set givens");       cancel = new jbutton("cancel");       symbols = new setsymbols(view);        // create menu bar       final menuattop menubar = new menuattop(mainwin);       setjmenubar(menubar);        // display "set-up mode"       final jlabel setupmode = new jlabel("set-up mode");       setupmode.sethorizontalalignment(jlabel.center);       font setupmodefont = new font("comic sans ms", font.bold, 18);       setupmode.setfont(setupmodefont);       setupmode.setforeground(color.red);        // display "normal play mode"       final jlabel mode = new jlabel("normal play mode");       mode.sethorizontalalignment(jlabel.center);       font modefont = new font("arial", font.bold, 14);       mode.setfont(modefont);        // set characteristics       settitle(header);       setmodal(modal);       setlayout(new flowlayout());       panel.setlayout(new borderlayout());       northpanel.setlayout(new flowlayout());        // add action listener "set givens" button       setgivencells.addactionlistener(new actionlistener() {          /**          * method handles action of activating          * "set givens" button.          *           * @param e captures information event occurred.          */         public void actionperformed(actionevent e) {           // set "given" cells           board.fixgivens();            // remove "set-up mode" label , replace           // "normal play mode" label           panel.remove(setupmode);           panel.add(mode, borderlayout.south);            // disable both buttons           setgivencells.setenabled(false);           cancel.setenabled(false);            validate();           repaint();          }       });        // add action listener "cancel" button       cancel.addactionlistener(new actionlistener() {          /**          * method handles action of activating          * "cancel" button.          *           * @param e captures information event occurred.          */         public void actionperformed(actionevent e) {           // have window refer "old" board           board = oldboard;            mainwin.west.remove(mainwin.symbols);           mainwin.east.remove(mainwin.view);            mainwin.view = oldview;           mainwin.symbols = oldsymbols;            mainwin.west.add(mainwin.symbols);           mainwin.east.add(mainwin.view);            // disable both buttons           setgivencells.setenabled(false);           cancel.setenabled(false);            setvisible(false);            repaint();          }       });        // place buttons @ top       northpanel.add(setgivencells);       northpanel.add(cancel);        // place board fill remainder of space not       // occupied buttons @ top       panel.add(view, borderlayout.center);       panel.add(northpanel, borderlayout.north);       panel.add(setupmode, borderlayout.south);        // place graphic "buttons" left of board       add(symbols);       add(panel);        setdefaultcloseoperation(jframe.dispose_on_close);       pack();       setvisible(true);      }    }    // inner class creates graphic "buttons" set selected cell   // desired "button"   class setsymbols extends jpanel {      // temporary board provides information create graphic "buttons"     private sudokuboard tempbd;      private int value;      /**      * constructs setsymbols object.      *       * @param view sudokuview object setsymbols.      */     public setsymbols(final sudokuview view) {       // instantiate , bind reference       tempbd = new sudokuboard(1, board.getboardsize() + 1);        setlayout(new gridlayout((tempbd.getboardsize())/2 + 1, 2));        for(int colsymbol = 0; colsymbol < tempbd.getboardsize(); colsymbol++) {         // keep track of value of graphic "button"         value = colsymbol;          final jpanel sympanel = new jpanel();          // set value each graphic "button"         tempbd.setvalue(0, colsymbol, colsymbol);         // add appropriate symbol each graphic "button"         sympanel.add(new sudokucontrolbutton(view, value));          // add graphic "button"         add(sympanel);        }      }    }  } 

i suggest use different tact. rather create new jdialog new game, why not use same view show new game. either reset current sudoku board (probably common way sudoku game) or create new jpanel view new sudoku game , swap views cardlayout.


edit
state in comment:

i interested in reset technique talked about. how go doing that?

it depends on how code written. assuming using mvc type of program structure, give model class reset method, called reset() or resetboard(), , inside method reset logical part of sudoku grid new initial state. model notify view (the gui portion of code) of changes (sometimes done through mediation of control class) , changes view representation of board model's new state.


edit 2

yeah, utilizing mvc concept. include reset method in sudokumain (the class runs program , contains controller)?

i'm not clear on program set up. mvc structured so:

  • model: "business logic" underlies application.
  • view: gui displays visual representation of model , interacts user.
  • control: go-between between model , view. user initiated events trigger control notify model , possibly change model's state.
  • main: very short program nothing more create above entities, ties them together, , sets program running.

so there no place in structure above main class have reset functionality. model should have method when called resets state, control should call method in response user interacting (probably pushing button) on view.


Comments

Popular posts from this blog

javascript - DIV "hiding" when changing dropdown value -

Does Firefox offer AppleScript support to get URL of windows? -

android - How to install packaged app on Firefox for mobile? -