My program won't repaint

Discussion in 'Mac Programming' started by Sideonecincy, Mar 1, 2007.

  1. macrumors 6502

    Sep 29, 2003
    I have to display the amount that the car would cost. I can get it to display when the program loads, but it will not repaint over itself each time a different option is changed. I know it has to do with my totalfield or whether i need to use a textbox.

    public void setSampleFont()
    // Chose car name
    String facename
    = (String) carCombo.getSelectedItem();

    // Select car options
    String selected = (String)carCombo.getSelectedItem();
    if (selected.equalsIgnoreCase("Corvette"))
    baseprice = Corvette;
    else if(selected.equalsIgnoreCase("Cobalt"))
    baseprice = Cobalt;
    else if(selected.equalsIgnoreCase("Silverado"))
    baseprice = Silverado;

    if (SunroofCheckBox.isSelected())
    baseprice = baseprice + Sunroofupgrade;
    if (PlayerCheckBox.isSelected())
    baseprice = baseprice + Radioupgrade;
    if (RimsCheckBox.isSelected())
    baseprice = baseprice + Rimsupgrade;
    if (LeatherCheckBox.isSelected())
    baseprice = baseprice + Leatherupgrade;

    //chose transmission type

    if (autoButton.isSelected())
    baseprice = baseprice + automatic;
    else if (manualButton.isSelected())
    baseprice = baseprice - manual;

    totalField = new JLabel();

    totalField.setText("Current MSRP: $" + baseprice);
  2. macrumors 603


    Jul 23, 2002
    Fuquay Varina, NC
    I would suggest you add an ActionListener to all of the components that factor into the price. Then when they are changed, the listener will update the price.

    Also, change your method name to reflect what it does, such as updatePrice() and please try to use the Java code convention for naming.

    A sample listener, add to your class
      private class UpdatePriceListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
          updatePrice(); //assumes you rename that poorly named method     
    use one instance on all of your components involved in the calculation

    private final UpdatePriceListener priceListener = new UpdatePriceListener();
    //and so on
    Good Luck.

Share This Page