using List Iterator with swing buttons and actionPerformed method - any hel
Hi,
I've added an arrayList to my swing program so I can store data input and received from the user and the program. I now need to access this data from the arrayList and step through it with forward and backward buttons.
I've added a ListIterator to each actionPerformed method for the forward and backward buttons, but there is a problem with the output.
When a user clicks the backward button, it outputs the first value in the arrayList, instead of the preceding input value, and vice versa for the forwardButton.
My code for the actionPerfomed methods is currently as follows,
publicclass BackwardActionimplements ActionListener{
publicvoid actionPerformed(ActionEvent event){
ListIterator e = theList.listIterator(theList.size()-1);
while (e.hasPrevious())
{
tempCelsius.setText(e.previous() +"");
//System.out.println(e.previous()+"");
}
}
}
publicclass ForwardActionimplements ActionListener{
publicvoid actionPerformed(ActionEvent event){
ListIterator e = theList.listIterator();
while (e.hasNext())
{
tempCelsius.setText(e.next() +"");
//System.out.println(e.next() + "");
}
}
}
I've tested the output of the arrayList, both forward and backward to a console from the buttons using 'System.out.println', and the output from a user inut of 10,20,30,40 is
40
30
20
10
10
20
30
40
I really can't see why this won't work.
Please help!
[2401 byte] By [
escarletta] at [2007-10-2 10:51:35]

> When a user clicks the backward button, it outputs the first value in the arrayList, instead of the preceding input value, and vice versa for the forwardButton.
Well, of course it does. Every time you click a button you create a new ListIterator, and then iterate through the entire Iterator so it will obviously only display the text from the last item.
Don't even worry about using an iterator. Just keep an index of the current position in the ArrayList. When you go forward in increment the index and then use the get(..) method of ArrayList. To go backwards you would decrement the index. Of course your code would next to handle the condition when you reach the end of the ArrayList you would go back to the beginning and visa versa.
Hi,
I've been trying to solve this problem with a ListIterator, but have finally given up with that solution. It kept going back to the very start of the arrayList.
All I'm trying to do is allow a user to backward and forward through values that they have input within a textfield in a swing program. Then, if they add another input value as they go through the previous values, that new value will be added to the list in the current position.
eg: a user inputs 22,33,44,55. Then they go back through the values 44,33,22. Then they go forwards 33,44. Then they enter a new value in the textfield, 38, and that value is added at the current position in the list, between 44 and 55.
I've tried using the index of the arrayList, as suggested, but this is producing an 'ArrayIndexOutOfBoundsException: -1' etc. My current code for the actionPerformed methods is as follows,
public class BackwardAction implements ActionListener {
public void actionPerformed(ActionEvent event) {
theListCurrentIndex--;
tempCelsius.setText(theList.get(theListCurrentIndex) + "");
forwardButton.setEnabled(true);
if (theListCurrentIndex==0)
backwardButton.setEnabled(false);
}
}
public class ForwardAction implements ActionListener {
public void actionPerformed(ActionEvent event) {
theListCurrentIndex++;
tempCelsius.setText(theList.get(theListCurrentIndex) + "");
backwardButton.setEnabled(true);
if (theListCurrentIndex == theList.size()-1)
forwardButton.setEnabled(false);
}
}
Any ideas why this error is occurring?
Any adivce or help really appreciated at this point!
> Any adivce or help really appreciated at this point!
Yes. Learn how to debug programs.
Add a System.out.println(...) at the beginning and end of your actions to check what the value of your index is. You should be able to follow the increment and decrement of the index. When you see something out of the ordinary then you fix the problem
We can't help you based on the code provided. The code in the ActionListeners looks reasonable, but maybe you have code somewhere else in you program that is causing the problem.
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,
public class BackwardAction implements ActionListener {
public void actionPerformed(ActionEvent event) {
tempCelsius.setText(theList.get(theListCurrentIndex) + "");
theListCurrentIndex--;
forwardButton.setEnabled(true);
if (theListCurrentIndex < 0)
theListCurrentIndex = theList.size()-1;
backwardButton.setEnabled(false);
}
}
public class ForwardAction implements ActionListener {
public void actionPerformed(ActionEvent event) {
tempCelsius.setText(theList.get(theListCurrentIndex) + "");
theListCurrentIndex++;
backwardButton.setEnabled(true);
if (theListCurrentIndex == theList.size())
theListCurrentIndex = 0;
forwardButton.setEnabled(false);
}
}
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,
public class ClickAction implements ActionListener {
public void actionPerformed(ActionEvent event) {
// Parse degrees Celsius as a double
// and convert to Fahrenheit.
String celsiusAsText = tempCelsius.getText();
try { //start of the exception handling
Double celsiusAsDouble = Double.parseDouble(celsiusAsText);
double fahrenheitAsDouble = celsiusAsDouble*1.8+32;
fahrenheitLabel.setText(fahrenheitAsDouble + " Fahrenheit");
theList.add(celsiusAsDouble);
} catch(Exception e)
{fahrenheitLabel.setText("? Fahrenheit");}
}
}
Any ideas what is causing this problem?
Thanks for the help!
Instead of theListCurrentIndex-- try the -- in the front. So you are telling the program to subtract first right now...I think that could be an issue. Try it and let me know if that helps. Cheers,Surya
Hi,
Thanks for the reply. I've already tried that solution. It gives an 'ArrayIndexOutOfBoundsException: -1', which I assume means that it initially sets the index to -1 which is not possible, Therefore the error.
I really feel like I'm banging my head against a brick wall with this problem.
All I'm trying to do is save some user input, from a textfield, to an arrayList and then go through the list backwards and forwards by using two buttons.
You wouldn't think it would be that hard!
Anyway, any advice or help really appreciated at this point!
For some reasons when your pointer in BackwardAction is at index 0, you make a wrap-around and then set the backward-butten=false. You should either do a wrap-around or disable buttons but not both. Btw. you have forgotten {} in your if-statements. Here is the code I would use without wraparounds.
public class BackwardAction implements ActionListener {
public void actionPerformed(ActionEvent event) {
theListCurrentIndex--;
if (theListCurrentIndex <= 0) {
theListCurrentIndex = 0;
backwardButton.setEnabled(false);
}
tempCelsius.setText(theList.get(theListCurrentIndex) + "");
forwardButton.setEnabled(true);
}
}
public class ForwardAction implements ActionListener {
public void actionPerformed(ActionEvent event) {
theListCurrentIndex++;
if (theListCurrentIndex >= theList.size()-1) {
theListCurrentIndex = theList.size()-1;
forwardButton.setEnabled(false);
}
tempCelsius.setText(theList.get(theListCurrentIndex) + "");
backwardButton.setEnabled(true);
}
}
Note: The code is untested
Hi,
Thank you so much for your reply. It's almost spot on now. The only problem is when you press the backward button for the first time. It starts at the beginning of the arrayList, instead of the previous value entered.
eg: the user enters 22,32,42,52 and then presses the back button. The list should go back to 42, but instead it goes back to 22. The you press the forward button and it works perfectly.
The rest of it works perfectly. Any idea why this might be happening?
Many thanks for the help!
> eg: the user enters 22,32,42,52 and then presses the
> back button. The list should go back to 42, but
> instead it goes back to 22. The you press the forward
> button and it works perfectly.
>
> The rest of it works perfectly. Any idea why this
> might be happening?
Yes, theListCurrentIndex is probably initialized to be = 0 instead of = size()-1. Set theListCurrentIndex = size()-1 at the same time as you enter those values.
Hi,Thank you so much for all your help. It is now working perfectly, and I actually have the correct actions for the forward and backward buttons.Have a great week!