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

    Default Stuck with GUI array

    Greetings all,
    I have been working on a GUI project to buy and sell vehicles

    I currently have it setup to allow me to add vehicles and also move through the Array and view the vehicles.

    Issue 1: I am having problems with removing a vehicle as I am not sure how to keep track of where I am at in the array as I move backwards and forward through the GUI
    Issue 2: I also have a Static Variable in each of the subclasses (car, truck, bus) that I am trying to get to show up on the GUI and I cannot figure out how to access those values dynamically.

    I have attached a copy of my Project in zipped format and can answer any questions as necessary.

    I have values countVeh that keep track of how many vehicles are in the Array and currVeh but when I try to do a remove, i am not sure what I need to base the value on as I don't know where I am at in the Array, if that makes any sense.

    I am not sure that I should have for (int j = currVeh; j < countVeh; j++) where j = currVeh as it doesn't seem to do anything.

    I am just looking for direction as to where to go as I am not sure where I am going wrong. Thanks so much in advance.

    Wally

    my Code for removing is:
    Java Code:
    ButtonRemove removeHandler = new ButtonRemove();
        vehRemove.addActionListener(removeHandler);
    
    
    private class ButtonRemove implements ActionListener
      {
    
        public void actionPerformed(ActionEvent event)
        {
          for (int j = currVeh; j < countVeh; j++)
          {
            for (int k = j; k < countVeh; k++)       // move higher ones down
              vehArray[k] = vehArray[k + 1];
            countVeh--;
          }
        }
      }
    Attached Files Attached Files

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

    Default Re: Stuck with GUI array

    I was able to work out the Static Count, but still having problems being able to remove a vehicle from the array. Any help would be appreciated.
    Thanks alot
    Wally

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,036
    Rep Power
    6

    Default Re: Stuck with GUI array

    Java Code:
    //If you want to "delete" an element, say element n, then do this;
    for (int j = n+1; j < array.length; j++) {
               array[j-1] = array[j];
    }
    array = Arrays.copyOf(array, array.length-1);
    or better still using lists.

    Java Code:
    List<Integer> list = new ArrayList<Integer>();
    //.. fill list
    
    list.remove(n);
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

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

    Default Re: Stuck with GUI array

    Yeah I have to use an Array for this project. So since I am using a GUI, and it has a forward and a backward button and the array can hold 100 elements. If I fill it and then use the forward and back buttons and say end up at element number 37, I know how to move everything from 38 up down to replace it, but for some reason I am having issues knowing where I am at in the array. I have a variable called countVeh that is the size of the array and I have another variable called currVeh which should be where I am at in the array. So when I open the GUI, if I go forward I was adding 1 to currVeh and if I went backward I was subtracting 1 from currVeh and then tried to do:

    Java Code:
    private class ButtonRemove implements ActionListener
      {
    
        public void actionPerformed(ActionEvent event)
        {
          for (int j = currVeh; j < countVeh; j++)
          {
            for (int k = j; k < countVeh; k++)       // move higher ones down
              vehArray[k] = vehArray[k + 1];
            countVeh--;
          }
        }
      }
    But it did not seem to work, the elements never went away.

    It might have something to do with how I have the GUI configured, but I am not sure.... I have read so much on removing from the Array and still no luck...

    Thanks for all the help out there folks. I am very close to completion and have learned so much.

    Wally

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,036
    Rep Power
    6

    Default Re: Stuck with GUI array

    Well, I am not certain why you need nested loops. Assuming I understand what you are trying to do you could try the following:


    1. Determine in the action listener if you are adding or removing elements(perhaps depending on which button was clicked).
    2. Have one loop for adding and another loop for removing.
    3. If you recopy the array back to itself using Arrays.copyOf with one less than the original length, then the array length can serve as the vehicle count. To insert an element, copy the array with one greater than the original length before inserting.
    4. And just in case it matters, when you insert or remove an element, then the relative locations of some of the vehicles changes. So if you remove vehicle #43, then all vehicle indices above 43 are one less than they were before. This would probably only be an issue if the vehicle position in the array had to match a field in a vehicle object. In that case its just a matter of updating the field as the array is manipulated.


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

Similar Threads

  1. Stuck on 2D array issue
    By scottdaking in forum New To Java
    Replies: 2
    Last Post: 02-25-2013, 08:11 PM
  2. Re: I'm new and stuck, please help :(
    By leader111 in forum New To Java
    Replies: 2
    Last Post: 09-25-2012, 11:38 PM
  3. 2D array help(stuck)
    By Dex in forum New To Java
    Replies: 9
    Last Post: 08-07-2012, 03:51 PM
  4. Stuck with a little aspect of the array list
    By kingkongjaffa in forum New To Java
    Replies: 2
    Last Post: 09-18-2011, 05:07 PM
  5. I'm stuck help!!!
    By nobody58 in forum Advanced Java
    Replies: 2
    Last Post: 03-18-2010, 03:52 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
  •