This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes How to jump to a point in a program? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to jump to a point in a program?" Watch "How to jump to a point in a program?" New topic
Author

How to jump to a point in a program?

Avin Sinanan
Ranch Hand

Joined: Nov 07, 2001
Posts: 109
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);
}


}
}


yours respectfully<br />Avin Sinanan
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Hello,
Perhaps I'm missing your point a bit...
What about putting each loop in it's own method and then running a thread to handle the button event and call the method with the second loop when the button is pushed? The methods could otherwise be called from main in whatever order you wanted.
Good Luck,
-Dirk Schreckmann


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Avin Sinanan
Ranch Hand

Joined: Nov 07, 2001
Posts: 109
I could do that. But its a bit to late to do that now. You see I've already written a program in that format. Its kinda long..really long. And I can't go back and write it back. Its due friday and I'll take to long. So am worndering if you or anyone out there can devise a technique to jump to a point while still maintaing the program format.
Thanks for the advice and giving me some of your time.
Yours respectfully Avin Sinanan
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
There is no way to do what you want without involving multiple threads. When you click a button, the event dispatch thread eventually calls your actionPerformed() method, and that code begins running. It will continue running until the end of the method. While it is running, the UI is dead-frozen. YOU can't click any buttons, so there is no way to capture any button clicks until the method completes.
The only way to implement what you want is to spawn a worker thread in the actionPerformed() method, which runs your loop code as well as saves state about the running thread. Then later, when your actionPerformed() method is called in response to another button click, you can see if you are currently running some code in another thread, and if this is indeed the "special" button, and if so, set some shared state so the other thread can branch on this information and exit/break out of the current loop it's in, etc.


Rob
SCJP 1.4
Avin Sinanan
Ranch Hand

Joined: Nov 07, 2001
Posts: 109
Thanks for the advice Ross.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to jump to a point in a program?
 
Similar Threads
An interesting Program
Swing GUI wierdness
Save and Open files ( realistically) ?? code included
JFileChooser
How to make a proper button listener?