jcheckbox problems (revised)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • carlos123
    New Member
    • Sep 2007
    • 53

    #1

    jcheckbox problems (revised)

    hello, i am making a hall pass program for my computer programming class. i am having issues with my jcheckboxes. first off. this file reads from a .txt that just has
    greg
    john
    joe
    54343
    45777
    33444

    in it. after the user puts in a correct id number, it displays 4 checkboxes to where they want to go. bathroom, office, nurse, and other. once the user clicks one of the checkboxes, it will remove all the stuff from the panel, and add the name of the person and the time they left. the problem i am having is when the user clicks the checkbox , nothing happens. run this for yourself and see for yourself what happens (if you want of course) this is my final assignment for computer programming and i need help bad lol. i have added an itemlistener, that when the checkbox is selected a boolean = true, then in the actionevents, when the "submit" button is pressed, and the id is correct, and the checkbox boolean is true, it removes all the stuff and puts in all the new elements. i want to say that only the bathroom checkbox has the stuff that it should have to make it work , like added the name and time to the second screen after the 4 checkboxes. also i would like to add that this program compiles just fine, just nothing happens when i click the checkboxes. THANKS IN ADVANCED






    Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.Calendar;
    import java.text.SimpleDateFormat;
    import java.lang.Object;
    import java.io.*;
    import javax.swing.event.*;
    import javax.swing.JCheckBox;
    
    public class main implements ActionListener
    {
            JFrame f1,f2;
            JPanel p1,p2,p3,p4;
            String id,enterstring,name,namestring,idstring,str;
            GP g1;
            JRadioButton bathroom,office,nurse,other;
            JButton submit,returnbutton;
            JTextField idnumber;
            JLabel idprompt,what,time,namelabel;
            boolean show,bathroomb,officeb,nurseb,otherb;
            int idnum,counter;
            SimpleDateFormat sdf;
           String[] namearray; 
           int[] idarray;
           int a,b,idint,enter,teacher;
         public main()
        {   
            teacher = 1234567890;
             bathroomb = false;
             officeb = false;
             nurseb = false;
             otherb = false;
            namearray = new String[3];
            idarray = new int[3];
         sdf = new SimpleDateFormat("yyyy.MM.dd G 'at' hh:mm:ss z");  
           show=true;
            a=0;
            b=0;
            namelabel = new JLabel("");
           bathroom = new JRadioButton("Bathroom");
            bathroom.setMnemonic(KeyEvent.VK_C);
            bathroom.setSelected(false);
            bathroom.addActionListener(this);
            counter = 1;
           office = new JRadioButton("Office");
           office.setMnemonic(KeyEvent.VK_C);
            office.setSelected(false);
             office.addActionListener(this);
           nurse = new JRadioButton("Nurse");
            nurse.setMnemonic(KeyEvent.VK_C);
            nurse.setSelected(false);
             nurse.addActionListener(this);
           other = new JRadioButton("Other");
            other.setMnemonic(KeyEvent.VK_C);
            other.setSelected(false);
             other.addActionListener(this);
            idprompt = new JLabel("Enter ID Number");
            idnumber = new JTextField(14);
            f1 = new JFrame("Hall Pass");
              f1.setSize(550,200);        
            Container c1 = f1.getContentPane();
            time = new JLabel("left at "+sdf.format(new java.util.Date(System.currentTimeMillis())));
            id = "123";
            p1 = new JPanel();
            p1.setSize(500,150);
            p1.setLayout(new BorderLayout());
            
              what = new JLabel("");
    
           bathroom.addItemListener(new Item());
           other.addItemListener(new Item());
            nurse.addItemListener(new Item());
            office.addItemListener(new Item());
            returnbutton = new JButton("Return");
            returnbutton.addActionListener(this);
            submit = new JButton("Submit");
            submit.addActionListener(this);
    
            p2 = new JPanel();  //panel to add JLabel and TextField
    
            g1 = new GP();    
             p1.add(g1,BorderLayout.CENTER);
    
                      //g1.setBackground(new RGB(255, 255, 255));
           g1.add(idprompt);
           g1.add(idnumber);
            p2.add(submit);
            g1.add(what);
        
            p1.add(p2,BorderLayout.SOUTH);      //adds p2 to south (Bottom) Graphics Panel will be added to Center    
            c1.add(p1);
           
         
            f1.show();
           
          
        }
        
        public static void main(String[] args)	
       {
          main m = new main();
       }
      
       
     
       
        public class Item implements ItemListener{
            
           public void itemStateChanged(ItemEvent evt) {
        
                if (evt.getStateChange() == ItemEvent.SELECTED) {
                
                 
                 if (bathroom.isSelected()){
                     
                     bathroomb = true;
                     System.out.println(bathroomb);
                    }
                 if (office.isSelected()){
                     
                     officeb = true;
                    }    
                 if (nurse.isSelected()){
                     
                     
                     nurseb = true;
                    }  
                 if (other.isSelected()){   
                     otherb = true;
                     
                    }
            }
               
               
               
               
               
            }
        }
    
          public void actionPerformed (ActionEvent event)
          {
      try {
            BufferedReader in = new BufferedReader(new FileReader("names.txt"));
              
            if (event.getSource() == returnbutton)
            {
                f1.show();
               
             
            }
            
           //idnum = Integer.Parseint(enter);
        
          
            if (event.getSource() == submit)
            {
             
           for(int i=0; i<6; i++){
    	str = in.readLine().trim() ;
    	   if(i<3){
    		  namearray[a] = str;
    		  a++;
    	    }else{
    		    idarray[b] = Integer.parseInt(str);
    		    b++;
    	     }
    }
    
    
            
            in.close();
          
    
    
               enterstring = idnumber.getText();
               enter = Integer.parseInt(enterstring);
               for(int b = 0;  b< 3; b++)
               {
                   //number 1
               if (enter==idarray[0])
               {
                     g1.remove(idnumber);
                     p1.remove(submit);
                     g1.remove(what);
                     g1.remove(idprompt);
                      p2.remove(submit);
                      namelabel.setText(namearray[0]);
                      g1.add(namelabel);
                     g1.add(bathroom);
                     g1.add(office);
                     g1.add(nurse);
                     g1.add(other);
                                          f1.dispose();
                          f1.show(); 
                     
                    
                 
             
                     
                     
                     
                   //  nurse.addItemListener(new ItemListener() {
                  
                        if (bathroomb == true){
                            g1.remove(namelabel);     
                     g1.remove(namelabel);
                     g1.remove(bathroom);
                     g1.remove(office);
                     g1.remove(nurse);
                     g1.remove(other);
                          g1.setBackground(Color.red);
                          p2.add(returnbutton);
                          namelabel.setText(namearray[0]+" is currently using the pass the time left was ");
                           g1.add(namelabel);
                           g1.add(time);
                         f1.dispose();
                          f1.show();
                        }
                        else             
                        
                        
                        
                        
                        
                      if (officeb == true){
                           g1.remove(namelabel);     
                     g1.remove(namelabel);
                     g1.remove(bathroom);
                     g1.remove(office);
                     g1.remove(nurse);
                     g1.remove(other);
                          g1.setBackground(Color.red);
                 
                          p2.add(returnbutton);
                          namelabel.setText(namearray[0]+" is currently using the pass the time left was ");
                           g1.add(namelabel);
                           g1.add(time);
                          f1.dispose();
                          f1.show();
                          
                        }
                        
                        
                        
                        
                        
                        
                        
                        
                        
                      if (nurseb == true){
                          
                           g1.setBackground(Color.red);
                 
                          p2.add(returnbutton);
                          namelabel.setText(namearray[0]+" is currently using the pass the time left was ");
                           g1.add(namelabel);
                           g1.add(time);
                          
                          
                          
                        }
                        
                        
                        
                        
                        
                        
                      if (otherb == true){
                          
                           g1.setBackground(Color.red);
                 
                          p2.add(returnbutton);
                          namelabel.setText(namearray[0]+" is currently using the pass the time left was ");
                           g1.add(namelabel);
                           g1.add(time);
                          
                          
                        }
                              
                    
                    }
                     else{
                 what.setText("Incorrect Login");
                }
                     
                      
                    
                     
                  //g1.setBackground(Color.red);
                 
                 // p2.add(returnbutton);
                 
                 // g1.add(time);
                  f1.dispose();
                  f1.show();
                }
            
                
            //number 2
            if (enter==teacher)
            {
                f1.dispose();
            }
              if (enter == idarray[1])
               {
                     g1.remove(idnumber);
                     p1.remove(submit);
                     g1.remove(what);
                     g1.remove(idprompt);
                  g1.setBackground(Color.red);
                  p2.remove(submit);
                  p2.add(returnbutton);
                   namelabel.setText(namearray[1]);
                 g1.add(namelabel);
                  g1.add(time);
                  f1.dispose();
                  f1.show();
                }
            
                else{
                 what.setText("Incorrect Login");
                }
            }
              if (enter == idarray[2])
               {
                     g1.remove(idnumber);
                     p1.remove(submit);
                     g1.remove(what);
                     g1.remove(idprompt);
                  g1.setBackground(Color.red);
                  p2.remove(submit);
                  p2.add(returnbutton);
                   namelabel.setText(namearray[2]);
                 g1.add(namelabel);
                  g1.add(time);
                  f1.dispose();
                  f1.show();
                }
            
                else{
                 what.setText("Incorrect Login");
                }
            
        
                idnumber.setText(null);
          
               
               
           
    
           
    
              g1.repaint();
     } catch (IOException e) {
        }
            }
        }
     
       
    	
    
     class GP extends JPanel  //graphics panel
    
    {
       
    
        public GP()
         {      
         
          
            
             
            setBackground(Color.green);
            
         }
       
      
      
        
       
      
        public void paint(Graphics g)
        {
         super.paint(g);
           
            
           
          
             
           
        }
    
    }
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Well, I don't see any JCheckBoxes in the code except in the import statements.
    But then again it's 0004 here so I could be due for some sleep ...

    Comment

    • carlos123
      New Member
      • Sep 2007
      • 53

      #3
      Your right there isnt any, i just changed it and it still does the same thing with the checkboxes

      Comment

      • carlos123
        New Member
        • Sep 2007
        • 53

        #4
        i really need your help

        Comment

        • carlos123
          New Member
          • Sep 2007
          • 53

          #5
          jcheckbox problems (revised)

          hello, i am making a hall pass program for my computer programming class. i am having issues with my jcheckboxes. first off. this file reads from a .txt that just has
          greg
          john
          joe
          54343
          45777
          33444

          in it. after the user puts in a correct id number, it displays 4 checkboxes to where they want to go. bathroom, office, nurse, and other. once the user clicks one of the checkboxes, it will remove all the stuff from the panel, and add the name of the person and the time they left. the problem i am having is when the user clicks the checkbox , nothing happens. run this for yourself and see for yourself what happens (if you want of course) this is my final assignment for computer programming and i need help bad lol. i have added an itemlistener, that when the checkbox is selected a boolean = true, then in the actionevents, when the "submit" button is pressed, and the id is correct, and the checkbox boolean is true, it removes all the stuff and puts in all the new elements. i want to say that only the bathroom checkbox has the stuff that it should have to make it work , like added the name and time to the second screen after the 4 checkboxes. also i would like to add that this program compiles just fine, just nothing happens when i click the checkboxes. THANKS IN ADVANCED

          Code:
          import java.awt.*;
          import java.awt.event.*;
          import javax.swing.*;
          import java.util.Calendar;
          import java.text.SimpleDateFormat;
          import java.lang.Object;
          import java.io.*;
          import javax.swing.event.*;
          import javax.swing.JCheckBox;
           
          public class main implements ActionListener
          {
                  JFrame f1,f2;
                  JPanel p1,p2,p3,p4;
                  String id,enterstring,name,namestring,idstring,str;
                  GP g1;
                  JCheckBox bathroom,office,nurse,other;
                  JButton submit,returnbutton;
                  JTextField idnumber;
                  JLabel idprompt,what,time,namelabel;
                  boolean show,bathroomb,officeb,nurseb,otherb;
                  int idnum,counter;
                  SimpleDateFormat sdf;
                  String select;
                 String[] namearray; 
                 int[] idarray;
                 int a,b,idint,enter,teacher;
               public main()
              {   
                  teacher = 1234567890;
                   bathroomb = false;
                   officeb = false;
                   nurseb = false;
                   otherb = false;
                  namearray = new String[3];
                  idarray = new int[3];
               sdf = new SimpleDateFormat("yyyy.MM.dd G 'at' hh:mm:ss z");  
                 show=true;
                  a=0;
                  b=0;
                  select = "unselected";
                  namelabel = new JLabel("");
                 bathroom = new JCheckBox("Bathroom");
                  bathroom.setMnemonic(KeyEvent.VK_C);
                  bathroom.setSelected(false);
                  bathroom.addActionListener(this);
                  counter = 1;
                 office = new JCheckBox("Office");
                 office.setMnemonic(KeyEvent.VK_C);
                  office.setSelected(false);
                   office.addActionListener(this);
                 nurse = new JCheckBox("Nurse");
                  nurse.setMnemonic(KeyEvent.VK_C);
                  nurse.setSelected(false);
                   nurse.addActionListener(this);
                 other = new JCheckBox("Other");
                  other.setMnemonic(KeyEvent.VK_C);
                  other.setSelected(false);
                   other.addActionListener(this);
                  idprompt = new JLabel("Enter ID Number");
                  idnumber = new JTextField(14);
                  f1 = new JFrame("Hall Pass");
                    f1.setSize(550,200);        
                  Container c1 = f1.getContentPane();
                  time = new JLabel("left at "+sdf.format(new java.util.Date(System.currentTimeMillis())));
                  id = "123";
                  p1 = new JPanel();
                  p1.setSize(500,150);
                  p1.setLayout(new BorderLayout());
                  
                    what = new JLabel("");
           
                 bathroom.addItemListener(new Item());
                 other.addItemListener(new Item());
                  nurse.addItemListener(new Item());
                  office.addItemListener(new Item());
                  returnbutton = new JButton("Return");
                  returnbutton.addActionListener(this);
                  submit = new JButton("Submit");
                  submit.addActionListener(this);
           
                  p2 = new JPanel();  //panel to add JLabel and TextField
           
                  g1 = new GP();    
                   p1.add(g1,BorderLayout.CENTER);
           
                            //g1.setBackground(new RGB(255, 255, 255));
                 g1.add(idprompt);
                 g1.add(idnumber);
                  p2.add(submit);
                  g1.add(what);
              
                  p1.add(p2,BorderLayout.SOUTH);      //adds p2 to south (Bottom) Graphics Panel will be added to Center    
                  c1.add(p1);
                 
               
                  f1.show();
                 
                
              }
              
              public static void main(String[] args)	
             {
                main m = new main();
             }
            
             
           
             
              public class Item implements ItemListener{
                  
                 public void itemStateChanged(ItemEvent evt) {
              
                      if (evt.getStateChange() == ItemEvent.SELECTED) {
                      
                       
                       if (bathroom.isSelected()){
                           select.equals("selected");
                           bathroomb = true;
                           System.out.println(bathroomb);
                          }
                       if (office.isSelected()){
                           
                           officeb = true;
                          }    
                       if (nurse.isSelected()){
                           
                           
                           nurseb = true;
                          }  
                       if (other.isSelected()){   
                           otherb = true;
                           
                          }
                  }
                     
                     
                     
                     
                     
                  }
              }
           
                public void actionPerformed (ActionEvent event)
                {
            try {
                  BufferedReader in = new BufferedReader(new FileReader("names.txt"));
                    
                  if (event.getSource() == returnbutton)
                  {
                      f1.show();
                     
                   
                  }
                  
                 //idnum = Integer.Parseint(enter);
              
                
                  if (event.getSource() == submit)
                  {
                   
                 for(int i=0; i<6; i++){
          	str = in.readLine().trim() ;
          	   if(i<3){
          		  namearray[a] = str;
          		  a++;
          	    }else{
          		    idarray[b] = Integer.parseInt(str);
          		    b++;
          	     }
          }
           
           
                  
                  in.close();
                
           
           
                     enterstring = idnumber.getText();
                     enter = Integer.parseInt(enterstring);
                     for(int b = 0;  b< 3; b++)
                     {
                         //number 1
                     if (enter==idarray[0])
                     {
                           g1.remove(idnumber);
                           p1.remove(submit);
                           g1.remove(what);
                           g1.remove(idprompt);
                            p2.remove(submit);
                            namelabel.setText(namearray[0]);
                            g1.add(namelabel);
                           g1.add(bathroom);
                           g1.add(office);
                           g1.add(nurse);
                           g1.add(other);
                                                f1.dispose();
                                f1.show(); 
                           
                          
                       
                   
                           
                           
                           
                         //  nurse.addItemListener(new ItemListener() {
                        
                              if (select.equals("selected")){
                                  System.out.println("TESTWTF");
                                  g1.remove(namelabel);     
                           g1.remove(namelabel);
                           g1.remove(bathroom);
                           g1.remove(office);
                           g1.remove(nurse);
                           g1.remove(other);
                                g1.setBackground(Color.red);
                                p2.add(returnbutton);
                                g1.add(namelabel);
                                namelabel.setText(namearray[0]+" is currently using the pass the time left was ");
                                 
                                 g1.add(time);
                               f1.dispose();
                                f1.show();
                                bathroomb=false;
                              }
                              else             
                              
                              
                              
                              
                              
                            if (officeb == true){
                                 g1.remove(namelabel);     
                           g1.remove(namelabel);
                           g1.remove(bathroom);
                           g1.remove(office);
                           g1.remove(nurse);
                           g1.remove(other);
                                g1.setBackground(Color.red);
                       
                                p2.add(returnbutton);
                                namelabel.setText(namearray[0]+" is currently using the pass the time left was ");
                                 g1.add(namelabel);
                                 g1.add(time);
                                f1.dispose();
                                f1.show();
                                
                              }
                              
                              
                              
                              
                              
                              
                              
                              
                              
                            if (nurseb == true){
                                
                                 g1.setBackground(Color.red);
                       
                                p2.add(returnbutton);
                                namelabel.setText(namearray[0]+" is currently using the pass the time left was ");
                                 g1.add(namelabel);
                                 g1.add(time);
                                
                                
                                
                              }
                              
                              
                              
                              
                              
                              
                            if (otherb == true){
                                
                                 g1.setBackground(Color.red);
                       
                                p2.add(returnbutton);
                                namelabel.setText(namearray[0]+" is currently using the pass the time left was ");
                                 g1.add(namelabel);
                                 g1.add(time);
                                
                                
                              }
                                    
                          
                          }
                           else{
                       what.setText("Incorrect Login");
                      }
                           
                            
                          
                           
                        //g1.setBackground(Color.red);
                       
                       // p2.add(returnbutton);
                       
                       // g1.add(time);
                        f1.dispose();
                        f1.show();
                      }
                  
                      
                  //number 2
                  if (enter==teacher)
                  {
                      f1.dispose();
                  }
                    if (enter == idarray[1])
                     {
                           g1.remove(idnumber);
                           p1.remove(submit);
                           g1.remove(what);
                           g1.remove(idprompt);
                        g1.setBackground(Color.red);
                        p2.remove(submit);
                        p2.add(returnbutton);
                         namelabel.setText(namearray[1]);
                       g1.add(namelabel);
                        g1.add(time);
                        f1.dispose();
                        f1.show();
                      }
                  
                      else{
                       what.setText("Incorrect Login");
                      }
                  }
                    if (enter == idarray[2])
                     {
                           g1.remove(idnumber);
                           p1.remove(submit);
                           g1.remove(what);
                           g1.remove(idprompt);
                        g1.setBackground(Color.red);
                        p2.remove(submit);
                        p2.add(returnbutton);
                         namelabel.setText(namearray[2]);
                       g1.add(namelabel);
                        g1.add(time);
                        f1.dispose();
                        f1.show();
                      }
                  
                      else{
                       what.setText("Incorrect Login");
                      }
                  
              
                      idnumber.setText(null);
                
                     
                     
                 
           
                 
           
                    g1.repaint();
           } catch (IOException e) {
              }
                  }
              }
           
             
          	
           
           class GP extends JPanel  //graphics panel
           
          {
             
           
              public GP()
               {      
               
                
                  
                   
                  setBackground(Color.green);
                  
               }
             
            
            
              
             
            
              public void paint(Graphics g)
              {
               super.paint(g);
                 
                  
                 
                
                   
                 
              }
           
          }

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Ok. Let's start by clearing the basics first. Start by changing this code

            [CODE=java]catch (IOException e) {
            //If an exception is thrown nothing is done
            }[/CODE]

            to

            [CODE=java]catch (IOException e) {
            e.printStackTra ce();
            //now if an exception is thrown the trace is displayed to the screen
            }[/CODE]

            Do this everywhere in the code where you put in the catch blocks and run it. This is just to make sure no exceptions are being thrown.
            In general you should not gobble up exceptions by using empty catch blocks.

            Comment

            • carlos123
              New Member
              • Sep 2007
              • 53

              #7
              ok i did that , no nothing was printed

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                Originally posted by carlos123
                ok i did that , no nothing was printed
                Well I just copied and pasted your code into my editor.
                There's simply no exception handling in the whole thing.
                You are reading from a text file and assuming that
                1.) The file is always going to be available
                2.) The file will always contain more than 6 lines
                3.) The file will always have numbers from line 4 - 6
                Such things needs to be checked for by the code not just assumed.
                After that you do lots of adding and removing from a frame before calling it's dispose method on it! That most certainly isn't the clever way of going about things.

                P.S I never got to see the JCheckboxes on the frame at all.

                Comment

                • JosAH
                  Recognized Expert MVP
                  • Mar 2007
                  • 11453

                  #9
                  If there's a choice of one out of many, shouldn't the choice be represented by
                  JRadioButtons in a ButtonGroup instead?

                  kind regards,

                  Jos

                  Comment

                  Working...