Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Sideonecincy

macrumors 6502
Original poster
Sep 29, 2003
421
0
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);
totalField.repaint();
 

jeremy.king

macrumors 603
Jul 23, 2002
5,479
1
Holly Springs, 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
Code:
  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

Code:
private final UpdatePriceListener priceListener = new UpdatePriceListener();
comboBox.addActionListener(priceListener);
SunroofCheckBox.addActionListener(priceListener);
//and so on

Good Luck.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.