Hi everybody,

my problem is as follow, I have two classes myClass and myGUI

myClass code is

public class MyClass 
{
	
	public void start()
	{
		while (true)
   		{
			// do some stuff
		}
	}
	
	public void stop()
	{
		System.out.println("Hello from stop method!");
	}
	
}

myGUI code is

public class MyGUI extends javax.swing.JFrame 
{
MyClass cl = new MyClass();
 
public static void main(String[] args) 
	{
				MyGUI inst = new MyGUI();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
	}
	
public MyGUI()
       {
		super();
		initGUI();
       }
 
private void initGUI() 
{
// some GUI code
myStartBtn.addActionListener(new ActionListener() 
				{
					public void actionPerformed(ActionEvent evt) 
					{
						cl.start();
					}
				});
myStopBtn.addActionListener(new ActionListener() 
				{
					
					public void actionPerformed(ActionEvent evt) 
					{
						cl.stop();
					}
				});
}
}

as you can see, the start() method puts the program in an infinite loop, which makes me unable to call the stop() method using myStopBtn... So, I think this can be done using threads but actually I don't know how exactly am gonna do this

any help will be appreciated.

Thanks

Recommended Answers

All 5 Replies

You are right. You need to use Thread to accomplish your requirement.

Your class should extend Thread, I am providing a simple sample program that masks a password entry. I am sure you can tweak it to meet your needs

public class SomeClass {
    public static void main (String[] arg) {
         //...
         System.out.print(" Enter User Name > ");
         String userName = stdin.readLine();

         MaskPassword maskPassword = new MaskPassword ();

         System.out.print(" Enter Password > ");         
         maskPassword.start();
         String password = stdin.readLine();
         maskPassword.stop();
         // other processes...
    }
}


class MaskPassword extends Thread {
    private boolean running = true;

    public void run() {
        while (running) {
            System.out.print("\b ");
        }
    }

    public synchronized void stop() {
        running = false;
    }
}

Hope this helps.

Thanks for your reply man, I did it, but another problem appeared

First this is how I tweaked the code

myStartBtn.addActionListener(new ActionListener() 
{
	public void actionPerformed(ActionEvent evt) 
	{
				
		th = new Thread(new Runnable()
		{
			public void run()
			{
							
	  		myServer.start();
			myMessagesLable.setForeground(Color.BLUE);
			myMessagesLable.setText("Server Started!");
			}
		});
	th.start();
							
	}
	});

myStopBtn.addActionListener(new ActionListener() 
{
					
      public void actionPerformed(ActionEvent evt) 
      {
		myMessagesLable.setForeground(Color.RED);
		myMessagesLable.setText("Server stopped!");
		Server.flag = -1;
		th.stop(); // I don't wan't to use this method !!!!
		myS.stop(); // here am closing an open port
				
	}
});

as you can see in the code I need to stop the thread without the deprecated stop() method, but I don't know how. I can't use a boolean variable as you are doing in your code because I can't do a loop in the run() method.

So what do you think?

If you don't want to use stop() method because it is deprecated, you can use one of your own method, say halt() and in there you set your boolean variable to true/false to stop the loop.

But if your program uses different logic than a while loop then you should put some logic in myS.stop() method to terminate your thread.

How do u write a code for vehicle registration checker with maximum of 7 character input. The first two letters searches for the list of cities, the following 2(numbers) searches for random numbers of the car(pre written) and the last 3 shows a car manufacturer code..
When a button is clicked the information searched appears in a single text box. eg..
GA56TKR
GA gatwick
56 registration date
TKR Toyota

Thanks

densman, do not hijack threads to ask unrelated questions. You need to make a new thread for this and you need to post the code that you have to start with. No one is going to just write it for you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.