0

How come each time I click the submit button on my code it keeps on giving me the hourly string value? even if the item in my JComboBox has the current value of weekly? how would I fix this?

The code for my ComputeWindow

public class ComputeWindow extends JFrame{

        private JLabel payRate,employeeID,payType,gWage;
        private JTextField  empRate,idTxt,gWageTxt;
        private JComboBox rateChooser;
        private JPanel panel;
        private JButton generate;

        public ComputeWindow(){

            super("Gross Wage");
            setSize(300, 150);
            setLocationRelativeTo(null);
            setVisible(true);
            setDefaultCloseOperation(EXIT_ON_CLOSE);


            String[] employeeType = {"Hourly","Bi Weekly","Weekly","Base"};

            employeeID = new JLabel("Employee ID: ");
            payRate  = new JLabel("Employee Rate: ");
            payType = new JLabel("Pay Type: ");
            gWage = new JLabel("Gross Wage");
            empRate = new JTextField(10);
            idTxt = new JTextField(10);
            gWageTxt = new JTextField(10);
            generate = new JButton("Compute!");
            rateChooser = new JComboBox(employeeType);
            panel = new JPanel(new GridLayout(5,1));

            panel.add(employeeID);
            panel.add(idTxt);
            panel.add(payRate);
            panel.add(empRate);
            panel.add(payType);
            panel.add(rateChooser);
            panel.add(gWage);
            panel.add(gWageTxt);
            panel.add(generate);
            add(panel, BorderLayout.CENTER);
            String Option =(String)rateChooser.getSelectedItem();

            GenerateHandler generateHandler = new GenerateHandler();
            generate.addActionListener(generateHandler);
            generateHandler.setOption(Option);
        }   
    }

and for the handler here it is

private class GenerateHandler implements ActionListener{

        String option;

        public void setOption(String option){
            this.option = option;
        }

        public void actionPerformed(ActionEvent e) {

            if(option =="Hourly"){
                JOptionPane.showMessageDialog(null,option);
            }else if(option == "Weekly"){
                JOptionPane.showMessageDialog(null,option);
            }
        }

    }

}

any suggestions? on how would I fix this? or it will return or show me the current value that is being used in my JComboBox?

user962206
  • 15,637
  • 61
  • 177
  • 270

1 Answers1

2

You can change your code as follows:

GenerateHandler generateHandler = new GenerateHandler(rateChooser);

and

private class GenerateHandler implements ActionListener{
    JComboBox rateChooser;

    public GenerateHandler(JComboBox rateChooser){
        this.rateChooser = rateChooser;
    }

    public void actionPerformed(ActionEvent e) {
        String Option =(String)rateChooser.getSelectedItem();
        if("Hourly".equals(option)){
            JOptionPane.showMessageDialog(null,option);
        }else if("Weekly".equals(option)){
            JOptionPane.showMessageDialog(null,option);
        }
    }
}

Above code will solve your solution, but this is not a good practice to extend JFrame, specially JSomething ... and initialize everything in a constructor the way it is done.

Kowser
  • 8,123
  • 7
  • 40
  • 63