13 Replies - 877 Views - Last Post: 14 May 2011 - 06:12 AM Rate Topic: -----

#1 xxxjayxxx  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 71
  • Joined: 01-August 10

How can I solve this problem ?

Posted 12 May 2011 - 07:09 AM

try {
                            convertbutton.setText("STOP");
                            OpenSecreeningBase.setEnabled(false);
                            SaveScreeningBase.setEnabled(false);
                            FileWriter writer = new FileWriter(screeningbase);
                            for (File moleculeFile : screeningBase.listFiles()) {
                                BitSet fingerprint = convertToFingerprint(moleculeFile); //call function

                                writer.append(moleculeFile.getName());
                                Screening_Ready.setText("Processing : " + moleculeFile.getName() + " ...");
                                writer.append("  ");
                                {
                                    for (int i = 0; i < fingerprint.size(); i++) {

                                        if (fingerprint.get(i)) {
                                            writer.append("1");
                                            writer.flush();
                                        } else {
                                            writer.append("0");
                                            writer.flush();
                                        }



                                    }
                                    writer.append("\n");
                                }
                            }
                            writer.close();
                            convertbutton.setText("CONVERT");[/b]]
                            

                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            })).start();


I had designed a program. This program enable to convert thing. However the main point is when I click the convert button the word "CONVERT" will become "STOP" and become back to "CONVERT" when the program stop. But how can I stop the program when I click the button again ?

This post has been edited by xxxjayxxx: 12 May 2011 - 07:12 AM


Is This A Good Question/Topic? 0
  • +

Replies To: How can I solve this problem ?

#2 sakshamkum  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 19
  • View blog
  • Posts: 232
  • Joined: 09-June 09

Re: How can I solve this problem ?

Posted 12 May 2011 - 07:37 AM

it is not the full code. please post the full code or the function which contains this code
Was This Post Helpful? 0
  • +
  • -

#3 xxxjayxxx  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 71
  • Joined: 01-August 10

Re: How can I solve this problem ?

Posted 12 May 2011 - 08:04 AM

View Postsakshamkum, on 12 May 2011 - 07:37 AM, said:

it is not the full code. please post the full code or the function which contains this code

What do u mean not the full code ? The rest is the public class and actionlistener.
Was This Post Helpful? 0
  • +
  • -

#4 Renagado  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 117
  • View blog
  • Posts: 388
  • Joined: 14-June 09

Re: How can I solve this problem ?

Posted 12 May 2011 - 01:47 PM

Do you mean to interrupt the program while it is converting?

Only one way to do that, and that's by threading. See when the program is running a lengthy operation, the program is busy running the code doing that, and can never respond to user input. So you separate the converting processing from the UI thread, so the latter can halt the first. However threading is something you need to learn about before using it, DIC has some info on it in the tutorial section, also you could try the java trail on concurrency.
Was This Post Helpful? 0
  • +
  • -

#5 xxxjayxxx  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 71
  • Joined: 01-August 10

Re: How can I solve this problem ?

Posted 12 May 2011 - 06:36 PM

View PostRenagado, on 12 May 2011 - 01:47 PM, said:

Do you mean to interrupt the program while it is converting?

Only one way to do that, and that's by threading. See when the program is running a lengthy operation, the program is busy running the code doing that, and can never respond to user input. So you separate the converting processing from the UI thread, so the latter can halt the first. However threading is something you need to learn about before using it, DIC has some info on it in the tutorial section, also you could try the java trail on concurrency.


Sorry I quite confuse about threading ... is multithreading = threading ?
Which means that something like this :
public class Convert_Test_File implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            (new Thread(new Runnable() {

                public void run() {



}
            })).start();
        }
    }


If I want my life more easy, I can create a stop button so that the process of coding is more simple ?

This post has been edited by xxxjayxxx: 12 May 2011 - 06:39 PM

Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10439
  • View blog
  • Posts: 38,658
  • Joined: 27-December 08

Re: How can I solve this problem ?

Posted 12 May 2011 - 06:44 PM

Don't mix Threads and Swing, as Swing isn't Thread-safe. There are plenty of other options like Swing Timer and SwingWorker that don't violate the Swing Thread-safety rule.

My suggestion is to manage a boolean instance field. When you want the STOP state to be in place, invert the boolean. And just test that the boolean is of the stopping value. If so, don't execute any actions.

You might also want to check out the following links:
Dogstopper's blog entry on state machines
Finite State Automaton
My tutorial on Event-Driven programming

Hope this helps some. :)
Was This Post Helpful? 1
  • +
  • -

#7 xxxjayxxx  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 71
  • Joined: 01-August 10

Re: How can I solve this problem ?

Posted 12 May 2011 - 08:49 PM

View Postmacosxnerd101, on 12 May 2011 - 06:44 PM, said:

Don't mix Threads and Swing, as Swing isn't Thread-safe. There are plenty of other options like Swing Timer and SwingWorker that don't violate the Swing Thread-safety rule.

My suggestion is to manage a boolean instance field. When you want the STOP state to be in place, invert the boolean. And just test that the boolean is of the stopping value. If so, don't execute any actions.

You might also want to check out the following links:
Dogstopper's blog entry on state machines
Finite State Automaton
My tutorial on Event-Driven programming

Hope this helps some. :)


got any tutorial that show me how to use swingworker ? Does that means that if I using swing I cant do multithreading to my program ?

This post has been edited by xxxjayxxx: 12 May 2011 - 08:50 PM

Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10439
  • View blog
  • Posts: 38,658
  • Joined: 27-December 08

Re: How can I solve this problem ?

Posted 12 May 2011 - 08:54 PM

My first link goes into some detail on SwingWorker, and Dogstopper has a tutorial as well.

Quote

Does that means that if I using swing I cant do multithreading to my program ?

It means that your Threads should not interact with your GUI. If you have Threads for another part of your program, like client-server interactions, etc., you need to very carefully control access to the data so your GUI doesn't generate a ConcurrentModificationException or simply freeze up.
Was This Post Helpful? 1
  • +
  • -

#9 xxxjayxxx  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 71
  • Joined: 01-August 10

Re: How can I solve this problem ?

Posted 12 May 2011 - 09:48 PM

public class Convert_Screening_File implements ActionListener {

        public void actionPerformed(ActionEvent e) {
SwingUtilities.invokeLater(new Runnable() {

            public void run() {


});

}
}

something like this ?

This post has been edited by xxxjayxxx: 12 May 2011 - 09:49 PM

Was This Post Helpful? 0
  • +
  • -

#10 xxxjayxxx  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 71
  • Joined: 01-August 10

Re: How can I solve this problem ?

Posted 13 May 2011 - 11:51 AM

View Postxxxjayxxx, on 12 May 2011 - 09:48 PM, said:

public class Convert_Screening_File implements ActionListener {

        public void actionPerformed(ActionEvent e) {
SwingUtilities.invokeLater(new Runnable() {

            public void run() {


});

}
}

something like this ?


???
Was This Post Helpful? 0
  • +
  • -

#11 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10439
  • View blog
  • Posts: 38,658
  • Joined: 27-December 08

Re: How can I solve this problem ?

Posted 13 May 2011 - 02:30 PM

Quote

My suggestion is to manage a boolean instance field. When you want the STOP state to be in place, invert the boolean. And just test that the boolean is of the stopping value. If so, don't execute any actions.

Not exactly what I suggested in my last post.
Was This Post Helpful? 0
  • +
  • -

#12 xxxjayxxx  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 71
  • Joined: 01-August 10

Re: How can I solve this problem ?

Posted 14 May 2011 - 01:10 AM

sorry I'm lost :(
Was This Post Helpful? 0
  • +
  • -

#13 Renagado  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 117
  • View blog
  • Posts: 388
  • Joined: 14-June 09

Re: How can I solve this problem ?

Posted 14 May 2011 - 04:22 AM

What he means(I guess) is using a bool as a flag. Say you do start a new thread from your GUI thread. That means 2 things are running at the same time. That's cool as long as they don't touch each others data at the same time, or if you let the 2nd thread touch the GUI. So let's say you make a boolean called isInterrupted and set it to false.

Then in your 2nd thread you probably have some data processing in a loop. Then you just check if the GUI thread has set the bool to true(in the loop), if so you cancel the process.

That should work, since the state of the bool won't change that often but don't use this for switching processing/reading between two threads(found that one out the hard way)

As an alternative you might read up on the SwingWorker, but at least do some learning on threading/concurrency. It's not very easy. Hope you can continue now.
Was This Post Helpful? 1
  • +
  • -

#14 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: How can I solve this problem ?

Posted 14 May 2011 - 06:12 AM

View Postxxxjayxxx, on 13 May 2011 - 02:51 PM, said:

View Postxxxjayxxx, on 12 May 2011 - 09:48 PM, said:

public class Convert_Screening_File implements ActionListener {

        public void actionPerformed(ActionEvent e) {
SwingUtilities.invokeLater(new Runnable() {

            public void run() {


});

}
}

something like this ?


???

Not really, you are just postponing the problem trying to hiding it :v: :)

In your actionPerformed() you are in a thread, do your job there no need to fork another thread
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1