views:

38

answers:

1

i have a GUI that is mainly a survey, when the survey is finished the information would be extract to a table , i am currently having trouble getting all the information , i created a Actionlistener called finish handler which is attached to the survey GUI button "Finished" so when the finish button is clicked it will extract all the information and store it into my DataStorage class.below is my survey GUI, the actionListener class , and the datastorage class.

/*********frame3 Survey*********/
   frame3 = new JFrame();
   frame3.setTitle("Student Survey");
   frame3.setLayout(new MigLayout());
   frame3.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
   btnFinish = new JButton("Finish!");
   btnFinish.addActionListener(new FinishHandler());
   pSSFinish = new JPanel();
   pSSFinish.add(btnFinish);
   String[] levelStrings = { "1.1", "1.2", "2.1", "2.2", "3.1", "3.2" };
   String[] DipStrings = { "Info-Communications", "Computer Engineering", "Mechatronics", "Electronics"};   
   jcbDip = new JComboBox(DipStrings);
   jcbLvl = new JComboBox(levelStrings);
   jcbDip.addActionListener(new FinishHandler());
   jcbLvl.addActionListener(new FinishHandler());
   //pSSInfo
   pSSInfo = new JPanel();
   pSSInfo.setBorder(loweredetched);
   pSSInfo.setLayout(new MigLayout());
   lblSSTitleName = new JLabel("Name:");
   lblSSTitleID = new JLabel("Student ID:");
   lblSSName = new JLabel("");
   lblSSID = new JLabel("");
   pSSInfo.add(lblSSTitleName);
   pSSInfo.add(lblSSName,"wrap");
   pSSInfo.add(lblSSTitleID);
   pSSInfo.add(lblSSID);

   //pSSCourse
   pSSCourse = new JPanel();
   pSSCourse.setBorder(titleCourse);
   pSSCourse.setLayout(new MigLayout());
   lblSSCourseDip = new JLabel("Diploma:");
   lblSSCourseLvl = new JLabel("Level:");
   lblSSCourseCre = new JLabel("No of credits:");
   txfSSCourse = new JTextField(8);
   pSSCourse.add(lblSSCourseDip);
   pSSCourse.add(jcbLvl,"wrap");
   pSSCourse.add(lblSSCourseLvl);
   pSSCourse.add(jcbDip,"wrap");
   pSSCourse.add(lblSSCourseCre);
   pSSCourse.add(txfSSCourse);

   //pSSAge
   pSSAge = new JPanel();
   pSSAge.setBorder(raisedbevel);
   lblSSAge = new JLabel("Age");
   txfSSAge = new JTextField(8);
   pSSAge.add(lblSSAge);
   pSSAge.add(txfSSAge);
   //pSSGender
   String male = "Male";
   String female = "Female";
   btnMale = new JRadioButton(male);
   btnMale.addActionListener(new FinishHandler());
   btnFemale = new JRadioButton(female);
   btnFemale.addActionListener((new FinishHandler()));
   ButtonGroup group = new ButtonGroup();
   group.add(btnMale);
   group.add(btnFemale);
   pSSGender = new JPanel();
   pSSGender.setBorder(titleGender);
   pSSGender.setLayout(new MigLayout());
   pSSGender.add(btnMale,"wrap");
   pSSGender.add(btnFemale);
   //pSSMisc
   pSSMisc = new JPanel();
   pSSMisc.setBorder(titleMisc);
   pSSMisc.setLayout(new MigLayout());
   cbMiscPt = new JCheckBox("Working Part Time");
   cbMiscPt.addItemListener(new FinishHandler());
   cbMiscPc = new JCheckBox("Have own PC/NoteBook");
   cbMiscPc.addItemListener(new FinishHandler());
   cbMiscChild = new JCheckBox("Have children");
   cbMiscChild.addItemListener(new FinishHandler());
   pSSMisc.add(cbMiscPt,"wrap");
   pSSMisc.add(cbMiscPc,"wrap");
   pSSMisc.add(cbMiscChild);

   frame3.add(pSSFinish,"dock south");
   frame3.add(pSSInfo,"dock north");
   frame3.add(pSSCourse,"dock west");
   frame3.add(pSSAge,"wrap, grow");
   frame3.add(pSSGender);
   frame3.add(pSSMisc,"dock east");

   frame3.pack();
   frame3.setVisible(false);

/Finish handler/

class FinishHandler implements ActionListener, ItemListener
  {
   public void actionPerformed(ActionEvent e)
   { //System.out.println("current login in student is: "+ds.getStudent(currentUser).getUser());
    ds.getStudent(currentUser).setDiploma((String)jcbDip.getSelectedItem());
    System.out.println(ds.getStudent(currentUser).getDiploma());//debug
    ds.getStudent(currentUser).setLevel((String)jcbLvl.getSelectedItem());
    System.out.println(ds.getStudent(currentUser).getLevel());//debug
    ds.getStudent(currentUser).setCredits(txfSSCourse.getText());
    System.out.println(ds.getStudent(currentUser).getCredits());//debug
    ds.getStudent(currentUser).setAge(txfSSAge.getText());
    System.out.println(ds.getStudent(currentUser).getAge());
    if(btnMale.getText().equals("Male"))
    {
     ds.getStudent(currentUser).setGender("Male");
     System.out.println(ds.getStudent(currentUser).getGender());//debug
    }

    else if(btnFemale.getText().equals("Female"))
    {
     ds.getStudent(currentUser).setGender("Female");
     System.out.println(ds.getStudent(currentUser).getGender());//debug
    }
    if(btnMale.getText().equals(""))
    {
     ds.getStudent(currentUser).setGender("Did not set");
     System.out.println(ds.getStudent(currentUser).getGender());//debug
    }
    } 

     public void itemStateChanged(ItemEvent e) 
     {

        Object source = e.getItemSelectable();

         if (source == cbMiscPt) 
         {
          ds.getStudent(currentUser).setPartTime(true);
          System.out.println(ds.getStudent(currentUser).getPartTime());//debug

         } 
          else if (source == cbMiscPc) 
         {
             ds.getStudent(currentUser).setHavePc(true);
             System.out.println(ds.getStudent(currentUser).getHavePc());//debug
         } 
          else if (source == cbMiscChild) 
         {
             ds.getStudent(currentUser).setHaveChild(true);
             System.out.println(ds.getStudent(currentUser).getHaveChild());//debug
         }

        if (e.getStateChange() == ItemEvent.DESELECTED)
        {
         if (source == cbMiscPt) 
         {
          ds.getStudent(currentUser).setPartTime(false);
          System.out.println(ds.getStudent(currentUser).getPartTime());//debug

         } 
          else if (source == cbMiscPc) 
         {
             ds.getStudent(currentUser).setHavePc(false);
             System.out.println(ds.getStudent(currentUser).getHavePc());//debug
         } 
          else if (source == cbMiscChild) 
         {
             ds.getStudent(currentUser).setHaveChild(false);
             System.out.println(ds.getStudent(currentUser).getHaveChild());//debug
         }
        }

    } 


  }

/*DataSotrage class/

import java.util.*;

public class DataStorage 
{
    HashMap<String, Student> students = new HashMap<String, Student>();  
    HashMap<String, Staff> staffMembers = new HashMap<String, Staff>();  
    //Default constructor
    public DataStorage(){
    }

    public void addStaffMember(Staff aAcc) 
    {
     staffMembers.put(aAcc.getUser(),aAcc);
    }

    public void addStudentMember(Student aAcc)
    {
     students.put(aAcc.getUser(),aAcc);
    }

   public Staff getStaffMember(String user)
   {
   return staffMembers.get(user);
   }

   public Student getStudent(String user)
   {
    return students.get(user);
   }

}
+1  A: 

The design you have gone for is very chatty. You have attached a new FinishHandler to each widget, when you only need to set it to your btnFinish. The way you have it set, for each change in the GUI, you will update your DataStorage object. Instead, by only adding it to the btnFinish, you can reduce the amount of times your Handler gets called and can simplify your process. Of course, you will then need to consolidate your itemStateChanged and actionPerformed methods. This will then give you the option of initializing your DataStorage object when you get into actionPerformed, and you can then initiate your persistence process. Look at SwingWorker and concepts around concurrency in Swing as you look into moving your data from memory to storage.

EDIT:

By 'consolidate your itemStateChanged and actionPerformed methods,' I mean move all of you data gathering for your checkboxes into the action performed method. This way, when you click on your finish button, you can gather all of the data that you need for your DataStorage object.

akf
thanks for the reply, i do not understand by what you mean by "consolidate your itemStateChanged and actionPerformed methods."i figured i put a new FinishHandler to each widget is because so that when a change is made like chaning gender from Male to female it will regconise it and when the Finish button is pressed all is stored into the respective DataStorage using the respective accessor method
kyrogue
i have removed the Handlers for the widgets but not for the JCheckBoxes, because i need a itemStateChanged event to check whether or not is it ticked or unticked so ask to get the information, is this correct?
kyrogue
General tip: Why do you not pair up with one of the other students from your course? Pair programming works a lot better, then trying to find everything out yourself!
Verhagen
akf, from the java docs, to listen for checkboxes i would need a ItemListener, so how can i implement that into a ActionListener?Verhagen: because we all are still students working together so immaturity conflicts will happen.
kyrogue
In my opinion, you don't need to listen for the changes. It would be easier to query the state (ie, `cbMiscPt.isSelected()`) when you are populating your `DataStorage`.
akf