Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Little Help, Price Reset is not working

  1. #1
    Junior Member
    Join Date
    Nov 2009
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Little Help, Price Reset is not working

    Hi,

    I have wrote my Java code but I notice when I click on ClearAll button, it clears all fields apart from the price, I think there is code missing in my script someone fix it for me please.




    CarSale.Java
    import java.applet.Applet; 
    import java.awt.Button; 
    import java.awt.Color; 
    import java.awt.Label; 
    import java.awt.TextArea; 
    import java.awt.TextField; 
    import java.awt.event.ActionEvent; 
    import java.awt.event.ActionListener; 
     
    public class CarSale extends Applet implements ActionListener { 
     
       private TextArea txaCars, txaSelected; 
       private TextField txfSelItem; 
       private Label lblCarNo; 
       private Label lblTitle; 
       private TextField txffirstname; 
       private Label lblfirstname; 
       private TextField txflastname; 
       private Label lbllastname; 
       private TextField txfaddress; 
       private Label lbladdress; 
       private TextField txfpostcode; 
       private Label lblpostcode; 
       private Label lblemail; 
       private TextField txfemail; 
       private Label lblphone; 
       private TextField txfphone; 
       private Label lblTotal; 
       private Color colTotal = Color.LIGHT_GRAY;
       private float total;
     
       private Button btnGetItem, btnClearItems, btnClearInfo, btnClearAll, btnSubmit; 
     
       private final int MAXCARS = 100;   // maximum size of arrays 
     
       private Car[] cars;         // cars on offer 
       private Car[] pickedCars;      // cars selected 
     
       private int nbrOfCars = 0;      // number of cars on offer 
       private int nbrCarsPicked = 0;   // number of cars picked so far 
       private int nbrToPick = 0;         // number of cars chosen 
     
       public void init(){ 
          setSize(600,510); 
          setLayout(null); 
          setBackground(Color.GREEN); 
     
          createGUI(); 
     
          cars = new Car[MAXCARS]; 
          pickedCars = new Car[MAXCARS]; 
          makeCar();       
          displayCars(); 
       } 
     
       private void displayCars() { 
          txaCars.setText(""); 
          for(int i = 0; i < nbrOfCars; i++){ 
             txaCars.append("" + i+ ": "); 
             txaCars.append(cars[i].toString() + "\n"); 
     
          } 
       } 
     
       private void displaySelectedCars() { 
          txaSelected.setText(""); 
          for(int i = 0; i < nbrCarsPicked; i++){ 
             txaSelected.append(pickedCars[i] + "\n"); 
          } 
       } 
     
       private void createGUI() { 
     
           lblTitle=new Label("Auto Trader Car Sales"); 
           lblTitle.setBounds(10,10,585,20); 
           lblTitle.setBackground(Color.yellow); 
           add(lblTitle); 
     
     
          lblfirstname = new Label("First Name"); 
          lblfirstname.setBounds(10,40,90,20);; 
          add(lblfirstname); 
     
          txffirstname= new TextField(""); 
          txffirstname.setBounds(110,80,150,20); 
          add(txffirstname); 
     
          lbllastname = new Label("Last Name"); 
          lbllastname.setBounds(10,60,100,20); 
          add(lbllastname); 
     
          txflastname= new TextField (""); 
          txflastname.setBounds(110,100,150,20); 
          add(txflastname); 
     
     
          lbladdress = new Label ("Address"); 
          lbladdress.setBounds(10,80,100,20); 
          add (lbladdress); 
     
          txfaddress=new TextField(""); 
          txfaddress.setBounds(110,120,150,20); 
          add(txfaddress); 
     
     
          lblpostcode =new Label("Post Code"); 
          lblpostcode.setBounds(10,100,100,20); 
          add(lblpostcode); 
     
          txfpostcode=new TextField(""); 
          txfpostcode.setBounds(110,60,150,20); 
          add(txfpostcode); 
     
          lblemail=new Label("Email"); 
          lblemail.setBounds(10,120,100,20); 
          add(lblemail); 
     
          txfemail=new TextField(""); 
          txfemail.setBounds(110,40,150,20); 
          add(txfemail); 
     
          lblphone=new Label("Phone"); 
          lblphone.setBounds(10,140,100,20); 
          add(lblphone); 
     
          txfphone=new TextField(""); 
          txfphone.setBounds(110,140,150,20); 
          add(txfphone); 
     
          btnClearInfo = new Button("Clear Information"); 
          btnClearInfo.setBounds(130,165,110,24); 
          btnClearInfo.addActionListener(this); 
          add(btnClearInfo); 
     
           lblTitle=new Label("Cars For Sale"); 
           lblTitle.setBounds(10,200,585,20); 
           lblTitle.setBackground(Color.yellow); 
           add(lblTitle); 
     
           lblTitle=new Label("Shopping Basket"); 
           lblTitle.setBounds(10,345,585,20); 
           lblTitle.setBackground(Color.yellow); 
           add(lblTitle); 
     
          txaCars = new TextArea("", 10,10,TextArea.SCROLLBARS_BOTH); 
          txaCars.setBounds(10,225,585,85); 
          add(txaCars); 
     
          txaSelected = new TextArea("", 10,10,TextArea.SCROLLBARS_BOTH); 
          txaSelected.setBounds(10,370,585,85); 
          add(txaSelected); 
     
          lblCarNo = new Label("Car Number ?"); 
          lblCarNo.setBounds(56,315,80,30); 
          add(lblCarNo); 
     
          txfSelItem = new TextField(""); 
          txfSelItem.setBounds(140,320,40,20); 
          txfSelItem.addActionListener(this); 
          add(txfSelItem); 
     
          btnGetItem = new Button("Get Car"); 
          btnGetItem.setBounds(200,317,110,24); 
          btnGetItem.addActionListener(this); 
          add(btnGetItem); 
     
          btnClearItems = new Button("Clear Basket"); 
          btnClearItems.setBounds(10,460,110,24); 
          btnClearItems.addActionListener(this); 
          add(btnClearItems); 
     
          btnClearAll = new Button("Clear All"); 
          btnClearAll.setBounds(140,460,110,24); 
          btnClearAll.addActionListener(this); 
          add(btnClearAll); 
     
          btnSubmit = new Button("Submit Order"); 
          btnSubmit.setBounds(480,480,110,24); 
          btnSubmit.addActionListener(this); 
          add(btnSubmit); 
     
          lblTotal = new Label("£0.00");
          lblTotal.setBounds(460,457,130,20);
          lblTotal.setBackground(colTotal);
          add(this.lblTotal);
     
     
     
       } 
     
       public void actionPerformed(ActionEvent e) { 
     
          // Get item 
          if(e.getSource() == btnGetItem || e.getSource() == txfSelItem){ 
             try { 
                nbrToPick = Integer.parseInt(txfSelItem.getText()); 
             } 
             catch(Exception ex){ 
                txfSelItem.setText(""); 
                return; 
             } 
             // See if item exists foe item no entered and there is 
             // room in picked array 
             if(nbrToPick >= 0 && nbrToPick < nbrOfCars 
                   && nbrCarsPicked < MAXCARS){ 
                pickedCars[nbrCarsPicked] = cars[nbrToPick]; 
                nbrCarsPicked++; 
                displaySelectedCars(); 
                total = total + cars [nbrToPick].getValue();
                displayTotal();
             } 
             txfSelItem.setText("");             
          }    
          if(e.getSource() == btnClearItems){ 
             txfSelItem.setText("");             
             txaSelected.setText(""); 
             for(int i = 0; i < nbrCarsPicked ; i++) 
                pickedCars[i] = null; 
             nbrCarsPicked = 0;          
             System.out.println("EMPTY SHOPPING BASKET");
          }   
          if(e.getSource() == btnClearInfo){ 
             txffirstname.setText("");             
             txflastname.setText(""); 
             txfaddress.setText(""); 
             txfpostcode.setText(""); 
             txfemail.setText(""); 
             txfphone.setText("");      
             System.out.println("CLEAR CUSTOMER INFORMATION");
          } 
     
       if(e.getSource() == btnClearAll){ 
          txfSelItem.setText("");             
          txaSelected.setText(""); 
          txffirstname.setText("");             
          txflastname.setText(""); 
          txfaddress.setText(""); 
          txfpostcode.setText(""); 
          txfemail.setText(""); 
          txfphone.setText("");      
          System.out.println("RESET ALL");
       } 
       if(e.getSource() == btnSubmit){    
    	      txfSelItem.setText("");             
    	      txaSelected.setText(""); 
    	      txffirstname.setText("");             
    	      txflastname.setText(""); 
    	      txfaddress.setText(""); 
    	      txfpostcode.setText(""); 
    	      txfemail.setText(""); 
    	      txfphone.setText("");
           System.out.println("ORDER HAS BEEN SUBMITTED + RESET");
        } 
       }
       private void displayTotal() { 
    	   this.lblTotal.setText("£" + this.total);
       } 
       private void makeCar() { 
          cars[nbrOfCars] = new Car("Ferrari F430", 120000.00f); 
          nbrOfCars++; 
          cars[nbrOfCars] = new Car("BMW M3", 15000.00f); 
          nbrOfCars++; 
          cars[nbrOfCars] = new Car("Benz Classic", 250000.00f); 
          nbrOfCars++; 
          cars[nbrOfCars] = new Car("Skyline GT", 30000.00f); 
          nbrOfCars++; 
       } 
    }

    Car.java
    public class Car {
     
    	String name;
    	float value;
     
    	/**
    	 * @param name the name of the Car
    	 * @param value	the value of the Car
    	 */
    	public Car(String name, float value) {
    		super();
    		// TODO Auto-generated constructor stub
    		this.name = name;
    		this.value = value;
    	}
     
    	/**
    	 * @return Returns the name.
    	 */
    	public String getName() {
    		return name;
    	}
     
    	/**
    	 * @param name The name to set.
    	 */
    	public void setName(String name) {
    		this.name = name;
    	}
     
    	/**
    	 * @return Returns the value.
    	 */
    	public float getValue() {
    		return value;
    	}
     
    	/**
    	 * @param value The value to set.
    	 */
    	public void setValue(float value) {
    		this.value = value;
    	}
     
    	/**
    	 * Car display string
    	 */
    	public String toString() {
    		return name + "  (" + value + ")";
    	}
    }

    regards


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Little Help, Price Reset is not working

    Hello drkossa.

    The problem is that you are missing lblTotal.setText(""); from your clear methods. Try:

    import java.applet.Applet; 
    import java.awt.Button; 
    import java.awt.Color; 
    import java.awt.Label; 
    import java.awt.TextArea; 
    import java.awt.TextField; 
    import java.awt.event.ActionEvent; 
    import java.awt.event.ActionListener; 
     
    public class CarSale extends Applet implements ActionListener { 
     
       private TextArea txaCars, txaSelected; 
       private TextField txfSelItem; 
       private Label lblCarNo; 
       private Label lblTitle; 
       private TextField txffirstname; 
       private Label lblfirstname; 
       private TextField txflastname; 
       private Label lbllastname; 
       private TextField txfaddress; 
       private Label lbladdress; 
       private TextField txfpostcode; 
       private Label lblpostcode; 
       private Label lblemail; 
       private TextField txfemail; 
       private Label lblphone; 
       private TextField txfphone; 
       private Label lblTotal; 
       private Color colTotal = Color.LIGHT_GRAY;
       private float total;
     
       private Button btnGetItem, btnClearItems, btnClearInfo, btnClearAll, btnSubmit; 
     
       private final int MAXCARS = 100;   // maximum size of arrays 
     
       private Car[] cars;         // cars on offer 
       private Car[] pickedCars;      // cars selected 
     
       private int nbrOfCars = 0;      // number of cars on offer 
       private int nbrCarsPicked = 0;   // number of cars picked so far 
       private int nbrToPick = 0;         // number of cars chosen 
     
       public void init(){ 
          setSize(600,510); 
          setLayout(null); 
          setBackground(Color.GREEN); 
     
          createGUI(); 
     
          cars = new Car[MAXCARS]; 
          pickedCars = new Car[MAXCARS]; 
          makeCar();       
          displayCars(); 
       } 
     
       private void displayCars() { 
          txaCars.setText(""); 
          for(int i = 0; i < nbrOfCars; i++){ 
             txaCars.append("" + i+ ": "); 
             txaCars.append(cars[i].toString() + "\n"); 
     
          } 
       } 
     
       private void displaySelectedCars() { 
          txaSelected.setText(""); 
          for(int i = 0; i < nbrCarsPicked; i++){ 
             txaSelected.append(pickedCars[i] + "\n"); 
          } 
       } 
     
       private void createGUI() { 
     
           lblTitle=new Label("Auto Trader Car Sales"); 
           lblTitle.setBounds(10,10,585,20); 
           lblTitle.setBackground(Color.yellow); 
           add(lblTitle); 
     
     
          lblfirstname = new Label("First Name"); 
          lblfirstname.setBounds(10,40,90,20);; 
          add(lblfirstname); 
     
          txffirstname= new TextField(""); 
          txffirstname.setBounds(110,80,150,20); 
          add(txffirstname); 
     
          lbllastname = new Label("Last Name"); 
          lbllastname.setBounds(10,60,100,20); 
          add(lbllastname); 
     
          txflastname= new TextField (""); 
          txflastname.setBounds(110,100,150,20); 
          add(txflastname); 
     
     
          lbladdress = new Label ("Address"); 
          lbladdress.setBounds(10,80,100,20); 
          add (lbladdress); 
     
          txfaddress=new TextField(""); 
          txfaddress.setBounds(110,120,150,20); 
          add(txfaddress); 
     
     
          lblpostcode =new Label("Post Code"); 
          lblpostcode.setBounds(10,100,100,20); 
          add(lblpostcode); 
     
          txfpostcode=new TextField(""); 
          txfpostcode.setBounds(110,60,150,20); 
          add(txfpostcode); 
     
          lblemail=new Label("Email"); 
          lblemail.setBounds(10,120,100,20); 
          add(lblemail); 
     
          txfemail=new TextField(""); 
          txfemail.setBounds(110,40,150,20); 
          add(txfemail); 
     
          lblphone=new Label("Phone"); 
          lblphone.setBounds(10,140,100,20); 
          add(lblphone); 
     
          txfphone=new TextField(""); 
          txfphone.setBounds(110,140,150,20); 
          add(txfphone); 
     
          btnClearInfo = new Button("Clear Information"); 
          btnClearInfo.setBounds(130,165,110,24); 
          btnClearInfo.addActionListener(this); 
          add(btnClearInfo); 
     
           lblTitle=new Label("Cars For Sale"); 
           lblTitle.setBounds(10,200,585,20); 
           lblTitle.setBackground(Color.yellow); 
           add(lblTitle); 
     
           lblTitle=new Label("Shopping Basket"); 
           lblTitle.setBounds(10,345,585,20); 
           lblTitle.setBackground(Color.yellow); 
           add(lblTitle); 
     
          txaCars = new TextArea("", 10,10,TextArea.SCROLLBARS_BOTH); 
          txaCars.setBounds(10,225,585,85); 
          add(txaCars); 
     
          txaSelected = new TextArea("", 10,10,TextArea.SCROLLBARS_BOTH); 
          txaSelected.setBounds(10,370,585,85); 
          add(txaSelected); 
     
          lblCarNo = new Label("Car Number ?"); 
          lblCarNo.setBounds(56,315,80,30); 
          add(lblCarNo); 
     
          txfSelItem = new TextField(""); 
          txfSelItem.setBounds(140,320,40,20); 
          txfSelItem.addActionListener(this); 
          add(txfSelItem); 
     
          btnGetItem = new Button("Get Car"); 
          btnGetItem.setBounds(200,317,110,24); 
          btnGetItem.addActionListener(this); 
          add(btnGetItem); 
     
          btnClearItems = new Button("Clear Basket"); 
          btnClearItems.setBounds(10,460,110,24); 
          btnClearItems.addActionListener(this); 
          add(btnClearItems); 
     
          btnClearAll = new Button("Clear All"); 
          btnClearAll.setBounds(140,460,110,24); 
          btnClearAll.addActionListener(this); 
          add(btnClearAll); 
     
          btnSubmit = new Button("Submit Order"); 
          btnSubmit.setBounds(480,480,110,24); 
          btnSubmit.addActionListener(this); 
          add(btnSubmit); 
     
          lblTotal = new Label("£0.00");
          lblTotal.setBounds(460,457,130,20);
          lblTotal.setBackground(colTotal);
          add(this.lblTotal);
     
     
     
       } 
     
       public void actionPerformed(ActionEvent e) { 
     
          // Get item 
          if(e.getSource() == btnGetItem || e.getSource() == txfSelItem){ 
             try { 
                nbrToPick = Integer.parseInt(txfSelItem.getText()); 
             } 
             catch(Exception ex){ 
                txfSelItem.setText(""); 
                return; 
             } 
             // See if item exists foe item no entered and there is 
             // room in picked array 
             if(nbrToPick >= 0 && nbrToPick < nbrOfCars 
                   && nbrCarsPicked < MAXCARS){ 
                pickedCars[nbrCarsPicked] = cars[nbrToPick]; 
                nbrCarsPicked++; 
                displaySelectedCars(); 
                total = total + cars [nbrToPick].getValue();
                displayTotal();
             } 
             txfSelItem.setText("");             
          }    
          if(e.getSource() == btnClearItems){ 
             txfSelItem.setText("");             
             txaSelected.setText(""); 
             for(int i = 0; i < nbrCarsPicked ; i++) 
                pickedCars[i] = null; 
             nbrCarsPicked = 0;          
             System.out.println("EMPTY SHOPPING BASKET");
            [B] lblTotal.setText("");[/B]
          }   
          if(e.getSource() == btnClearInfo){ 
             txffirstname.setText("");             
             txflastname.setText(""); 
             txfaddress.setText(""); 
             txfpostcode.setText(""); 
             txfemail.setText(""); 
             txfphone.setText("");      
             System.out.println("CLEAR CUSTOMER INFORMATION");
          } 
     
       if(e.getSource() == btnClearAll){ 
          txfSelItem.setText("");             
          txaSelected.setText(""); 
          txffirstname.setText("");             
          txflastname.setText(""); 
          txfaddress.setText(""); 
          txfpostcode.setText(""); 
          txfemail.setText(""); 
          txfphone.setText("");
         [B] lblTotal.setText("");[/B]
          System.out.println("RESET ALL");
       } 
       if(e.getSource() == btnSubmit){    
              txfSelItem.setText("");             
              txaSelected.setText(""); 
              txffirstname.setText("");             
              txflastname.setText(""); 
              txfaddress.setText(""); 
              txfpostcode.setText(""); 
              txfemail.setText(""); 
              txfphone.setText("");
           System.out.println("ORDER HAS BEEN SUBMITTED + RESET");
        } 
       }
       private void displayTotal() { 
           this.lblTotal.setText("£" + this.total);
       } 
       private void makeCar() { 
          cars[nbrOfCars] = new Car("Ferrari F430", 120000.00f); 
          nbrOfCars++; 
          cars[nbrOfCars] = new Car("BMW M3", 15000.00f); 
          nbrOfCars++; 
          cars[nbrOfCars] = new Car("Benz Classic", 250000.00f); 
          nbrOfCars++; 
          cars[nbrOfCars] = new Car("Skyline GT", 30000.00f); 
          nbrOfCars++; 
       } 
    }
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

Similar Threads

  1. Problem with reset on JComboBox
    By WorkingMan in forum AWT / Java Swing
    Replies: 4
    Last Post: April 25th, 2013, 12:19 PM
  2. Working with Methods
    By duckman in forum Object Oriented Programming
    Replies: 3
    Last Post: November 9th, 2009, 08:27 PM
  3. Reset the value of each variable
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 22nd, 2009, 09:45 AM
  4. Replies: 24
    Last Post: April 14th, 2009, 03:43 PM
  5. Replies: 4
    Last Post: January 27th, 2009, 12:03 AM