Results 1 to 4 of 4
  1. #1
    carlos123 is offline Member
    Join Date
    Nov 2007
    Posts
    38
    Rep Power
    0

    Exclamation jcheckbox issues need help. thanks.

    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


    Java 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);
           
            
           
          
             
           
        }
     
    }

  2. #2
    unhurt is offline Member
    Join Date
    Oct 2007
    Posts
    21
    Rep Power
    0

    Default

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
    import java.util.Calendar;
    import java.text.SimpleDateFormat;
    import java.lang.Object;
    import java.io.*;
    
    
    
    public class maincheck implements ActionListener {
    
    	JFrame f = new JFrame("Hall Pass");
    	Container c = f.getContentPane();
    
    	JLabel lblID = new JLabel("Enter ID Number  :");
    	JTextField txtID = new JTextField(25);
    
    	JLabel lblNAME = new JLabel();
    	JLabel lblTIME = new JLabel();
    
    	ButtonGroup bg = new ButtonGroup();
    	JRadioButton rdbBATH = new JRadioButton("Bathroom");
    	JRadioButton rdbOFFC = new JRadioButton("Office");
    	JRadioButton rdbNURS = new JRadioButton("Nurse");
    	JRadioButton rdbOTHE = new JRadioButton("Others...");
    
    	boolean check = false;
    	JButton btnSUB = new JButton("Enter");
    	JButton btnRET = new JButton("Return");
    
    //first page
    	JPanel pnl1 = new JPanel();
    	JPanel pnl2 = new JPanel();
    	JPanel pnl3 = new JPanel();
    //second page
    	JPanel pnl4 = new JPanel();
    	JPanel pnl5 = new JPanel();
    
    	String[] namearray = new String[3];
    	int[] idarray = new int[3];
    	int INdex = 0;
    	SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd G 'at' hh:mm:ss z");  
    //body
    	public void create() {
    		f.setLayout(new BorderLayout());
    		createFirstPage();
    
    		btnSUB.addActionListener(this);
    		btnRET.addActionListener(this);
    
    		bg.add(rdbBATH);
    		bg.add(rdbOFFC);
    		bg.add(rdbNURS);
    		bg.add(rdbOTHE);
    
    		f.setSize(520,200);
    		f.setVisible(true);
    		f.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
    	}
    //main
    	public static void main(String agr[]) {
    		maincheck m = new maincheck();
    		m.create();
    	}
    //first page
    	public void createFirstPage() {
    		pnl1.add(lblID);
    		pnl1.add(txtID);
    		txtID.setText("");
    		f.add(pnl1, BorderLayout.NORTH);
    
    		pnl2.add(btnSUB);
    		f.add(pnl2, BorderLayout.SOUTH);
    
    		pnl1.setVisible(true);
    		pnl2.setVisible(true);
    	}
    
    	public void createRadioButton() {
    		pnl3.add(rdbBATH);
    		pnl3.add(rdbOFFC);
    		pnl3.add(rdbNURS);
    		pnl3.add(rdbOTHE);
    		f.add(pnl3, BorderLayout.CENTER);
    
    		pnl3.setVisible(true);
    	}
    //second page
    	public void createSecondPage(String text) {
    		lblNAME.setText(namearray[INdex] + " goes to " + text + " At ");
    		lblTIME.setText(sdf.format(new java.util.Date(System.currentTimeMillis())));
    		pnl4.add(lblNAME);
    		pnl4.add(lblTIME);
    		f.add(pnl4, BorderLayout.NORTH);
    
    		pnl5.add(btnRET);
    		f.add(pnl5, BorderLayout.SOUTH);
    
    		pnl4.setVisible(true);
    		pnl5.setVisible(true);
    
    //disable first page
    		pnl1.setVisible(false);
    		pnl2.setVisible(false);
    		pnl3.setVisible(false);
    	}
    
    	public void actionPerformed(ActionEvent e) {
    
    		if (e.getSource() == btnSUB) {
    			String name = null;
    			int a = 0;
    			int b = 0;
    
    			if (check == false) {
    				try {
    					BufferedReader in = new BufferedReader(new FileReader("names.txt"));
    
    
    						for(int i=0; i<6; i++){
    
    							name = in.readLine().trim() ;
    							if(i<3){
    								namearray[a] = name;
    								a++;
    							}else{
    								idarray[b] = Integer.parseInt(name);
    								b++;
    							}
    						}
    					in.close();
    
    				}catch(Exception ex) {
    					System.err.println("reader error : " + ex.getMessage());
    				}
    
    				try {
    					for (int i = 0; i<3; i++) {
    
    						if ((Integer.parseInt(txtID.getText())) == idarray[i]){
    							INdex = i;
    							check = true;
    							btnSUB.setText("Submit");
    							createRadioButton();
    						}
    
    					}
    				}catch(Exception ex){
    					System.out.println("wrong input");
    				}
    
    				if (check == false) {
    					System.out.println("Wrong ID!");
    				}
    			}else{
    
    				if (rdbBATH.isSelected() == true) {
    					createSecondPage("Bathroom");
    					check = false;
    
    				}else if (rdbOFFC.isSelected() == true) {
    					createSecondPage("Office");
    					check = false;
    
    				}else if (rdbNURS.isSelected() == true) {
    					createSecondPage("Nurse");
    					check = false;
    
    				}else if (rdbOTHE.isSelected() == true) {
    					createSecondPage("Others place...");
    					check = false;
    
    				}else{
    					System.out.println("Please Select Where Are You Going.");
    				}
    			}
    		}
    
    		if (e.getSource() == btnRET) {
    			createFirstPage();
    			pnl4.setVisible(false);
    			pnl5.setVisible(false);
    		}
    	}
    }
    i make my own version of it, can't really stand ur indentation lol, if u still insist on ur version just take the related chunk and implement it urself
    i don't quite understand what u want but the way i done is
    >enter id
    >check id from names.txt
    >if exist ask to select where to go (bathroom, office, nurse, others)
    >open next page showing the name of the ID, going to where and when
    Last edited by unhurt; 11-02-2007 at 05:28 PM.

  3. #3
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Java Code:
    C:\jexp>javac checkboxtest.java
    Note: checkboxtest.java uses or overrides a deprecated API.
    Note: Recompile with -Xlint:deprecation for details.
    
    C:\jexp>javac -Xlint:deprecation checkboxtest.java
    checkboxtest.java:150: warning: [deprecation] show() in java.awt.Window has been
     deprecated
                f1.show();
    The show method was deprecated in j2se 1.5. Look up the method in the Window api to see that it has been replaced by setVisible. If you are using an older version then setVisible will work okay (since 1.1);
    just nothing happens when i click the checkboxes
    Actually, they are radio buttons.
    The code logic in the ItemListener was faulty and the code to reset the gui components was buried/hidden/not_accessible in the actionPerformed "submit" block.
    Many changes and some suggestions.
    When you change/alter the components in a container you must tell the container to renew its layout. We do this with either the Container method validate or the JComponent method revalidate.
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.*;
    import java.text.SimpleDateFormat;
    import java.lang.Object;
    import java.io.*;
    import javax.swing.event.*;
    import javax.swing.JCheckBox;
     
    public class CheckBoxTest implements ActionListener
    {
        JFrame f1,f2;
        JPanel p1,p2,p3,p4,g1;
        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 CheckBoxTest()
        {   
            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("54343", 14);
            f1 = new JFrame("Hall Pass");
    //        f1.setSize(550,200); 
            Container c1 = f1.getContentPane();
            time = new JLabel("left at "+sdf.format(new Date(System.currentTimeMillis())));
            id = "123";
            p1 = new JPanel();
            // this won't do much before realization.
    //        p1.setSize(500,150);
            p1.setPreferredSize(new Dimension(500, 150));
            
            p1.setLayout(new BorderLayout());
            
            what = new JLabel("");
     
            Item item = new Item();  // for multiple use
            bathroom.addItemListener(item);
            other.addItemListener(item);
            nurse.addItemListener(item);
            office.addItemListener(item);
            returnbutton = new JButton("Return");
            returnbutton.addActionListener(this);
            submit = new JButton("Submit");
            submit.addActionListener(this);
     
            // Not necessary: you can add the JLabel to p1 south section
            p2 = new JPanel();  //panel to add JLabel and TextField
     
    //        g1 = new GP();    
            g1 = new JPanel();
            p1.add(g1,BorderLayout.CENTER);
     
            g1.setBackground(//new Color(255, 255, 255));
                             Color.green);
            g1.add(idprompt);
            g1.add(idnumber);
            p2.add(submit);
            g1.add(what);
            //adds p2 to south (Bottom) Graphics Panel will be added to Center    
            p1.add(p2,BorderLayout.SOUTH);
            c1.add(p1);
            f1.pack();
            f1.setVisible(true);
        }
        
        public static void main(String[] args)	
        {
            new CheckBoxTest();
        }
    
        public class Item implements ItemListener
        {
            public void itemStateChanged(ItemEvent evt)
            {
                bathroomb = bathroom.isSelected();
                officeb = office.isSelected();
                nurseb = nurse.isSelected();
                otherb = other.isSelected();
                setComponents();
                System.out.printf("bathroomb = %b  officeb = %b  nurseb = %b  oficeb = %b%n",
                                   bathroomb, officeb, nurseb, otherb);
    /*
                // What happens if a radio button is deselected?
                // An ActionListener might work better for these.
                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)
        {
            if (event.getSource() == returnbutton)
            {
    //            f1.setVisible(true);
                // You may need to have a member variable to keep the
                // index selected and used in the calls to changeGUI
                // below. Here we'll use zero for now...
                changeGUI(0);
            }
            
            if (event.getSource() == submit)
            {
                try
                {
                    BufferedReader in = new BufferedReader(new FileReader("names.txt"));
              
                    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();
                } catch (IOException e) {
                    System.out.println("read error: " + e.getMessage());
                }
                System.out.printf("namearray = %s%nidarray = %s%n",
                                   Arrays.toString(namearray),
                                   Arrays.toString(idarray));
                enterstring = idnumber.getText();
                enter = Integer.parseInt(enterstring);
    
                // This loop not needed.
    //            for(int b = 0;  b< 3; b++)
    //            {
                    //number 1
                    if (enter==idarray[0])
                    {
                        changeGUI(0);
                    }
                    //number 2
                    else if (enter==teacher)
                    {
                        f1.dispose();
                    }
                    else if (enter == idarray[1])
                    {
                        changeGUI(1);
                    }
                    else if (enter == idarray[2])
                    {
                        changeGUI(2);
                    }
                    else
                    {
                        what.setText("Incorrect Login");
                    }
                    idnumber.setText(null);
    //            }
            }
        }
    
        private void changeGUI(int index)
        {
    //        g1.remove(idnumber);
    //        p1.remove(submit);
    //        g1.remove(what);
    //        g1.remove(idprompt);
            p2.remove(submit);
            p2.add(returnbutton);
            g1.removeAll();
            namelabel.setText(namearray[index]);
            g1.add(namelabel);
            g1.add(bathroom);
            g1.add(office);
            g1.add(nurse);
            g1.add(other);
            g1.revalidate();
            p2.revalidate();
            p1.repaint();
            System.out.println("changeGUI index = " + index);
    //        f1.dispose();
    //        f1.setVisible(true);
        }
    
        private void setComponents()
        {
            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.setVisible(true);
            }
            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.setVisible(true);
            }
            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);
            }
            g1.revalidate();
            p2.revalidate();
            //g1.setBackground(Color.red);
            // p2.add(returnbutton);
            // g1.add(time);
    //        f1.dispose();
    //        f1.setVisible(true);
            p1.repaint();
        }
    }

  4. #4
    carlos123 is offline Member
    Join Date
    Nov 2007
    Posts
    38
    Rep Power
    0

    Default thanks

    hello . hey thanks for the help on this. when it shows the "red screen" i want to display the users name that logged in , instead of just "array[0]" so after you choose the checkbox it displays the right name.




    Quote Originally Posted by hardwired View Post
    Java Code:
    C:\jexp>javac checkboxtest.java
    Note: checkboxtest.java uses or overrides a deprecated API.
    Note: Recompile with -Xlint:deprecation for details.
    
    C:\jexp>javac -Xlint:deprecation checkboxtest.java
    checkboxtest.java:150: warning: [deprecation] show() in java.awt.Window has been
     deprecated
                f1.show();
    The show method was deprecated in j2se 1.5. Look up the method in the Window api to see that it has been replaced by setVisible. If you are using an older version then setVisible will work okay (since 1.1);
    just nothing happens when i click the checkboxes
    Actually, they are radio buttons.
    The code logic in the ItemListener was faulty and the code to reset the gui components was buried/hidden/not_accessible in the actionPerformed "submit" block.
    Many changes and some suggestions.
    When you change/alter the components in a container you must tell the container to renew its layout. We do this with either the Container method validate or the JComponent method revalidate.
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.*;
    import java.text.SimpleDateFormat;
    import java.lang.Object;
    import java.io.*;
    import javax.swing.event.*;
    import javax.swing.JCheckBox;
     
    public class CheckBoxTest implements ActionListener
    {
        JFrame f1,f2;
        JPanel p1,p2,p3,p4,g1;
        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 CheckBoxTest()
        {   
            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("54343", 14);
            f1 = new JFrame("Hall Pass");
    //        f1.setSize(550,200); 
            Container c1 = f1.getContentPane();
            time = new JLabel("left at "+sdf.format(new Date(System.currentTimeMillis())));
            id = "123";
            p1 = new JPanel();
            // this won't do much before realization.
    //        p1.setSize(500,150);
            p1.setPreferredSize(new Dimension(500, 150));
            
            p1.setLayout(new BorderLayout());
            
            what = new JLabel("");
     
            Item item = new Item();  // for multiple use
            bathroom.addItemListener(item);
            other.addItemListener(item);
            nurse.addItemListener(item);
            office.addItemListener(item);
            returnbutton = new JButton("Return");
            returnbutton.addActionListener(this);
            submit = new JButton("Submit");
            submit.addActionListener(this);
     
            // Not necessary: you can add the JLabel to p1 south section
            p2 = new JPanel();  //panel to add JLabel and TextField
     
    //        g1 = new GP();    
            g1 = new JPanel();
            p1.add(g1,BorderLayout.CENTER);
     
            g1.setBackground(//new Color(255, 255, 255));
                             Color.green);
            g1.add(idprompt);
            g1.add(idnumber);
            p2.add(submit);
            g1.add(what);
            //adds p2 to south (Bottom) Graphics Panel will be added to Center    
            p1.add(p2,BorderLayout.SOUTH);
            c1.add(p1);
            f1.pack();
            f1.setVisible(true);
        }
        
        public static void main(String[] args)	
        {
            new CheckBoxTest();
        }
    
        public class Item implements ItemListener
        {
            public void itemStateChanged(ItemEvent evt)
            {
                bathroomb = bathroom.isSelected();
                officeb = office.isSelected();
                nurseb = nurse.isSelected();
                otherb = other.isSelected();
                setComponents();
                System.out.printf("bathroomb = %b  officeb = %b  nurseb = %b  oficeb = %b%n",
                                   bathroomb, officeb, nurseb, otherb);
    /*
                // What happens if a radio button is deselected?
                // An ActionListener might work better for these.
                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)
        {
            if (event.getSource() == returnbutton)
            {
    //            f1.setVisible(true);
                // You may need to have a member variable to keep the
                // index selected and used in the calls to changeGUI
                // below. Here we'll use zero for now...
                changeGUI(0);
            }
            
            if (event.getSource() == submit)
            {
                try
                {
                    BufferedReader in = new BufferedReader(new FileReader("names.txt"));
              
                    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();
                } catch (IOException e) {
                    System.out.println("read error: " + e.getMessage());
                }
                System.out.printf("namearray = %s%nidarray = %s%n",
                                   Arrays.toString(namearray),
                                   Arrays.toString(idarray));
                enterstring = idnumber.getText();
                enter = Integer.parseInt(enterstring);
    
                // This loop not needed.
    //            for(int b = 0;  b< 3; b++)
    //            {
                    //number 1
                    if (enter==idarray[0])
                    {
                        changeGUI(0);
                    }
                    //number 2
                    else if (enter==teacher)
                    {
                        f1.dispose();
                    }
                    else if (enter == idarray[1])
                    {
                        changeGUI(1);
                    }
                    else if (enter == idarray[2])
                    {
                        changeGUI(2);
                    }
                    else
                    {
                        what.setText("Incorrect Login");
                    }
                    idnumber.setText(null);
    //            }
            }
        }
    
        private void changeGUI(int index)
        {
    //        g1.remove(idnumber);
    //        p1.remove(submit);
    //        g1.remove(what);
    //        g1.remove(idprompt);
            p2.remove(submit);
            p2.add(returnbutton);
            g1.removeAll();
            namelabel.setText(namearray[index]);
            g1.add(namelabel);
            g1.add(bathroom);
            g1.add(office);
            g1.add(nurse);
            g1.add(other);
            g1.revalidate();
            p2.revalidate();
            p1.repaint();
            System.out.println("changeGUI index = " + index);
    //        f1.dispose();
    //        f1.setVisible(true);
        }
    
        private void setComponents()
        {
            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.setVisible(true);
            }
            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.setVisible(true);
            }
            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);
            }
            g1.revalidate();
            p2.revalidate();
            //g1.setBackground(Color.red);
            // p2.add(returnbutton);
            // g1.add(time);
    //        f1.dispose();
    //        f1.setVisible(true);
            p1.repaint();
        }
    }

Similar Threads

  1. iterator issues
    By orchid in forum New To Java
    Replies: 2
    Last Post: 08-12-2008, 01:43 PM
  2. Can we add JCheckBox array into JTextArea?
    By nancyhung in forum AWT / Swing
    Replies: 1
    Last Post: 02-17-2008, 12:07 AM
  3. Issues with Jva I.O
    By Annatar01 in forum New To Java
    Replies: 0
    Last Post: 02-08-2008, 01:16 AM
  4. Array issues
    By Neo82 in forum New To Java
    Replies: 1
    Last Post: 12-31-2007, 03:22 AM
  5. Replies: 1
    Last Post: 08-08-2007, 01:25 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •