Results 1 to 7 of 7
- 01-24-2010, 08:29 AM #1
Member
- Join Date
- Jan 2010
- Posts
- 12
- Rep Power
- 0
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 10:06 AM. Reason: Forgot a class
- 01-24-2010, 09:41 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 17
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.
- 01-24-2010, 10:07 AM #3
Member
- Join Date
- Jan 2010
- Posts
- 12
- Rep Power
- 0
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.
-
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.
- 01-24-2010, 06:48 PM #5
Member
- Join Date
- Jan 2010
- Posts
- 12
- Rep Power
- 0
I'll be sure to check again. Thanks for the input :)
- 01-24-2010, 07:04 PM #6
Member
- Join Date
- Jan 2010
- Posts
- 12
- Rep Power
- 0
Problem solved. Thanks Fubarable, along with pbrockway :D
Last edited by ecliptical; 01-24-2010 at 10:35 PM.
-
Similar Threads
-
Times without dates.
By JavaJuJitZu in forum Advanced JavaReplies: 14Last Post: 01-17-2010, 11:54 PM -
Activate JComboBox 1 when object is selected in JComboBox 2...
By bahumbaba in forum AWT / SwingReplies: 2Last Post: 12-10-2009, 02:58 PM -
Difference in seconds between two times
By jazzy639 in forum New To JavaReplies: 11Last Post: 09-26-2009, 07:58 PM -
Value stored in variable not displaying.
By mainy in forum New To JavaReplies: 5Last Post: 03-10-2009, 05:52 PM -
SwingWorker problem!!! How can I run it 2 times or more?
By davigre in forum AWT / SwingReplies: 3Last Post: 10-02-2008, 06:48 AM
Bookmarks