Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Trouble adding objects to JComboBox

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Location
    West Virginia
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Trouble adding objects to JComboBox

    I'm trying to add a string and double value from two text field into an object, then have that object be placed in a combo box.

    I have a button that adds an employee object with a (string, double) parameter.
    private void btnAddHourlyActionPerformed(java.awt.event.ActionEvent evt) {                                             
            double hoursWorked = Double.parseDouble(tfHoursWorked.getText());
            String empID = tfEmpID.getText();
            HourlyEmployee hrEmp = new HourlyEmployee(empID, hoursWorked);
            myList.add(hrEmp);
        }

    And a combo box that I want to first clear, then add the hrEmp object into it using the payStub() method in my HourlyEmployee class.
    private void btnDisplayHourlyActionPerformed(java.awt.event.ActionEvent evt) {                                                 
            cbEmployeeList.removeAllItems();
            cbEmployeeList.addItem(hrEmp);
        }

    Here's my HourlyEmployee class:
    /**
     * Class for HourlyEmployee.
     * @author Marcus
     */
    public class HourlyEmployee implements Payday {
        private String employeeID;
        private double hoursWorked;
        /**
         * Constructor for an HourlyEmployee object
         * @param inEmployeeID HourlyEmployee ID
         * @param inHoursWorked Number of hours worked by the HourlyEmployee
         */
        public HourlyEmployee (String inEmployeeID, double inHoursWorked) {
            this.employeeID = inEmployeeID;
            this.hoursWorked = inHoursWorked;
        }
        /**
         * Returns the employee and earnings
         * @return a String containing the employee ID and earnings
         */
        @Override
        public String payStub() {
            return "Hourly employee: " + employeeID +  " earned $" + findEarnings();
        }
        /**
         * Returns the employee earnings
         * @return the employee earnings
         */
        @Override
        public double findEarnings() {
            /*
            if (hoursWorked <= 40)
            return hoursWorked * 10.00;
            else
            return (hoursWorked * 15) - 200;
            **/
            if (hoursWorked < 40)
                return hoursWorked * 10;
            else
                return (hoursWorked - 40) * 15 + 40 * 10;
        }
    }

    And here's the GUI if that helps:
    import java.util.ArrayList;
    public class PaydayGUI extends javax.swing.JFrame {
        ArrayList<Payday> myList = new ArrayList<>();
        /**
         * Creates new form PaydayGUI
         */
        public PaydayGUI() {
            initComponents();
            tfEmpID.setText("");
            tfHoursWorked.setText("");
            tfSalary.setText("");
        }
     
        /**
         * This method is called from within the constructor to initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is always
         * regenerated by the Form Editor.
         */
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
        private void initComponents() {
     
            jPanel1 = new javax.swing.JPanel();
            cleardisplayPanel = new javax.swing.JPanel();
            btnDisplayHourly = new javax.swing.JButton();
            btnDisplayPartTime = new javax.swing.JButton();
            btnDisplayAll = new javax.swing.JButton();
            btnClrAllTxtBoxes = new javax.swing.JButton();
            btnDltAllEmp = new javax.swing.JButton();
            cbEmployeeList = new javax.swing.JComboBox();
            hourlyPartTimePanel = new javax.swing.JPanel();
            hoursWorkedLbl = new javax.swing.JLabel();
            tfHoursWorked = new javax.swing.JTextField();
            salariedPanel = new javax.swing.JPanel();
            salaryLbl = new javax.swing.JLabel();
            tfSalary = new javax.swing.JTextField();
            empIDPanel = new javax.swing.JPanel();
            txtEmployeeID = new javax.swing.JLabel();
            tfEmpID = new javax.swing.JTextField();
            lblEmployeeTracker = new javax.swing.JLabel();
            btnAddHourly = new javax.swing.JButton();
            btnAddPartTime = new javax.swing.JButton();
            btnAddSalaried = new javax.swing.JButton();
     
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
     
            cleardisplayPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Clear and Display"));
     
            btnDisplayHourly.setText("Display Hourly");
            btnDisplayHourly.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btnDisplayHourlyActionPerformed(evt);
                }
            });
     
            btnDisplayPartTime.setText("Display Part Time");
     
            btnDisplayAll.setText("Display All");
     
            btnClrAllTxtBoxes.setText("Clear All Text Boxes");
            btnClrAllTxtBoxes.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btnClrAllTxtBoxesActionPerformed(evt);
                }
            });
     
            btnDltAllEmp.setText("Delete all Employees");
     
            cbEmployeeList.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
            cbEmployeeList.addContainerListener(new java.awt.event.ContainerAdapter() {
                public void componentAdded(java.awt.event.ContainerEvent evt) {
                    cbEmployeeListComponentAdded(evt);
                }
            });
     
            javax.swing.GroupLayout cleardisplayPanelLayout = new javax.swing.GroupLayout(cleardisplayPanel);
            cleardisplayPanel.setLayout(cleardisplayPanelLayout);
            cleardisplayPanelLayout.setHorizontalGroup(
                cleardisplayPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(cleardisplayPanelLayout.createSequentialGroup()
                    .addGap(38, 38, 38)
                    .addGroup(cleardisplayPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(btnDisplayHourly, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btnDisplayPartTime, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btnDisplayAll, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(cleardisplayPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(btnClrAllTxtBoxes, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btnDltAllEmp, javax.swing.GroupLayout.DEFAULT_SIZE, 181, Short.MAX_VALUE)
                        .addComponent(cbEmployeeList, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGap(66, 66, 66))
            );
            cleardisplayPanelLayout.setVerticalGroup(
                cleardisplayPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(cleardisplayPanelLayout.createSequentialGroup()
                    .addGap(34, 34, 34)
                    .addGroup(cleardisplayPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(btnDisplayHourly)
                        .addComponent(cbEmployeeList, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addGroup(cleardisplayPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(btnDisplayPartTime)
                        .addComponent(btnClrAllTxtBoxes))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addGroup(cleardisplayPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(btnDisplayAll)
                        .addComponent(btnDltAllEmp))
                    .addContainerGap(23, Short.MAX_VALUE))
            );
     
            hourlyPartTimePanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Hourly and Part Time"));
     
            hoursWorkedLbl.setText("Hours Worked");
     
            tfHoursWorked.setText("jTextField2");
     
            javax.swing.GroupLayout hourlyPartTimePanelLayout = new javax.swing.GroupLayout(hourlyPartTimePanel);
            hourlyPartTimePanel.setLayout(hourlyPartTimePanelLayout);
            hourlyPartTimePanelLayout.setHorizontalGroup(
                hourlyPartTimePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(hourlyPartTimePanelLayout.createSequentialGroup()
                    .addGap(32, 32, 32)
                    .addComponent(hoursWorkedLbl)
                    .addGap(47, 47, 47)
                    .addComponent(tfHoursWorked, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(59, Short.MAX_VALUE))
            );
            hourlyPartTimePanelLayout.setVerticalGroup(
                hourlyPartTimePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(hourlyPartTimePanelLayout.createSequentialGroup()
                    .addGap(39, 39, 39)
                    .addGroup(hourlyPartTimePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(hoursWorkedLbl)
                        .addComponent(tfHoursWorked, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addContainerGap(45, Short.MAX_VALUE))
            );
     
            salariedPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Salared"));
     
            salaryLbl.setText("Salary");
     
            tfSalary.setText("jTextField3");
     
            javax.swing.GroupLayout salariedPanelLayout = new javax.swing.GroupLayout(salariedPanel);
            salariedPanel.setLayout(salariedPanelLayout);
            salariedPanelLayout.setHorizontalGroup(
                salariedPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(salariedPanelLayout.createSequentialGroup()
                    .addGap(36, 36, 36)
                    .addComponent(salaryLbl)
                    .addGap(60, 60, 60)
                    .addComponent(tfSalary, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(53, Short.MAX_VALUE))
            );
            salariedPanelLayout.setVerticalGroup(
                salariedPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(salariedPanelLayout.createSequentialGroup()
                    .addGap(39, 39, 39)
                    .addGroup(salariedPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(salaryLbl)
                        .addComponent(tfSalary, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addContainerGap(44, Short.MAX_VALUE))
            );
     
            empIDPanel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
     
            txtEmployeeID.setText("Employee ID");
     
            tfEmpID.setText("jTextField1");
            tfEmpID.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    tfEmpIDActionPerformed(evt);
                }
            });
     
            javax.swing.GroupLayout empIDPanelLayout = new javax.swing.GroupLayout(empIDPanel);
            empIDPanel.setLayout(empIDPanelLayout);
            empIDPanelLayout.setHorizontalGroup(
                empIDPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(empIDPanelLayout.createSequentialGroup()
                    .addGap(31, 31, 31)
                    .addComponent(txtEmployeeID, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(42, 42, 42)
                    .addComponent(tfEmpID, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(20, Short.MAX_VALUE))
            );
            empIDPanelLayout.setVerticalGroup(
                empIDPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(empIDPanelLayout.createSequentialGroup()
                    .addGap(21, 21, 21)
                    .addGroup(empIDPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(txtEmployeeID, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(tfEmpID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addContainerGap(21, Short.MAX_VALUE))
            );
     
            lblEmployeeTracker.setFont(new java.awt.Font("Lucida Calligraphy", 1, 14)); // NOI18N
            lblEmployeeTracker.setText("Employee Tracker");
     
            btnAddHourly.setText("Add Hourly");
            btnAddHourly.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btnAddHourlyActionPerformed(evt);
                }
            });
     
            btnAddPartTime.setText("Add Part Time");
            btnAddPartTime.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btnAddPartTimeActionPerformed(evt);
                }
            });
     
            btnAddSalaried.setText("Add Salaried");
            btnAddSalaried.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btnAddSalariedActionPerformed(evt);
                }
            });
     
            javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
            jPanel1.setLayout(jPanel1Layout);
            jPanel1Layout.setHorizontalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addGap(30, 30, 30)
                            .addComponent(cleardisplayPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(jPanel1Layout.createSequentialGroup()
                                    .addGap(20, 20, 20)
                                    .addComponent(hourlyPartTimePanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGap(18, 18, 18)
                                    .addComponent(salariedPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(jPanel1Layout.createSequentialGroup()
                                    .addGap(170, 170, 170)
                                    .addComponent(empIDPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(jPanel1Layout.createSequentialGroup()
                                    .addGap(215, 215, 215)
                                    .addComponent(lblEmployeeTracker)))
                            .addGap(0, 0, Short.MAX_VALUE)))
                    .addContainerGap())
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addGap(52, 52, 52)
                    .addComponent(btnAddHourly)
                    .addGap(18, 18, 18)
                    .addComponent(btnAddPartTime)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnAddSalaried)
                    .addGap(110, 110, 110))
            );
            jPanel1Layout.setVerticalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                    .addContainerGap(32, Short.MAX_VALUE)
                    .addComponent(lblEmployeeTracker)
                    .addGap(40, 40, 40)
                    .addComponent(empIDPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(salariedPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(hourlyPartTimePanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(18, 18, 18)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(btnAddHourly)
                        .addComponent(btnAddPartTime)
                        .addComponent(btnAddSalaried))
                    .addGap(33, 33, 33)
                    .addComponent(cleardisplayPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(30, 30, 30))
            );
     
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            );
     
            pack();
        }// </editor-fold>                        
     
        private void tfEmpIDActionPerformed(java.awt.event.ActionEvent evt) {                                        
     
        }                                       
     
        private void btnAddHourlyActionPerformed(java.awt.event.ActionEvent evt) {                                             
            double hoursWorked = Double.parseDouble(tfHoursWorked.getText());
            String empID = tfEmpID.getText();
            HourlyEmployee hrEmp = new HourlyEmployee(empID, hoursWorked);
            myList.add(hrEmp);
        }                                            
     
        private void btnAddPartTimeActionPerformed(java.awt.event.ActionEvent evt) {                                               
            double hoursWorked = Double.parseDouble(tfHoursWorked.getText());
            String empID = tfEmpID.getText();
            PartTimeEmployee ptEmp = new PartTimeEmployee(empID, hoursWorked);
            myList.add(ptEmp);
        }                                              
     
        private void btnAddSalariedActionPerformed(java.awt.event.ActionEvent evt) {                                               
            double salary = Double.parseDouble(tfSalary.getText());
            String empID = tfEmpID.getText();
            SalariedEmployee salEmp = new SalariedEmployee(empID, salary);
            myList.add(salEmp);
        }                                              
     
        private void btnClrAllTxtBoxesActionPerformed(java.awt.event.ActionEvent evt) {                                                  
            tfEmpID.setText("");
            tfHoursWorked.setText("");
            tfSalary.setText("");
        }                                                 
     
        private void btnDisplayHourlyActionPerformed(java.awt.event.ActionEvent evt) {                                                 
            cbEmployeeList.removeAllItems();
        }                                                
     
        private void cbEmployeeListComponentAdded(java.awt.event.ContainerEvent evt) {                                              
            // TODO add your handling code here:
        }                                             
     
        /**
         * @param args the command line arguments
         */
        public static void main(String args[]) {
            /* Set the Nimbus look and feel */
            //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
            /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
             * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
             */
            try {
                for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                    if ("Nimbus".equals(info.getName())) {
                        javax.swing.UIManager.setLookAndFeel(info.getClassName());
                        break;
                    }
                }
            } catch (ClassNotFoundException ex) {
                java.util.logging.Logger.getLogger(PaydayGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                java.util.logging.Logger.getLogger(PaydayGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                java.util.logging.Logger.getLogger(PaydayGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                java.util.logging.Logger.getLogger(PaydayGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            }
            //</editor-fold>
     
            /* Create and display the form */
            java.awt.EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    new PaydayGUI().setVisible(true);
                }
            });
        }
        // Variables declaration - do not modify                     
        private javax.swing.JButton btnAddHourly;
        private javax.swing.JButton btnAddPartTime;
        private javax.swing.JButton btnAddSalaried;
        private javax.swing.JButton btnClrAllTxtBoxes;
        private javax.swing.JButton btnDisplayAll;
        private javax.swing.JButton btnDisplayHourly;
        private javax.swing.JButton btnDisplayPartTime;
        private javax.swing.JButton btnDltAllEmp;
        private javax.swing.JComboBox cbEmployeeList;
        private javax.swing.JPanel cleardisplayPanel;
        private javax.swing.JPanel empIDPanel;
        private javax.swing.JPanel hourlyPartTimePanel;
        private javax.swing.JLabel hoursWorkedLbl;
        private javax.swing.JPanel jPanel1;
        private javax.swing.JLabel lblEmployeeTracker;
        private javax.swing.JPanel salariedPanel;
        private javax.swing.JLabel salaryLbl;
        private javax.swing.JTextField tfEmpID;
        private javax.swing.JTextField tfHoursWorked;
        private javax.swing.JTextField tfSalary;
        private javax.swing.JLabel txtEmployeeID;
        // End of variables declaration                   
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Trouble adding objects to JComboBox

    Can you make a small, complete program that compiles, executes and shows the problem for testing?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: Trouble adding objects to JComboBox

    You've shown a fairly high level of competence already so i'll give it to you. But i would hope that you take the time to understand what i've done and learn from it.

    private void btnDisplayHourlyActionPerformed(java.awt.event.ActionEvent evt) {                                                 
            cbEmployeeList.removeAllItems();
            for(PayDay he : myList){
            	if(he instanceof HourlyEmployee){
            		cbEmployeeList.addItem(he);
            	}
            }
        }

    In this action, i've added a loop that goes through your whole list of employees. If the employee is of type HourlyEmployee we add it to the combo box. You then need to override the toString method in your employee classes so that it displays in your combo box correctly. Something like the following.

        @Override
        public String toString() {
        	return employeeID + " " + hoursWorked;
        }

    I hope this is what you were looking for. If not we can continue to work through it.
    Writing code is your job, helping you fix and understand it is mine.

    <-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

Similar Threads

  1. Replies: 3
    Last Post: February 26th, 2013, 07:00 PM
  2. Replies: 7
    Last Post: February 2nd, 2013, 11:07 AM
  3. Replies: 17
    Last Post: July 27th, 2012, 12:52 AM
  4. Trouble adding components to a JFrame
    By NcAdams in forum AWT / Java Swing
    Replies: 3
    Last Post: May 22nd, 2012, 10:46 PM
  5. Trouble adding exceptions to fixed queue class
    By Farmer in forum Exceptions
    Replies: 5
    Last Post: December 19th, 2011, 07:23 AM