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

    Question JComboBox problems

    Hello all,

    I'm having some problems with my program regarding JComboBoxes.
    My JComboBox won't display an array of StudentClass objects. :confused:

    Also, how would I be able to update the JComboBox which contains the array which would hold the StudentClass objects that that user input?

    Once I can get that to work, I need to figure out how to "delete" the selected item from the JComboBox.

    How would I go about doing this?

    Also, where the GUI says "This student's current average is: 0.0%."
    I've been trying to figure out as to why it doesn't update when "Add this student!" is pressed.

    All help is appreciated.
    Thanks in advance!
    -Ecliptical

    Here are the 3 classes needed to run the program.

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.JTabbedPane;
    import javax.swing.JButton;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JFrame;
    import javax.swing.JComponent;
    import javax.swing.SwingUtilities;
    import javax.swing.JSeparator;
    import javax.swing.JComboBox;
    import java.awt.event.KeyEvent;
    import java.lang.String;
    import java.util.ArrayList;
    
    public class BHSSDatabasePanelTABBED 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, teacherDeptLabel, teacherSalaryLabel;
      private JTabbedPane tabbedPanel;
      private JPanel panel1, panel2, panel3, panel4;
      public static JPanel primary;
      // private JComponent panel1, panel2, panel3, panel4; //<<<---- What exactly does this do?
      private JTextField studentNameInput, studentAddressInput, osisInput, cutsInput, homeworkInput, homeworkCInput, examGrade1Input, examGrade2Input, examGrade3Input, examGrade4Input, finalExamInput, 
        projectGrade1Input, projectGrade2Input, projectGrade3Input, teacherNameInput, classNameInput, teacherClassInput, teacherSalaryInput, teacherDeptInput;
      private JComboBox studentNameList, studentNameList2, teacherNameList, classNameList;
      private JButton button1, button2, button3, button4, button5, button6, button7;
      private ArrayList <StudentClass> students;
      private StudentClass [] students1;
      private JSeparator separator1, separator2, separator3, separator4;
      private double average;
      private int index;
      
      public BHSSDatabasePanelTABBED()
      {
        students = new ArrayList<StudentClass>();
        average = 0;
        index = 1;
        students1 = new StudentClass[150];
        
        
        panel1 = new JPanel();
        
        //tabbedPanel.addTab("Add/Remove A Person", panel2);
        studNameLabel = new JLabel("Input the student's name which you wish to add.");
        studentNameInput = new JTextField(20);
        studentAddressLabel = new JLabel("Students Address");
        studentAddressInput = new JTextField(15);
        osisLabel = new JLabel("Input the student's OSIS.");
        osisInput = new JTextField(9);
        cutsLabel = new JLabel("Input the number of cuts");
        cutsInput = new JTextField(3);
        examGradeLabel1 = new JLabel("Exam 1 Grade");
        examGrade1Input = new JTextField(3);
        examGradeLabel2 = new JLabel("Exam 2 Grade");
        examGrade2Input = new JTextField(3);
        examGradeLabel3 = new JLabel("Exam 3 Grade");
        examGrade3Input = new JTextField(3);
        examGradeLabel4 = new JLabel("Exam 4 Grade");
        examGrade4Input = new JTextField(3);
        finalExamLabel = new JLabel("Final Exam Grade");
        finalExamInput = new JTextField(3);
        homeworkLabel = new JLabel("Number of HW Completed");
        homeworkInput = new JTextField(3);
        homeworkCLabel = new JLabel("Total Number of HW Assignments");
        homeworkCInput = new JTextField (3);
        button1 = new JButton("Add A Student!");
        button1.addActionListener(this);
        finalGrade = new JLabel("This student's final grade is: " + average + "%");
        studentListLabel = new JLabel("Select the student who you wish to delete.");
        studentNameList = new JComboBox(students1); 
        mainDisplay = new JLabel("Welcome to the BHSS Database");
    //<--- //*************How do display the array. Code isn't working. Tried looking at API***********
        button2 = new JButton("Drop this student!");
        button2.addActionListener(this);
        
        panel1.add(mainDisplay);
        panel1.add(studNameLabel);
        panel1.add(studentNameInput);
        panel1.add(osisLabel);
        panel1.add(osisInput);
        panel1.add(cutsLabel);
        panel1.add(cutsInput);
        panel1.add(studentAddressLabel);
        panel1.add(studentAddressInput);
        panel1.add(examGradeLabel1);
        panel1.add(examGrade1Input);
        panel1.add(examGradeLabel2);
        panel1.add(examGrade2Input);
        panel1.add(examGradeLabel3);
        panel1.add(examGrade3Input);
        panel1.add(examGradeLabel4);
        panel1.add(examGrade4Input);
        panel1.add(finalExamLabel);
        panel1.add(finalExamInput);
        panel1.add(homeworkLabel);
        panel1.add(homeworkInput);
        panel1.add(homeworkCLabel);
        panel1.add(homeworkCInput);
        panel1.add(button1); //Add this student!
        panel1.add(finalGrade);
        panel1.add(studentListLabel);
        panel1.add(studentNameList);
        panel1.add(button2); //Delete this student!
        
        line1 = new JLabel("Please choose the tab which best suits your needs");
        
        tabbedPanel = new JTabbedPane();
        add(tabbedPanel);
        tabbedPanel.addTab("Add/Remove Student", panel1);
        
        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() == button1)
        {
          StudentClass student = new StudentClass();
          String a, d;
          int b, c, f;
          a = studentNameInput.getText();
          b = Integer.parseInt(osisInput.getText());
          c = Integer.parseInt(cutsInput.getText());
          d = studentAddressInput.getText();
          f = Integer.parseInt(osisInput.getText());
          student.setName(a);
          student.setAddress(d);
          student.setOSIS(f);
          student.setNumOfCuts(c);
          average = student.paradigm(Double.parseDouble(examGrade1Input.getText()), Double.parseDouble(examGrade2Input.getText()), Double.parseDouble(examGrade3Input.getText()),
          Double.parseDouble(examGrade4Input.getText()), Double.parseDouble(finalExamInput.getText()), Double.parseDouble(homeworkInput.getText()), Double.parseDouble(homeworkCInput.getText()));
          students1[index] = student;
          index++;
          //How can I store the student that was created in an Array?
        }
        
        else if(e.getSource() == button2)
        {
        //How to delete a student through JCombobox
        }
    }
      }
    Java Code:
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JFrame;  
    import javax.swing.JComponent;
    import javax.swing.SwingUtilities;
    import javax.swing.UIManager;
    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.GridLayout;
    import java.awt.event.KeyEvent;
    
    
    public class BHSSDatabase
    {
       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);
      BHSSDatabasePanelTABBED tabbedPanel = new BHSSDatabasePanelTABBED();
      //Add content to the window.
      
      //Display the window.
        frame.getContentPane().add(tabbedPanel.primary);
        frame.pack();
        frame.setVisible(true); 
      
    }
    }
    Java Code:
    import java.lang.String;
    
    public class StudentClass
    {
      private String name, address;
      private int osis, numOfCuts;
      private double finalAvg, exam1, exam2, exam3, exam4, finalExam;
      
      public StudentClass()
      {
        name = "";
        address = "";
        osis = 0;
        numOfCuts = 0;
        finalAvg = 0;
      }
      
      
      public void setName(String name1)
      {
        name = name1;
      }
      
      public String getName()
      {
        return name;
      }
      
      public void setAddress(String address1)
      {
        address = address1;
      }
      
      public String getAddress()
      {
        return address;
      }
      
      public void setOSIS(int osis1)
      {
        osis = osis1;
      }
      
      public int getOSIS()
      {
        return osis;
      }
      
      public void setNumOfCuts(int numOfCuts1)
      {
        numOfCuts = numOfCuts1;
      }
      
      public int getNumOfCuts()
      {
        return numOfCuts;
      }
      
      public double paradigm(double exam1, double exam2, double exam3, double exam4, double finalExam, double hwCompleted, double hwTotal)
      {
        double avg = 0.0;
        
        avg = (((exam1 + exam2 + exam3 + exam4)/(4)) * .7 + (finalExam * .15) + ((hwCompleted/hwTotal)*15));
    return avg;
      }
      
      public double paradigm(double exam1, double exam2, double exam3, double finalExam, double projectGrade, double projectTotalPoints, double hwCompleted, double hwTotal)
      {
        double avg = 0.0;
        
        avg = (((exam1 + exam2 + exam3)/3)*.35 + (finalExam*.15) + ((projectGrade/projectTotalPoints)*35) + ((hwCompleted/hwTotal)*15));
        return avg;
      }
      
      public double paradigm(double project1, double project2, double project3, double project4, double project5, double termAbstract, double termProject, double hwCompleted, double hwTotal)
      {
        double avg = 0.0;
        avg = (((((project1 + project2 + project3 + project4 + project5)/1.5) + (termAbstract))/2)*.70) + (termProject * .15) + ((hwCompleted/hwTotal) * 15);
        return avg;
      }
      
    
        
    }
    Last edited by ecliptical; 01-24-2010 at 01:04 AM. Reason: SSCCE

  2. #2
    sky
    sky is offline Member
    Join Date
    Nov 2009
    Posts
    96
    Rep Power
    0

    Default

    Try creating the ComboBox using a model:

    Java Code:
    filmModel = new DefaultComboBoxModel(days);
    filmList = new JComboBox(filmModel);
    Then, add/remove elements from the model. If the changes are not shown inmediatly, check out the methods validate() and repaint().

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

    Default

    Indeed, updating a model should work well and shouldn't even require a call to validate nor repaint. As for the original poster's problems and code, as the saying goes, tl;dr. Have a look at creating and posting an SSCCE.

Similar Threads

  1. Replies: 2
    Last Post: 12-10-2009, 01:58 PM
  2. I need help with JComboBox
    By eva21 in forum New To Java
    Replies: 1
    Last Post: 11-28-2008, 10:05 PM
  3. JCombobox
    By daniel50096230 in forum NetBeans
    Replies: 1
    Last Post: 09-21-2008, 12:23 PM
  4. Help with jComboBox
    By Marcus in forum AWT / Swing
    Replies: 2
    Last Post: 07-01-2007, 11:08 PM
  5. jcombobox
    By Freddie in forum AWT / Swing
    Replies: 4
    Last Post: 05-11-2007, 12: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
  •