View Single Post
  #3 (permalink)  
Old 11-02-2007, 09:30 PM
hardwired hardwired is offline
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
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.
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(); } }
Reply With Quote