Why this executes only once?

Hello people!

Ive been trying to make this code to cycle through an array... my array is already finished and working ok within my program... but the JFrame I am trying to implement is not going to good.

I want to make two buttons work, previous and next buttons...

made to cycle back and forth inside my array... before I bothered with the array limit being broken, i wanted to see if I could make it work... but starting from zero, it only adds 1 and wont add 1 once again... so... could anyone tell me why and how can I work around this?

here is my problem code so far...

privatestaticclass ButtonFrameextends JFrame{

private JButton prevButton;

private JButton nextButton;

private JTextArea items;

/** Creates a new instance of ButtonFrame */

public ButtonFrame(){

super("Inventory Items Browser");

int i =0;

setLayout(new FlowLayout());

Main myMain =new Main();

items =new JTextArea(1,5);

items.setText(myProducts[i].toString()+"\n"+myMain.getTotalValue());

items.setEditable(false);

add(items);

prevButton =new JButton("Previous");

add(prevButton);

nextButton =new JButton("Next");

add(nextButton);

ButtonHandler handler =new ButtonHandler();

prevButton.addActionListener(handler);

nextButton.addActionListener(handler);

}

privateclass ButtonHandlerimplements ActionListener{

publicvoid actionPerformed(ActionEvent event){

Main myMain =new Main();

ButtonHandler handler =new ButtonHandler();

int nxtpress=0;

if(event.getActionCommand().equals("Next") ){

nxtpress++;

items.setText(myProducts[+nxtpress].toString()+"\n"+myMain.getTotalValue());

}

[3005 byte] By [Elvenelfa] at [2007-11-27 11:20:20]
# 1

1) Why are you declaring your class static and private?

2) You should have your program working fully in a non-GUI way before making it into a GUI. You say you've done this? If so, good for you. You may want to show us this running non-GUI code.

3) To let us help you with the GUI (and non GUI) code, please post a Short, Self Contained, Compilable and Executable, Example Program (SSCCE) that demonstrates the incorrect behaviour. You can find out more about this construct here:

url http://homepage1.nifty.com/algafield/sscce.html

petes1234a at 2007-7-29 14:42:03 > top of Java-index,Java Essentials,Java Programming...
# 2

I declared static and private because this class is already inside Main.java ... at any rate, i am not really certain that that is the correct way to go... but it sure didnt give any problems that I know of...

Misunderstanding...

What I have previous to this was a non-GUI program that was taking user input and patching towards the array names. If found a match it would display the toString method then displaying the items information.

What I want to add now are the two buttons so a user can cycle back and forth within the array, and if the array is already at the final position and a user presses next, then it should display the first item or back to zero position... That part shouldnt be too tough once I get this part running... i am not sure why this is not adding 1 to the nxtpress variable... it only does it once and then subsequent presses does nothing...

We are talking the same program as in here:

http://forum.java.sun.com/thread.jspa?threadID=5192731&start=0&tstart=0

for a copy of my program as it is right now (with problem and all): You can download a rar file with all the stuff from:

http://www.tucms.com/downloads/Inventory Program.rar

Message was edited by:

Elvenelf

Message was edited by:

Elvenelf

null

Elvenelfa at 2007-7-29 14:42:03 > top of Java-index,Java Essentials,Java Programming...
# 3

This is your problem

int nxtpress=0;

Everytime an event is fired nxtpress is set to zero.

_helloWorld_a at 2007-7-29 14:42:03 > top of Java-index,Java Essentials,Java Programming...
# 4

ok, so instead of having an if statement i am now using a while loop, i also removed the nxtpress variable and replaced the array value number for +1... but it hangs... so thats not the way to go either...

I also tried adding a parameter to ButtonFrame... and using that parameter within the action event to advance the array position, but i failed at that too...

Elvenelfa at 2007-7-29 14:42:03 > top of Java-index,Java Essentials,Java Programming...
# 5

Change your listener code to look like this

private class ButtonHandler implements ActionListener{

int nxtpress=0; //Moved to here

public void actionPerformed(ActionEvent event){

Main myMain = new Main();

ButtonHandler handler = new ButtonHandler();

if(event.getActionCommand().equals("Next") ){

nxtpress++;

items.setText(myProducts[+nxtpress].toString()+"\n"+myMain.getTotalValue());

}

_helloWorld_a at 2007-7-29 14:42:03 > top of Java-index,Java Essentials,Java Programming...
# 6

Do you understand what difference that move makes? If not I can explain if you like.

_helloWorld_a at 2007-7-29 14:42:03 > top of Java-index,Java Essentials,Java Programming...
# 7

that will still initialize my nxtpress variable without resetting its value to zero on each button press... am I right!?

Elvenelfa at 2007-7-29 14:42:03 > top of Java-index,Java Essentials,Java Programming...
# 8

> that will still initialize my nxtpress variable

> without resetting its value to zero on each button

> press... am I right!?

Did you try it? You had nxtpress = 0 in your method, when this method is entered you are setting this value to zero and then moving it up to 1 when you leave the method and re-enter you start all over again. Now it is outside of the method as an instance variable it will start from zero and keep incrementing each time you fire events and call the method. Try it out.

_helloWorld_a at 2007-7-29 14:42:03 > top of Java-index,Java Essentials,Java Programming...
# 9

Yes. I tried it out. =) It works nicely. I am now finding a way of resetting its value to zero when it reaches the last array index value, so it doesnt throw an out of bounds exception for the array.

if(event.getActionCommand().equals("Next") ){

if(nxtpress==myProducts.length){//added this chk

nxtpress=0;

}else{

if(nxtpress<myProducts.length){//and this chk

nxtpress++;

}

}

items.setText(myProducts[+nxtpress].toString()+"\n"+myMain.getTotalValue());

}

I added those checks to that particular end, and it does it, but I still see that it throws the exception anyway... even though the program still runs and it advances back into 0 index position... o_O I am not sure if this is normal behavior or if it is alright to leave it like that...

Edit, i forgot that nxtpress will nvr be == to myProducts.length... so I made it == to myProducts.length-1 and it does not throw an exception anymore. =)

null>

Elvenelfa at 2007-7-29 14:42:03 > top of Java-index,Java Essentials,Java Programming...
# 10

You can do it like this also

if(event.getActionCommand().equals("Next") ){

nxtpress = (nxtpress <= myProducts.length-1 ? nxtpress++ : 0);

items.setText(myProducts[+nxtpress].toString()+"\n"+myMain.getTotalValue());

}

http://www.janeg.ca/scjp/oper/ternary.html

It is like saying

if(nxtpress <= myProducts.length-1) {

nxtpress++;

} else {

nxtpress =0;

}

it is just a shorthand way to achieve the same thing, not necessarily better.

_helloWorld_a at 2007-7-29 14:42:03 > top of Java-index,Java Essentials,Java Programming...
# 11

its nice to know about that alternate syntax... i am familiar with it from php, although i must admit that i did not know that I could use it here as well.

I am now having a bit of trouble figuring out this math part... it somehow acts funny... no exception throws, but it is adding more than one somehow advancing me to the index position 1 instead of 0 when i am at the last index and press next.

private class ButtonHandler implements ActionListener{

int press=0;

public void actionPerformed(ActionEvent event){

Main myMain = new Main();

ButtonHandler handler = new ButtonHandler();

if(event.getActionCommand().equals("Next") ){

if(press==myProducts.length-1){

press=0;

}

if(press<myProducts.length){

press++;

}

}

if(event.getActionCommand().equals("Previous")){

if(press==0){

press=myProducts.length;

}

if(press>0){

press--;

}

}

items.setText(myProducts[press].toString()+"\n"+myMain.getTotalValue());

}

}

Elvenelfa at 2007-7-29 14:42:03 > top of Java-index,Java Essentials,Java Programming...
# 12

Stop and think about what is happening with this code here:

if(press==myProducts.length-1){

press=0;

}

if(press<myProducts.length){

press++;

}

Let me know what you think happens when press == myProducts.length-1 (ie the last press for next before reset.>

_helloWorld_a at 2007-7-29 14:42:03 > top of Java-index,Java Essentials,Java Programming...
# 13

I think that when press == myProducts.length-1

press becomes zero again, it gets reset, so it should display index zero ... first item in array?

Elvenelfa at 2007-7-29 14:42:03 > top of Java-index,Java Essentials,Java Programming...
# 14

Well this code is all in the same method so is processed by the same event. So it doing as you have told it to do.

//On the last press of next the press variable

//is equal to myProducts.length-1 so below it gets set to zero

if(press==myProducts.length-1){

press=0;

}

//Straight afterwards you are checking to see if press

//is less than myProducts.length which it is as you

//just set it to zero above , so they add one and then carry on

if(press<myProducts.length){

press++;

}

You should be just doing this

//if it is not == to myProducts.length-1 then we keep

//adding in the else block. There is only one test you

//need to make, that in the if block

if(press==myProducts.length-1){

press=0;

}

else {

press++;

}

>

_helloWorld_a at 2007-7-29 14:42:03 > top of Java-index,Java Essentials,Java Programming...
# 15

LOL

Just when I had found that this worked lol Thanks a lot... hehe

if(press==myProducts.length-1){

press=0;

}

if(press<myProducts.length){

press++;

}

I like yours better, it is less cluttered... My code has more stuff thats not needed ... I'll keep refining to make it more readable... =) thanks so much for help given.>

Elvenelfa at 2007-7-29 14:42:08 > top of Java-index,Java Essentials,Java Programming...
# 16

> thanks so

> much for help given.

Np, glad to see you got it working.

_helloWorld_a at 2007-7-29 14:42:08 > top of Java-index,Java Essentials,Java Programming...