The Artima Developer Community
Sponsored Link

Java Answers Forum
How to jump to a point in a Program?

1 reply on 1 page. Most recent reply: Feb 25, 2002 8:41 PM by Matt Gerrans

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic    
Flat View: This topic has 1 reply on 1 page
Avin Sinanan

Posts: 28
Nickname: avin
Registered: Feb, 2002

How to jump to a point in a Program? Posted: Feb 25, 2002 6:46 PM
Reply to this message Reply
Advertisement
Hello I've made a program which adds a button to the JPanel. After that the program has 3 For loops.

I've incuded the code.

How does one alter the code so that when I press the Button the program jumps to the beginning of the 2nd for loop. It must jump to the 2nd for loop even if it is the 3rd loop at the time or even if it is the 1st for loop.

It should not complete the loop it is presently in. It miust exist the loop and jump to the 2nd loop immediately.

Can someone help me with this please.

yours respectuflly Avin Sinanan
Here is the code --->


import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.Random.*;

class DemoOne
{
public static void main(String[] args)
{
JFrame frame = new JFrame();
JPanel pane = new JPanel();
frame.setSize(300,300);
frame.setVisible(true);



JButton button = new JButton("Press Me to Jump");
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){

/*
Put action here when the button is pressed
When button is pressed the program should jump to
the 2nd for loop even if it is another for loop at the time.
*/
}
});

pane.add(button);
frame.getContentPane().add(pane);


for(int i =0 ; i<100; i++)
{
System.out.println("First" + i);
}

for(int j =0 ; j<100; j++)
{
System.out.println("Second" + j);
}

for(int k =0 ; k<100; k++)
{
System.out.println("" + k);
}


}
}


Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: How to jump to a point in a Program? Posted: Feb 25, 2002 8:41 PM
Reply to this message Reply
You can't go jumping around in a method like that. Anyway, that is horrible programming style even in the languages, such as Visual Basic, that do allow it.

What you really want to do is partition your tasks with the granularity that will allow you to accomplish whatever effect you are seeking.

Which brings us to the question: What exactly are you trying to accomplish with this sort of thing? Are you trying to have a cancel button that will cancel some activity and continue with another? This could be accomplished by having a little task queue and having your cancel button event listener call a cancel() method (which probably sets a "I've been cancelled" flag member variable) on the current running task (it would be be checking in its loops to see it had been cancelled, of course). If a task is cancelled (or completes), then the next task begins.

Flat View: This topic has 1 reply on 1 page
Topic: sorting arrays Previous Topic    

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use