With a little help I've finally managed to add an arrayList to my swing program. The problem is now with iteration through this list. When I try to go through the list, it simply jumps straight to the beginning or end of the list, depending on whether I click the backwards or forwards button.
The arrayList is printing out correctly, so I'm guessing the problem must be with the code for the iteration methods.
My code so far is as follows,
I can't seem to see the problem. Any advice or help appreciated.
Many thanks.
Garrett Rowe
Ranch Hand
Joined: Jan 17, 2006
Posts: 1295
posted
0
In this part of your code:
The while loop will continue until you get the beginning of the list. Instead use an if in place of the while statement:
and it will only execute once if the condition is true.
Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
celine scarlett
Ranch Hand
Joined: Nov 06, 2005
Posts: 93
posted
0
Hi,
Thanks for the reply. That works correctly for one click of the backwards button, but it will not go back beyond one position.
Any ideas what the problem might be?
Many thanks for the help.
Garrett Rowe
Ranch Hand
Joined: Jan 17, 2006
Posts: 1295
posted
0
Thinking of this problem a little more, I'm not sure if an Iterator is the best way to go. Maybe if you defined a class level (or less if appropriate) int variable
Then if you want to go forward you could put this int your method
[ January 28, 2006: Message edited by: Garrett Rowe ]
celine scarlett
Ranch Hand
Joined: Nov 06, 2005
Posts: 93
posted
0
Hi,
I've tried that solution. The problem is that by setting currentPosition to 0, for some reason it is ignoring the arrayList, and starting at 0. It then increments the value from 1 upwards and does not go through the arrayList.
Maybe there's a way to link this to the arrayList, but I'm getting really confused at the moment.
Anybody got any ideas?
Thanks.
Layne Lund
Ranch Hand
Joined: Dec 06, 2001
Posts: 3061
posted
0
Please post some code to show us what you tried. Garrett's suggestion should work, if you implement it properly. We need to see your code in order to help you figure out what you did differently.
Thanks for the reply. I have tried implementing the actionPerformed methods as suggested above, and I get a really weird result. It outputs an error saying 'ArrayIndexOutofBoundsException: -1'. Any ideas why this is happening?
My current is as follows,
I really can't see where the problem is with these methods. It's all very strange.
Any help or advice really appreciated at this stage.
[ January 29, 2006: Message edited by: celine scarlett ] [ January 29, 2006: Message edited by: celine scarlett ]
Garrett Rowe
Ranch Hand
Joined: Jan 17, 2006
Posts: 1295
posted
0
I dont see the error myself. One thought would be to make sure that when the current index is 0, the back button is always disabled. And anytime you add a new value to the ArrayList, the current index should be set to (theList.size - 1).
celine scarlett
Ranch Hand
Joined: Nov 06, 2005
Posts: 93
posted
0
Hi,
I've managed to get the buttons to alternate between decrementing and incrementing the index values. However, they only jump backwards and forwards to the first and last values in the list.
eg: a list contains 22,32,42,52 and a user presses the backwards button, it will go back to 22 and not 42 from 52 and vice versa
Current code for the two actionPerformed methods is as follows,
I've checked the actionPerformed method for the convert button, but I don't see how that would affect the other buttons. Anyway, the code for that method is as follows,
Any ideas what is causing this problem?
Thanks for the help!
celine scarlett
Ranch Hand
Joined: Nov 06, 2005
Posts: 93
posted
0
Hi,
With alot of help, I've managed to get the actionPerformed methods of the backward and forward buttons going through the arrayList correctly, with the exception of one small problem.
When a user enters a sequence of numbers eg: 22,32,42,52 and then presses the back button, they are taken back to 22, instead of 52. Then the user has to press the forward button. From this point on, the buttons work perfectly, and go backwards and forwards through the list.
Does anybody know why this error might be occurring?
My current code for the actionPerformed methods is as follows,
Any help or advice appreciated!
Tony Morris
Ranch Hand
Joined: Sep 24, 2003
Posts: 1608
posted
0
Java collections are poorly implemented as you are observing. I sympathise with your frustrations and assure you that you are not entirely at fault for being confused.
Use a Sequence and a BidirectionalIterator. Still broken (ala Java), but simpler from a client perspective. ContractualJ [ January 29, 2006: Message edited by: Tony Morris ]
When you add a new value to the list, it is added to the end of the list (which means you are now at the end of the list). The currentPosition variable should be updated to reflect this fact.
[ January 29, 2006: Message edited by: Garrett Rowe ]
celine scarlett
Ranch Hand
Joined: Nov 06, 2005
Posts: 93
posted
0
Hi,
That's brilliant. Thank you so much for your help. I now have working backwards and forwards buttons.
It's been a long weekend, but I've finally got there.