Sponsors: Michael Fertik - Best JAVA Web hosting Company & 30% off


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-24-2010, 07:29 AM
Member
 
Join Date: Jan 2010
Posts: 12
Rep Power: 0
ecliptical is on a distinguished road
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
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); 
  
}
}
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);
    }
  } 
}
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
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 01-24-2010, 08:41 AM
Senior Member
 
Join Date: Feb 2009
Posts: 661
Rep Power: 2
pbrockway2 is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 01-24-2010, 09:07 AM
Member
 
Join Date: Jan 2010
Posts: 12
Rep Power: 0
ecliptical is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 01-24-2010, 01:12 PM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 8,429
Rep Power: 11
Fubarable is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 01-24-2010, 05:48 PM
Member
 
Join Date: Jan 2010
Posts: 12
Rep Power: 0
ecliptical is on a distinguished road
Default
I'll be sure to check again. Thanks for the input
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 01-24-2010, 06:04 PM
Member
 
Join Date: Jan 2010
Posts: 12
Rep Power: 0
ecliptical is on a distinguished road
Default
Problem solved. Thanks Fubarable, along with pbrockway

Last edited by ecliptical; 01-24-2010 at 09:35 PM.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 01-24-2010, 06:09 PM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 8,429
Rep Power: 11
Fubarable is on a distinguished road
Default
Originally Posted by ecliptical View Post
Problem solved. Thanks Fubarable
You're welcome, but actually pbrockway alerted you to this first.
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Tags
combobox

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Times without dates. JavaJuJitZu Advanced Java 14 01-17-2010 10:54 PM
Activate JComboBox 1 when object is selected in JComboBox 2... bahumbaba AWT / Swing 2 12-10-2009 01:58 PM
Difference in seconds between two times jazzy639 New To Java 11 09-26-2009 06:58 PM
Value stored in variable not displaying. mainy New To Java 5 03-10-2009 04:52 PM
SwingWorker problem!!! How can I run it 2 times or more? davigre AWT / Swing 3 10-02-2008 05:48 AM


Java Forums is supported by the best jsp hosting.

All times are GMT +2. The time now is 05:05 AM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org