Results 1 to 7 of 7
  1. #1
    wfsteadman is offline Member
    Join Date
    Jan 2013
    Location
    Texas
    Posts
    45
    Rep Power
    0

    Default Using static variable from subclass

    Good day all,
    So I am building a GUI application that adds, cars, trucks, and buses into an array.

    I have it working fine. Now on the GUI, I want it to show me the current count of each type of vehicle.
    Car: #
    Truck: #
    Bus: #

    Where # is the number of that type of vehicle in the Array

    I have a superclass called vehicle
    I have three subclasses called Car, Truck, Bus

    from the main dealershipGUI I have the following code: (Note I am only showing for the Car, but I have this setup for Truck and Bus also)

    vehArray is the array that stores all vehicles. This works great and I can go through the records and see them with a forward and backward button.

    I am not sure why I can't get a label on the GUI to get the details of carCount that is public static int carCount=0;

    I even have Getters and Setters as shown below, but when I try to set this up it does not increment when I add a new car:

    Here is what I have in the JPanel:
    numCarCt = new JLabel(Integer.toString(Car.getCarCount()));

    What I want to happen is when I click the add button, the label on the main GUI page increments based on the value in carCount from the Car class, if that makes sense.


    I create the Car values by using the following:

    FROM dealershipGUI
    Java Code:
    if (vehType.getText().equals("Car"))
          {
            if (txtModel.getText().equals(""))
              txtModel.setText("Camaro");
            Car tempVeh = new Car(viewNumPassengers.getText(), viewNumDoors.getText(), vehType.getText(), txtMake.getText(), txtModel.getText(), txtYear.getText(), txtColor.getText(), txtPrice.getText());
            vehArray[currVeh] = tempVeh;
    My Car Class is made up of the following:

    Java Code:
    public class Car extends Vehicle
    {
      
      private String numPassengers;
      private String numDoors;
      public static int carCount=0;
      
      // Vehicle tempVeh = new Car(carNumPass, doors, make, model, year, color, price);
      public Car(String numPassengers, String numDoors, String vType, String vMake, String vModel, String vYear, String vColor, String vPrice)
      {
        super(vType, vMake, vModel, vYear, vColor, vPrice);
        this.numPassengers = numPassengers;
        this.numDoors = numDoors;
        carCount++;
      }
    
      public String getNumPassengers()
      {
        return numPassengers;
      }
    
      public void setNumPassengers(String numPassengers)
      {
        this.numPassengers = numPassengers;
      }
    
      public String getNumDoors()
      {
        return numDoors;
      }
    
      public void setNumDoors(String numDoors)
      {
        this.numDoors = numDoors;
      }
    
      public static int getCarCount()
      {
        return carCount;
      }
    
      public void setCarCount(int carCount)
      {
        this.carCount = carCount;
      }
      
      @Override
       public String displayVehicle()
        {
            return vType + "Make: "+ vMake + "  Model: "+ vModel +"  Year: "+ vYear + " Color: " + vColor + " Price: " + vPrice + 
                    " # Doors: " + getNumDoors() + " # Passengers; " + getNumPassengers();
        }

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,768
    Rep Power
    5

    Default Re: Using static variable from subclass

    What you are trying to do won't work because the JLabel is initialized to a String, not a dynamic method call. So what you need to do is simply update the JLabel text in the actionListener for your addButton.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    wfsteadman is offline Member
    Join Date
    Jan 2013
    Location
    Texas
    Posts
    45
    Rep Power
    0

    Default Re: Using static variable from subclass

    Jim,
    Is there a better way to do this? I am not sure I understand what you mean when you say the JLabel is not a dynamic method. I attached the file here so you could see the whole thing. Having issues showing the values and also removing an array element.
    Attached Files Attached Files

  4. #4
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,768
    Rep Power
    5

    Default Re: Using static variable from subclass

    The term dynamic method call was simply my way of describing what I thought you were trying to do. Probably a poor description. What I am saying is that the value you iniitially assigned to the JLabel is simply the return value of the Car.getCarCount()). So subequent updates to the car count won't automatically affect the label.

    In your actionlistener that responds to the button click, you need to do something like this (I'm using my own variables in the example).

    carCount++;
    label.setText(Integer.toString(carCount));

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    wfsteadman is offline Member
    Join Date
    Jan 2013
    Location
    Texas
    Posts
    45
    Rep Power
    0

    Default Re: Using static variable from subclass

    Thanks Jim, that worked Well. I appreciate the assist. Any tips on how to remove array elements in my code?

  6. #6
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,768
    Rep Power
    5

    Default Re: Using static variable from subclass

    I would highly recommend staying away from arrays and using the various implementations of the List interface. ArrayList is one of the more basic. It has remove capabilities and other useful features.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: Using static variable from subclass

    Using a List is the way to go. However if your requirements are to use an array then you cannot actually remove items from an array you can only replace them.

    You can either copy all elements above the one you want to remove, down one index but that leaves the last element in the array twice. eg to delete 7:
    1, 2, 5, 7, 8, 9
    1, 2, 5, 8, 9, 9

    Or you can create a new array and copy all items from old array to new array except the one you want to remove. You can either code it yourself or use System.arraycopy().

Similar Threads

  1. Variable static in subclass
    By alexanderschill in forum New To Java
    Replies: 4
    Last Post: 01-01-2013, 04:06 PM
  2. Replies: 7
    Last Post: 12-07-2012, 01:00 PM
  3. Replies: 5
    Last Post: 12-01-2010, 06:43 AM
  4. Replies: 3
    Last Post: 02-11-2010, 09:59 AM
  5. Replies: 1
    Last Post: 08-01-2007, 09:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •