View Full Version : My program won't repaint

Mar 1, 2007, 01:01 PM
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);

Mar 1, 2007, 01:30 PM
I would suggest you add an ActionListener (http://java.sun.com/j2se/1.5.0/docs/api/java/awt/event/ActionListener.html) 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 (http://java.sun.com/docs/codeconv/) 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.