java - How to "hide" a JFrame using a JButton outside the main -


so i've completed project, last thing being "hide" button. teacher never gave instruction on do, since it's annoying me , can't find answer works, figured i'd ask folks.

i've tried:

  1. setextendedstate(jframe.iconified) //causes compiler error, can't find variable name
  2. setstate(jframe.iconified) //same issue, "can't find symbol", or rather find variable
  3. setvisible(false) //this doesn't work bc hides entire frame, , can't without closing program.

i use container c = getcontentpane() create pane, inside main use:

classname variablename = new classname() create parameters. 

this how taught , have use way now(since teacher wants) have noticed there other ways achieve same goal.

any input specific program awesome! thanks!

my program follows(i posted whole thing nothing may left out):

import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.arrays;  public class project9 extends jframe {   font f1 = new font("serif", font.bold, 30);   font f2 = new font("serif", font.plain, 18);    private bookitem[] bookarray = new bookitem[10];    private jlabel headerlbl;   private jlabel messageslbl;    private jtextfield idlabelfld;   private jtextfield idfld;   private jtextfield pricelabelfld;   private jtextfield pricefld;   private jtextfield numinstocklabelfld;   private jtextfield numinstockfld;   private jtextfield codelabelfld;   private jtextfield codefld;   private jtextfield messagesfld;    private jbutton insertbtn;   private jbutton deletebtn;   private jbutton displaybtn;   private jbutton displayonebtn;   private jbutton hidebtn;   private jbutton clearbtn;    private string input = "";   private string displayonestr = "";    private int idinput = 0;   private double priceinput = 0.0;   private int numinstockinput = 0;   private int codeinput = 0;    private int index = 0;   private int numitems = 0;   private int responsecode = 0;    private container c = getcontentpane();    //main method, sets arrayframe params   public static void main(string[] args)   {         project9 arrayframe = new project9();         arrayframe.setsize(555,450);         arrayframe.setvisible(true);         arrayframe.setdefaultcloseoperation(jframe.exit_on_close);   }          //constructor   public project9()   {         //creates array          (int = 0; < bookarray.length; i++)         {               bookarray[i] = new bookitem();               system.out.println(bookarray[i]);         }           settitle("project 9");         c.setlayout(new flowlayout());          headerlbl = new jlabel("data entry: bestbargainbook store");         headerlbl.setfont(f1);         c.add(headerlbl);          idlabelfld = new jtextfield("enter id:", 15);         idlabelfld.seteditable(false);         c.add(idlabelfld);          idfld = new jtextfield(25);         c.add(idfld);          pricelabelfld = new jtextfield("enter price:", 15);         pricelabelfld.seteditable(false);         c.add(pricelabelfld);          pricefld = new jtextfield(25);         c.add(pricefld);          numinstocklabelfld = new jtextfield("enter number in stock:", 15);         numinstocklabelfld.seteditable(false);         c.add(numinstocklabelfld);          numinstockfld = new jtextfield(25);         c.add(numinstockfld);          codelabelfld = new jtextfield("enter code: 1,2,3 or 4:", 15);         codelabelfld.seteditable(false);         c.add(codelabelfld);          codefld = new jtextfield(25);         c.add(codefld);          insertbtn = new jbutton("insert");         c.add(insertbtn);          deletebtn = new jbutton("delete");         c.add(deletebtn);          displaybtn = new jbutton("display");         c.add(displaybtn);          displayonebtn = new jbutton("displayone");         c.add(displayonebtn);          hidebtn = new jbutton("hide");         c.add(hidebtn);          clearbtn = new jbutton("clear");         c.add(clearbtn);          messageslbl = new jlabel("messages:");         messageslbl.setfont(f2);         c.add(messageslbl);          messagesfld = new jtextfield(30);         c.add(messagesfld);          //event listeners         insertbtn.addactionlistener(new eventhandler());         deletebtn.addactionlistener(new eventhandler());         displaybtn.addactionlistener(new eventhandler());         displayonebtn.addactionlistener(new eventhandler());         hidebtn.addactionlistener(new eventhandler());         clearbtn.addactionlistener(new eventhandler());    }//end constructor        private class eventhandler implements actionlistener {   public void actionperformed(actionevent ev)   {          if (ev.getsource() == insertbtn)         {               input = idfld.gettext();               idinput = integer.parseint(input);                input = pricefld.gettext();               priceinput = double.parsedouble(input);                input = numinstockfld.gettext();               numinstockinput = integer.parseint(input);                input = codefld.gettext();               codeinput = integer.parseint(input);                insert(idinput, priceinput, numinstockinput,                       codeinput);                 if (responsecode == 0)               {                     messagesfld.settext("array full. cannot insert book id: " +                                          idinput);               }               else if (responsecode == 1)               {                     messagesfld.settext("succesful insertion of " + idinput);               }               else if (responsecode == -1)               {                     messagesfld.settext("duplicate id: " + idinput);               }                                      }          else if (ev.getsource() == deletebtn)         {               input = idfld.gettext();               idinput = integer.parseint(input);                delete(idinput);                if (responsecode == 1)               {                           messagesfld.settext("successful delete of book id: " +                                         idinput);               }               else if (responsecode == -1)               {                     messagesfld.settext("id: " + idinput + " not found.");               }                                          }          else if (ev.getsource() == displaybtn)         {               (index = 0; index < bookarray.length; index++)               {                     bookarray[index].display();               }               }          else if (ev.getsource() == displayonebtn)         {               input = idfld.gettext();               idinput = integer.parseint(input);                (index = 0; index < bookarray.length; index++)               {                     if (bookarray[index].getid() == idinput)                     {                           bookarray[index].getid();                           bookarray[index].getprice();                           bookarray[index].getnumberinstock();                           bookarray[index].getcode();                            messagesfld.settext("id: " + bookarray[index].getid() +                                               "  price: " + bookarray[index].getprice() +                                               "  number in stock: " + bookarray[index].getnumberinstock() +                                               "  code: " + bookarray[index].getcode());                      }               }                }                else if (ev.getsource() == hidebtn)         {          }          else if (ev.getsource() == clearbtn)         {               idfld.settext("");               pricefld.settext("");               numinstockfld.settext("");               codefld.settext("");               messagesfld.settext("");               repaint();         }    }//end actionperformed  }//end handler    //insert method, called when insert button pressed   public int insert(int id, double prc, int numinstock, int code)   {         if (numitems == 10)         {               system.out.println("\nthe array full, please delete entry");                responsecode = 0;               return responsecode;         }                (index = 0; index < bookarray.length; index++)         {               if (bookarray[index].getid() == id)               {                     system.out.println("\nthat id exists");                      responsecode = -1;                     return responsecode;               }               else if (bookarray[index].getid() == 0)               {                     bookarray[index] = new bookitem(id, prc, numinstock, code);                     numitems++;                       system.out.println("\n" + idinput + "\n" + priceinput + "\n" +                                         numinstockinput + "\n" + codeinput + "\n" + index);                     system.out.println("\nid: " + bookarray[index].getid());                     system.out.println("price: " + bookarray[index].getprice());                     system.out.println("nis: " + bookarray[index].getnumberinstock());                     system.out.println("code: " + bookarray[index].getcode());                     system.out.println("items in array: " + numitems);                       responsecode = 1;                     return responsecode;               }                         }          return responsecode;    }//end insert method     //delete method, called when delete button pressed   public int delete(int id)   {         (index = 0; index < bookarray.length; index++)         {               if (bookarray[index].getid() == id)               {                     bookarray[index].setid(0);                     bookarray[index].setprice(0);                     bookarray[index].setstock(0);                     bookarray[index].setcode(0);                     numitems--;                     system.out.println("\nsuccessful deletion");                      responsecode = 1;                     return responsecode;               }                         }          responsecode = -1;         return responsecode;    }//end delete method   }//end app                                                                  

in actionlistener "hide" button basic code be:

jbutton button = (jbutton)e.getsource(); window window = swingutilities.windowforcomponent( button ); jframe frame = (jframe)window; frame.setstate(jframe.iconified); 

this way not depending on instance variable identifies frame. using source object of event way make listeners generic , flexible.


Comments

Popular posts from this blog

jQuery Mobile app not scrolling in Firefox -

c++ - How to add Crypto++ library to Qt project -

php array slice every 2th rule -