1

I have values set for each of the car buying options but when I click the button I keep getting 0. I can't get fP to update after I declare it. Here is my code:

public class CarOptions extends JFrame {

    //All the Buttons needed
    private JComboBox colorOptions;
    private JRadioButton leatherInt;
    private JRadioButton touchScreen;
    private JRadioButton premiumSound;
    private JRadioButton bodyKit;

    //Car prices
    int cP = 1000;
    int eng1 = 6000;
    int eng2 = 8000;
    int eng3 = 12000;
    int acc1 = 600;
    int acc2 = 800;
    int acc3 = 3000;
    int acc4 = 1200;
    int fP;

    public CarOptions(){                
        createControlPanel();
    }
    public void createControlPanel(){
    //Panel for all the options
        JPanel colorOptions1 = createComboBox();
        JPanel sportsOptions = createCheckBoxes();
        JPanel accOptions = createRadioButtons();
        JPanel finalPrice1 = createButton();

        //Line up the panels
        JPanel controlPanel = new JPanel();
        controlPanel.setLayout(new GridLayout(4,1));
        controlPanel.add(colorOptions1);
        controlPanel.add(sportsOptions);
        controlPanel.add(accOptions);
        controlPanel.add(finalPrice1);

        add(controlPanel, BorderLayout.SOUTH);
    }
    public JPanel createComboBox(){

        colorOptions = new JComboBox();
        colorOptions.addItem("Black");
        colorOptions.addItem("Yellow");
        colorOptions.addItem("Blue");
        colorOptions.addItem("Red");

        JPanel panel = new JPanel();
        panel.add(colorOptions);
        return panel;
    }
    public JPanel createCheckBoxes(){
        ButtonGroup group = new ButtonGroup();
        AbstractButton hybrid = new JCheckBox("Hybrid");
        AbstractButton base = new JCheckBox("Base Model");
        AbstractButton sport = new JCheckBox("Sport");
        group.add(hybrid);
        group.add(base);
        group.add(sport);

        if (hybrid.isSelected()){
            fP = fP + eng1;
        }
        else if (base.isSelected()){
            fP = fP + eng2;
        }
        else if (sport.isSelected()){
            fP = fP + eng3;
        }

        JPanel panel = new JPanel();
        panel.add(hybrid);
        panel.add(base);
        panel.add(sport);
        panel.setBorder(new TitledBorder(new EtchedBorder(), "Engine Package"));
        return panel;
    }
    public JPanel createRadioButtons(){
        leatherInt = new JRadioButton("Leather Interior");
        touchScreen = new JRadioButton("Touchscreen Radio");
        premiumSound = new JRadioButton("Premium Sound System");
        bodyKit = new JRadioButton("Body Kit");

        if (leatherInt.isSelected()){
            fP = cP + acc1;
        }
        else if (touchScreen.isSelected()){
            fP = cP + acc2;
        }
        else if  (premiumSound.isSelected()){
            fP = cP + acc3;
        }        
        else if (bodyKit.isSelected()){
            fP = cP + acc4;
        }

        JPanel panel = new JPanel();
        panel.add(leatherInt);
        panel.add(touchScreen);
        panel.add(premiumSound);
        panel.add(bodyKit);
        panel.setBorder(new TitledBorder(new EtchedBorder(), "Accesories"));
        return panel;
    }
    public JPanel createButton(){
       JButton finalPrice = new JButton("Final Price");
       finalPrice.addActionListener(new ActionListener(){
           public void actionPerformed(ActionEvent e){

               JFrame fPFrame = new JFrame();
               fPFrame.setSize(200,100);
               fPFrame.setTitle("Final Price");
               fPFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
               fPFrame.setVisible(true);

               JLabel fPLabel = new JLabel("Your final price is: $" + fP);
               JPanel fPPanel = new JPanel();
               fPPanel.add(fPLabel);
               fPFrame.add(fPPanel);
           }
       });

       JPanel panel = new JPanel();
       panel.add(finalPrice);
       return panel;
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
mcguirjk
  • 41
  • 3
  • 1
    Please use code formatting for code and code snippets, structured documents like HTML/XML or input/output. To do that, select the text and click the `{}` button at the top of the message posting/editing form. – Andrew Thompson Feb 16 '15 at 15:45

2 Answers2

1

You get 0, because to don't calculate the price "when the button is clicked".

You are trying to calculate the price when you create the buttons. The problem is the user hasn't done anything yet. The price can only be calculated in response to an event.

So all your price calculation code needs to be moved to the ActionListener

camickr
  • 321,443
  • 19
  • 166
  • 288
1

You did the calculation only once, based on empty controls Here's a fix:

//All the Buttons needed
private JComboBox<String> colorOptions;
private JRadioButton leatherInt;
private JRadioButton touchScreen;
private JRadioButton premiumSound;
private JRadioButton bodyKit;

//Car prices
int cP = 1000;
int eng1 = 6000;
int eng2 = 8000;
int eng3 = 12000;
int acc1 = 600;
int acc2 = 800;
int acc3 = 3000;
int acc4 = 1200;
int fP;

public CarOptions(){                
    createControlPanel();
}
public void createControlPanel(){
//Panel for all the options
    JPanel colorOptions1 = createComboBox();
    JPanel sportsOptions = createCheckBoxes();
    JPanel accOptions = createRadioButtons();
    JPanel finalPrice1 = createButton();

    //Line up the panels
    JPanel controlPanel = new JPanel();
    controlPanel.setLayout(new GridLayout(4,1));
    controlPanel.add(colorOptions1);
    controlPanel.add(sportsOptions);
    controlPanel.add(accOptions);
    controlPanel.add(finalPrice1);

    add(controlPanel, BorderLayout.SOUTH);
}
public JPanel createComboBox(){

    colorOptions = new JComboBox<>();
    colorOptions.addItem("Black");
    colorOptions.addItem("Yellow");
    colorOptions.addItem("Blue");
    colorOptions.addItem("Red");

    JPanel panel = new JPanel();
    panel.add(colorOptions);
    return panel;
}
public JPanel createCheckBoxes(){
    ButtonGroup group = new ButtonGroup();
    AbstractButton hybrid = new JCheckBox("Hybrid");
    AbstractButton base = new JCheckBox("Base Model");
    AbstractButton sport = new JCheckBox("Sport");
    group.add(hybrid);
    group.add(base);
    group.add(sport);

    if (hybrid.isSelected()){
        fP = fP + eng1;
    }
    else if (base.isSelected()){
        fP = fP + eng2;
    }
    else if (sport.isSelected()){
        fP = fP + eng3;
    }

    JPanel panel = new JPanel();
    panel.add(hybrid);
    panel.add(base);
    panel.add(sport);
    panel.setBorder(new TitledBorder(new EtchedBorder(), "Engine Package"));
    return panel;
}
public JPanel createRadioButtons(){
    leatherInt = new JRadioButton("Leather Interior");
    touchScreen = new JRadioButton("Touchscreen Radio");
    premiumSound = new JRadioButton("Premium Sound System");
    bodyKit = new JRadioButton("Body Kit");

    recalculate();
    JPanel panel = new JPanel();
    panel.add(leatherInt);
    panel.add(touchScreen);
    panel.add(premiumSound);
    panel.add(bodyKit);
    panel.setBorder(new TitledBorder(new EtchedBorder(), "Accesories"));
    return panel;
}
public JPanel createButton(){
   JButton finalPrice = new JButton("Final Price");
   finalPrice.addActionListener(new ActionListener(){
       public void actionPerformed(ActionEvent e){
           recalculate();
           JFrame fPFrame = new JFrame();
           fPFrame.setSize(200,100);
           fPFrame.setTitle("Final Price");
           fPFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
           fPFrame.setVisible(true);

           JLabel fPLabel = new JLabel("Your final price is: $" + fP);
           JPanel fPPanel = new JPanel();
           fPPanel.add(fPLabel);
           fPFrame.add(fPPanel);
       }
   });

   JPanel panel = new JPanel();
   panel.add(finalPrice);
   return panel;
}

private void recalculate() {
       if (leatherInt.isSelected()){
            fP = cP + acc1;
        }
        else if (touchScreen.isSelected()){
            fP = cP + acc2;
        }
        else if  (premiumSound.isSelected()){
            fP = cP + acc3;
        }        
        else if (bodyKit.isSelected()){
            fP = cP + acc4;
        }
}
Werner Keil
  • 592
  • 5
  • 12