Results 1 to 7 of 7
  1. #1
    ecliptical is offline Member
    Join Date
    Jan 2010
    Posts
    12
    Rep Power
    0

    Default JComboBox displaying variable two times, why?

    Hi guys,
    So I was able to set up JComboBox perfectly for my first class, but for my second class when a teacher is added to the class the ComboBox displays the teacher twice.. why? I looked over my code yet I couldn't find any error with it.

    All help is appreciated,
    Thanks in advance.

    -Ecliptical

    Here's the code needed to run the program
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.lang.*;
    import java.util.*;
    
    
    public class BHSSDatabaseTestDriver
    {
       public static void main (String[] args)
      {
      //Create and set up the window.
      JFrame frame = new JFrame("The BHSS Database");
      frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
      BHSSDatabasePanelTABBEDTest tabbedPanel = new BHSSDatabasePanelTABBEDTest();
      //Add content to the window.
      
      //Display the window.
        frame.getContentPane().add(tabbedPanel.primary);
        frame.pack();
        frame.setVisible(true); 
      
    }
    }
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.lang.*;
    import java.util.*;
    
    public class BHSSDatabasePanelTABBEDTest extends JPanel implements ActionListener
    {
      private JLabel mainDisplay, line1, studNameLabel, osisLabel, cutsLabel, examGradeLabel1, examGradeLabel2, examGradeLabel3, examGradeLabel4, finalExamLabel, homeworkLabel, homeworkCLabel, studentListLabel, teacherNameLabel, teacherListLabel, classNameLabel,
        classListLabel, finalGrade, studentOsisLabel, studentAddressLabel, teacherClassLabel, teacherAddressLabel, yearsServiceLabel, teacherDeptLabel, teacherSalaryLabel;
      private JTabbedPane tabbedPanel;
      private JPanel panel1, panel2, panel3, panel4;
      public static JPanel primary;
      // private JComponent panel1, panel2, panel3, panel4; 
      private JTextField studentNameInput, studentAddressInput, osisInput, cutsInput, homeworkInput, homeworkCInput, examGrade1Input, examGrade2Input, examGrade3Input, examGrade4Input, finalExamInput, 
        projectGrade1Input, projectGrade2Input, projectGrade3Input, yearsServiceInput, teacherAddressInput, teacherNameInput, classNameInput, teacherClassInput, teacherSalaryInput, teacherDeptInput;
      private JComboBox studentNameList, studentNameList2, teacherNameList, classNameList;
      private JButton button1, button2, button3, button4, button5, button6, button7;
      private ArrayList <Teacher> teachers;
      private Teacher [] teachers1;
      
      public BHSSDatabasePanelTABBEDTest()
      {
        teachers = new ArrayList<Teacher>();
        teachers1 = new Teacher[1];
        
        panel2 = new JPanel();
        teacherNameLabel = new JLabel("Name of the Teacher to Add");
        teacherNameInput = new JTextField(20);
        teacherAddressLabel = new JLabel("Teacher's Address: ");
        teacherAddressInput = new JTextField(20);
        teacherClassLabel = new JLabel("Enter the class which this teacher teaches.");
        teacherClassInput = new JTextField(8);
        teacherDeptLabel = new JLabel("Which Department?");
        teacherDeptInput = new JTextField(9);
        teacherSalaryLabel = new JLabel("How much does this teacher make?");
        teacherSalaryInput = new JTextField(6);
        yearsServiceLabel = new JLabel("How many years in service?");
        yearsServiceInput = new JTextField(2);
        button3 = new JButton("Hire this teacher!");
        button3.addActionListener(this);
        teacherListLabel = new JLabel("Select the teacher which you wish to remove.");
        teacherNameList = new JComboBox(teachers1);
        button4 = new JButton("Fire This Teacher!");
        button4.addActionListener(this);
        
        panel2.add(teacherNameLabel);
        panel2.add(teacherNameInput);
        panel2.add(teacherAddressLabel);
        panel2.add(teacherAddressInput);
        panel2.add(teacherClassLabel);
        panel2.add(teacherClassInput);
        panel2.add(teacherDeptLabel);
        panel2.add(teacherDeptInput);
        panel2.add(teacherSalaryLabel);
        panel2.add(teacherSalaryInput);
        panel2.add(yearsServiceLabel);
        panel2.add(yearsServiceInput);
        panel2.add(button3);
        button3.addActionListener(this);
        panel2.add(teacherListLabel);
        panel2.add(teacherNameList);
        panel2.add(button4);
        button4.addActionListener(this);
        
        tabbedPanel = new JTabbedPane();
        add(tabbedPanel);
        tabbedPanel.addTab("Add/Remove Teacher", panel2);
        
        primary = new JPanel();
        primary.setBackground (Color.white);
        primary.setPreferredSize (new Dimension(500, 475));
        primary.setLayout( new BorderLayout() );
        primary.add( tabbedPanel, BorderLayout.CENTER );
        
        tabbedPanel.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
        setPreferredSize(new Dimension(345, 550));
      }
      
      public void actionPerformed(ActionEvent e)
      {
        if(e.getSource() == button3)
        {
          String nameT, addressT, deptT, classT;
          int years;
          double salaryT;
          Teacher teacher = new Teacher();
          nameT = teacherNameInput.getText();
          addressT = teacherAddressInput.getText();
          deptT = teacherDeptInput.getText();
          classT = teacherClassInput.getText();
          years = Integer.parseInt(yearsServiceInput.getText());
          salaryT = Double.parseDouble(teacherSalaryInput.getText());
          teacher.setName(nameT);
          teacher.setAddress(addressT);
          teacher.setDepartment(deptT);
          teacher.setClassTaught(classT);
          teacher.setYearsInService(years);
          teacher.setSalary(salaryT);
          teacherNameList.addItem(teacher.getName());
          teachers.add(teacher);
        }
      } 
    }
    Java Code:
    public class Teacher
    {
      private String teacherName, address, department, classTaught;
      private int yearsInService;
      private double salary;
      public Teacher()
      {
        teacherName = "";
        address = "";
        yearsInService = 0;
        salary = 0;
        department = "";
        classTaught = "";
      }
      
      public void setName(String teacherName1)
      {
        teacherName = teacherName1;
      }
      
      public String getName()
      {
        return teacherName;
      }
      
      public void setAddress(String address1)
      {
        address = address1;
      }
      
      public String getAddress()
      {
        return address;
      }
      
      public void setYearsInService(int yearsInService1)
      {
        yearsInService = yearsInService1;
      }
      
      public int getYearsInService()
      {
        return yearsInService;
      }
      
      public void setSalary(double salary1)
      {
        salary = salary1;
      }
      
      public double getSalary()
      {
        return salary;
      }
      
      public void setDepartment(String department1)
      {
        department = department1;
      }
      
      public String getDepartment()
      {
        return department;
      }
      
      public void setClassTaught(String classTaught1)
      {
        classTaught = classTaught1;
      }
      
      public String getClassTaught()
      {
        return classTaught;
      }
      
    
      
    }
    Last edited by ecliptical; 01-24-2010 at 09:06 AM. Reason: Forgot a class

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    How many times does the actionPerformed() method get called when you click on button3? (addTeacherBut would be a nicer name). You can tell by adding a System.out.println("Ding!") to the start of the if block.

    Then ask yourself why it might be being called that number of times.

  3. #3
    ecliptical is offline Member
    Join Date
    Jan 2010
    Posts
    12
    Rep Power
    0

    Default

    I did as you said, and indeed it does call it twice.
    I have no slight idea as to why though, but I'd guess it has something to do with the loop.

    Thanks for the tip pbrockway.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    It has nothing to do with a loop. Search through your file and find every reference to button3, and see what you are doing with the button at that time.

  5. #5
    ecliptical is offline Member
    Join Date
    Jan 2010
    Posts
    12
    Rep Power
    0

    Default

    I'll be sure to check again. Thanks for the input :)

  6. #6
    ecliptical is offline Member
    Join Date
    Jan 2010
    Posts
    12
    Rep Power
    0

    Default

    Problem solved. Thanks Fubarable, along with pbrockway :D
    Last edited by ecliptical; 01-24-2010 at 09:35 PM.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

Similar Threads

  1. Times without dates.
    By JavaJuJitZu in forum Advanced Java
    Replies: 14
    Last Post: 01-17-2010, 10:54 PM
  2. Replies: 2
    Last Post: 12-10-2009, 01:58 PM
  3. Difference in seconds between two times
    By jazzy639 in forum New To Java
    Replies: 11
    Last Post: 09-26-2009, 06:58 PM
  4. Value stored in variable not displaying.
    By mainy in forum New To Java
    Replies: 5
    Last Post: 03-10-2009, 04:52 PM
  5. Replies: 3
    Last Post: 10-02-2008, 05:48 AM

Tags for this Thread

Posting Permissions

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