Jump to content

Anyone can please help me ? I'm really stuck ...

- - - - -

  • Please log in to reply
16 replies to this topic

#1
xxxxjayxxx

xxxxjayxxx

    Programmer

  • Members
  • PipPipPipPip
  • 123 posts
 public class Stop_Convert implements ActionListener {


        public void actionPerformed(ActionEvent e) {

            

                private Thread myStopThread;


                public void run() {

                    {

                        Thread stopThread = myStopThread;

                        myStopThread = null;

                        if (stopThread == null) {

                            stopThread.interrupt();

                        }

                    }

                }

            }

        }

    }
public class Convert_Test_File implements ActionListener {


        public void actionPerformed(ActionEvent e) {

            


                public void run() {

                    {

                        refreshTab.setVisible(false);

                        about.setVisible(false);

                        try {

                            if (Test_File_Directory.getText().trim().length() == 0) {

                                JOptionPane.showMessageDialog(null, "\t\tTest molecule file not selected!", "Missing File",

                                        JOptionPane.WARNING_MESSAGE);

                            } else if (Save_Test_Directory.getText().trim().length() == 0) {

                                JOptionPane.showMessageDialog(null, "\t\tPlease select a directory to save the Fingerprints!", "Missing Directory",

                                        JOptionPane.WARNING_MESSAGE);

                            } else if (Test_File_Directory.getText().trim().length() == 0 && Save_Test_Directory.getText().trim().length() == 0) {

                                JOptionPane.showMessageDialog(null, "\t\tFile not selected!", "Missing File",

                                        JOptionPane.WARNING_MESSAGE);

                            } else if (testfile.length() != 0) {

                                openTestFile.setEnabled(false);

                                saveTestFile.setEnabled(false);

                                FileWriter writer = new FileWriter(testfile);

                                br = new BufferedReader(new FileReader(testFile));

                                IteratingSMILESReader iteratingSMILESReader = new IteratingSMILESReader(br, DefaultChemObjectBuilder.getInstance());


                                if (iteratingSMILESReader.hasNext()) {

                                    IAtomContainer molecule = (IAtomContainer) iteratingSMILESReader.next();

                                    int maxFingerprints = 1024;

                                    String[] descriptorValues_ = new String[maxFingerprints];


                                    for (int i = 0; i < maxFingerprints; ++i) {

                                        descriptorValues_[i] = "";

                                    }


                                    BitSet fingerprint = new Fingerprinter().getFingerprint(molecule);

                                    writer.append(testFile.getName());

                                    Test_Ready.setText("Processing : " + testFile.getName() + " ...");


                                    for (int i = 0; i < maxFingerprints; ++i) {

                                        if (fingerprint.get(i) == true) {

                                            writer.append("1");

                                            writer.flush();

                                        } else if (fingerprint.get(i) == false) {

                                            writer.append("0");

                                            writer.flush();

                                        }

                                        // progressBar.setValue(i);

                                        // progressBar.repaint();


                                    }


                                }

                                writer.close();

                                Test_Ready.setText("Process Completed!");

                                JOptionPane.showMessageDialog(null, "\t\tProcess Completed!", "Completed",

                                        JOptionPane.INFORMATION_MESSAGE);

                                openTestFile.setEnabled(true);

                                saveTestFile.setEnabled(true);

                                refreshTab.setVisible(true);

                                about.setVisible(true);

                            }


                        } catch (Exception ex) {

                            ex.printStackTrace();

                        }

                    }

                }

            }

        }

    }

I had create a stop button to and try to stop the program once I click on it. But how can I implement the Stop_Convert actionlistener to my convert actionlistener ?

#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
You're declaring methods in methods (run inside actionperformed), that can't possibly compile.

Something along these lines should do what you want.
Convert_Test_File  convertTestFile = new Convert_Test_File();

Stop_Convert stopConvert = new Stop_Convert(convertTestFile);


buttonStart.addActionListener(convertTestFile();

buttonStop.addActionListener(stopConvert);


 public class Stop_Convert implements ActionListener {

        private Convert_Test_File  convertTestFile;


        public Stop_Convert(Convert_Test_File  convertTestFile){

                 this.convertTestFile = convertTestFile;

        }


        public void actionPerformed(ActionEvent e) {            

                convertTestFile.stop();            

        }

}



public class Convert_Test_File implements ActionListener {


        private boolean continue = true;


        public void stop(){

            continue = false;

        }


        public void actionPerformed(ActionEvent e) {

                        refreshTab.setVisible(false);

                        about.setVisible(false);

                        try {

                            if (Test_File_Directory.getText().trim().length() == 0) {

                                JOptionPane.showMessageDialog(null, "\t\tTest molecule file not selected!", "Missing File",

                                        JOptionPane.WARNING_MESSAGE);

                            } else if (Save_Test_Directory.getText().trim().length() == 0) {

                                JOptionPane.showMessageDialog(null, "\t\tPlease select a directory to save the Fingerprints!", "Missing Directory",

                                        JOptionPane.WARNING_MESSAGE);

                            } else if (Test_File_Directory.getText().trim().length() == 0 && Save_Test_Directory.getText().trim().length() == 0) {

                                JOptionPane.showMessageDialog(null, "\t\tFile not selected!", "Missing File",

                                        JOptionPane.WARNING_MESSAGE);

                            } else if (testfile.length() != 0) {

                                openTestFile.setEnabled(false);

                                saveTestFile.setEnabled(false);

                                FileWriter writer = new FileWriter(testfile);

                                br = new BufferedReader(new FileReader(testFile));

                                IteratingSMILESReader iteratingSMILESReader = new IteratingSMILESReader(br, DefaultChemObjectBuilder.getInstance());


                                if (iteratingSMILESReader.hasNext()) {

                                    IAtomContainer molecule = (IAtomContainer) iteratingSMILESReader.next();

                                    int maxFingerprints = 1024;

                                    String[] descriptorValues_ = new String[maxFingerprints];


                                    for (int i = 0; i < maxFingerprints; ++i) {

                                        descriptorValues_[i] = "";

                                    }


                                    BitSet fingerprint = new Fingerprinter().getFingerprint(molecule);

                                    writer.append(testFile.getName());

                                    Test_Ready.setText("Processing : " + testFile.getName() + " ...");


                                    for (int i = 0; i < maxFingerprints && continue; ++i) {

                                        if (fingerprint.get(i) == true) {

                                            writer.append("1");

                                            writer.flush();

                                        } else if (fingerprint.get(i) == false) {

                                            writer.append("0");

                                            writer.flush();

                                        }

                                        // progressBar.setValue(i);

                                        // progressBar.repaint();


                                    }


                                }

                                writer.close();

                                if(continue){

                                   Test_Ready.setText("Process Completed!");

                                   JOptionPane.showMessageDialog(null, "\t\tProcess Completed!", "Completed",

                                        JOptionPane.INFORMATION_MESSAGE);

                                } else {

                                      //other message here.

                                }

                                openTestFile.setEnabled(true);

                                saveTestFile.setEnabled(true);

                                refreshTab.setVisible(true);

                                about.setVisible(true);

                            }


                        } catch (Exception ex) {

                            ex.printStackTrace();

                        }

                    }

        }

    }



#3
xxxxjayxxx

xxxxjayxxx

    Programmer

  • Members
  • PipPipPipPip
  • 123 posts
public class Convert_Test_File implements ActionListener {


        private boolean continue = true;


        public void stop(){

            continue = false;

        }


        public void actionPerformed(ActionEvent e) {

                        refreshTab.setVisible(false);

                        about.setVisible(false);

                        try {

                            if (Test_File_Directory.getText().trim().length() == 0) {

                                JOptionPane.showMessageDialog(null, "\t\tTest molecule file not selected!", "Missing File",

                                        JOptionPane.WARNING_MESSAGE);

                            } else if (Save_Test_Directory.getText().trim().length() == 0) {

                                JOptionPane.showMessageDialog(null, "\t\tPlease select a directory to save the Fingerprints!", "Missing Directory",

                                        JOptionPane.WARNING_MESSAGE);

                            } else if (Test_File_Directory.getText().trim().length() == 0 && Save_Test_Directory.getText().trim().length() == 0) {

                                JOptionPane.showMessageDialog(null, "\t\tFile not selected!", "Missing File",

                                        JOptionPane.WARNING_MESSAGE);

                            } else if (testfile.length() != 0) {

                                openTestFile.setEnabled(false);

                                saveTestFile.setEnabled(false);

                                FileWriter writer = new FileWriter(testfile);

                                br = new BufferedReader(new FileReader(testFile));

                                IteratingSMILESReader iteratingSMILESReader = new IteratingSMILESReader(br, DefaultChemObjectBuilder.getInstance());


                                if (iteratingSMILESReader.hasNext()) {

                                    IAtomContainer molecule = (IAtomContainer) iteratingSMILESReader.next();

                                    int maxFingerprints = 1024;

                                    String[] descriptorValues_ = new String[maxFingerprints];


                                    for (int i = 0; i < maxFingerprints; ++i) {

                                        descriptorValues_[i] = "";

                                    }


                                    BitSet fingerprint = new Fingerprinter().getFingerprint(molecule);

                                    writer.append(testFile.getName());

                                    Test_Ready.setText("Processing : " + testFile.getName() + " ...");


                                    for (int i = 0; i < maxFingerprints && continue; ++i) {

                                        if (fingerprint.get(i) == true) {

                                            writer.append("1");

                                            writer.flush();

                                        } else if (fingerprint.get(i) == false) {

                                            writer.append("0");

                                            writer.flush();

                                        }

                                        // progressBar.setValue(i);

                                        // progressBar.repaint();


                                    }


                                }

                                writer.close();

                                if(continue){

                                   Test_Ready.setText("Process Completed!");

                                   JOptionPane.showMessageDialog(null, "\t\tProcess Completed!", "Completed",

                                        JOptionPane.INFORMATION_MESSAGE);

                                } else {

                                      //other message here.

                                }

                                openTestFile.setEnabled(true);

                                saveTestFile.setEnabled(true);

                                refreshTab.setVisible(true);

                                about.setVisible(true);

                            }


                        } catch (Exception ex) {

                            ex.printStackTrace();

                        }

                    }

        }

    }
I had make use of this code and add it to mine there some error for
private boolean continue = true;

which is <identifier is missing> error.
and
continue = false;
which is continue outside the loop error.

#4
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
The only thing I currently spot is 1too many "}".

#5
xxxxjayxxx

xxxxjayxxx

    Programmer

  • Members
  • PipPipPipPip
  • 123 posts
I had try to remove the extra { but there's still error...
Posted Image
Posted Image
Posted Image
Posted Image

here the code i had remove the {
stopScreeningBase = new JButton("STOP");

        stopScreeningBase.setFont(font);

        stopScreeningBase.setBounds(519, 255, 70, 30);

        panel2.add(stopScreeningBase);

        stopScreeningBase.addActionListener(new Stop_Convert());
public class Convert_Test_File implements ActionListener {

       private boolean continue == true;


        public void stop(){

            continue = false;

        }


        public void actionPerformed(ActionEvent e, int i) {


                    

                        refreshTab.setVisible(false);

                        about.setVisible(false);

                        try {

                            if (Test_File_Directory.getText().trim().length() == 0) {

                                JOptionPane.showMessageDialog(null, "\t\tTest molecule file not selected!", "Missing File",

                                        JOptionPane.WARNING_MESSAGE);

                            } else if (Save_Test_Directory.getText().trim().length() == 0) {

                                JOptionPane.showMessageDialog(null, "\t\tPlease select a directory to save the Fingerprints!", "Missing Directory",

                                        JOptionPane.WARNING_MESSAGE);

                            } else if (Test_File_Directory.getText().trim().length() == 0 && Save_Test_Directory.getText().trim().length() == 0) {

                                JOptionPane.showMessageDialog(null, "\t\tFile not selected!", "Missing File",

                                        JOptionPane.WARNING_MESSAGE);

                            } else if (testfile.length() != 0) {

                                openTestFile.setEnabled(false);

                                saveTestFile.setEnabled(false);

                                FileWriter writer = new FileWriter(testfile);

                                br = new BufferedReader(new FileReader(testFile));

                                IteratingSMILESReader iteratingSMILESReader = new IteratingSMILESReader(br, DefaultChemObjectBuilder.getInstance());


                                if (iteratingSMILESReader.hasNext()) {

                                    IAtomContainer molecule = (IAtomContainer) iteratingSMILESReader.next();

                                    int maxFingerprints = 1024;

                                    String[] descriptorValues_ = new String[maxFingerprints];


                                    for ( i = 0; i < maxFingerprints; ++i) {

                                        descriptorValues_[i] = "";

                                    }


                                    BitSet fingerprint = new Fingerprinter().getFingerprint(molecule);

                                    writer.append(testFile.getName());

                                    Test_Ready.setText("Processing : " + testFile.getName() + " ...");


                                    for ( i = 0; i < maxFingerprints && continue; ++i) {

                                        if (fingerprint.get(i) == true) {

                                            writer.append("1");

                                            writer.flush();

                                        } else if (fingerprint.get(i) == false) {

                                            writer.append("0");

                                            writer.flush();

                                        }

                                        

                                    }


                                }

                                writer.close();

                                if(continue){

                                   Test_Ready.setText("Process Completed!");

                                   JOptionPane.showMessageDialog(null, "\t\tProcess Completed!", "Completed",

                                        JOptionPane.INFORMATION_MESSAGE);

                                }

                                openTestFile.setEnabled(true);

                                saveTestFile.setEnabled(true);

                                refreshTab.setVisible(true);

                                about.setVisible(true);

                            }


                        } catch (Exception ex) {

                            ex.printStackTrace();

                        }

                    }

                }


#6
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
ow xD forgot that continue is a keyword in Java and can't be used as variable name. Just name it something different.

#7
xxxxjayxxx

xxxxjayxxx

    Programmer

  • Members
  • PipPipPipPip
  • 123 posts
so the part which will stop the actionlistioner is :
public class Convert_Test_File implements ActionListener {


        [COLOR="lime"][COLOR="#9932cc"]private boolean continue = true;


        public void stop(){

            continue = false;

        }[/COLOR][/COLOR]


        public void actionPerformed(ActionEvent e) {

                        refreshTab.setVisible(false);

                        about.setVisible(false);

                        try {

                            if (Test_File_Directory.getText().trim().length() == 0) {

                                JOptionPane.showMessageDialog(null, "\t\tTest molecule file not selected!", "Missing File",

                                        JOptionPane.WARNING_MESSAGE);

                            } else if (Save_Test_Directory.getText().trim().length() == 0) {

                                JOptionPane.showMessageDialog(null, "\t\tPlease select a directory to save the Fingerprints!", "Missing Directory",

                                        JOptionPane.WARNING_MESSAGE);

                            } else if (Test_File_Directory.getText().trim().length() == 0 && Save_Test_Directory.getText().trim().length() == 0) {

                                JOptionPane.showMessageDialog(null, "\t\tFile not selected!", "Missing File",

                                        JOptionPane.WARNING_MESSAGE);

                            } else if (testfile.length() != 0) {

                                openTestFile.setEnabled(false);

                                saveTestFile.setEnabled(false);

                                FileWriter writer = new FileWriter(testfile);

                                br = new BufferedReader(new FileReader(testFile));

                                IteratingSMILESReader iteratingSMILESReader = new IteratingSMILESReader(br, DefaultChemObjectBuilder.getInstance());


                                if (iteratingSMILESReader.hasNext()) {

                                    IAtomContainer molecule = (IAtomContainer) iteratingSMILESReader.next();

                                    int maxFingerprints = 1024;

                                    String[] descriptorValues_ = new String[maxFingerprints];


                                    for (int i = 0; i < maxFingerprints; ++i) {

                                        descriptorValues_[i] = "";

                                    }


                                    BitSet fingerprint = new Fingerprinter().getFingerprint(molecule);

                                    writer.append(testFile.getName());

                                    Test_Ready.setText("Processing : " + testFile.getName() + " ...");


                                    for (int i = 0; i < maxFingerprints [COLOR="#9932cc"]&& continue[/COLOR]; ++i) {

                                        if (fingerprint.get(i) == true) {

                                            writer.append("1");

                                            writer.flush();

                                        } else if (fingerprint.get(i) == false) {

                                            writer.append("0");

                                            writer.flush();

                                        }

                                        // progressBar.setValue(i);

                                        // progressBar.repaint();


                                    }


                                }

                                writer.close();

                               [COLOR="#9932cc"] if(continue){

                                   Test_Ready.setText("Process Completed!");

                                   JOptionPane.showMessageDialog(null, "\t\tProcess Completed!", "Completed",

                                        JOptionPane.INFORMATION_MESSAGE);

                                } else {

                                      //other message here.

                                }[/COLOR]

                                openTestFile.setEnabled(true);

                                saveTestFile.setEnabled(true);

                                refreshTab.setVisible(true);

                                about.setVisible(true);

                            }


                        } catch (Exception ex) {

                            ex.printStackTrace();

                        }

                    }

        }

    }


#8
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Well it will stop the loop, as that's propably the thing that takes so long.
(You may have to put the code from the actionlistener in a separate thread new Thread(new Runnable(){public void run(){ code here } }).start();)

#9
xxxxjayxxx

xxxxjayxxx

    Programmer

  • Members
  • PipPipPipPip
  • 123 posts
something like :
code for convert:
public class Convert_Screening_File implements ActionListener {


        private boolean continue = true;


        public void stop(){

            continue = false;

        }


        public void actionPerformed(ActionEvent e) {

            (new Thread(new Runnable() {

                refreshTab.setVisible(false);

                        about.setVisible(false);

                        try {

                            if (Test_File_Directory.getText().trim().length() == 0) {

                                JOptionPane.showMessageDialog(null, "\t\tTest molecule file not selected!", "Missing File",

                                        JOptionPane.WARNING_MESSAGE);

                            } else if (Save_Test_Directory.getText().trim().length() == 0) {

                                JOptionPane.showMessageDialog(null, "\t\tPlease select a directory to save the Fingerprints!", "Missing Directory",

                                        JOptionPane.WARNING_MESSAGE);

                            } else if (Test_File_Directory.getText().trim().length() == 0 && Save_Test_Directory.getText().trim().length() == 0) {

                                JOptionPane.showMessageDialog(null, "\t\tFile not selected!", "Missing File",

                                        JOptionPane.WARNING_MESSAGE);

                            } else if (testfile.length() != 0) {

                                openTestFile.setEnabled(false);

                                saveTestFile.setEnabled(false);

                                FileWriter writer = new FileWriter(testfile);

                                br = new BufferedReader(new FileReader(testFile));

                                IteratingSMILESReader iteratingSMILESReader = new IteratingSMILESReader(br, DefaultChemObjectBuilder.getInstance());


                                if (iteratingSMILESReader.hasNext()) {

                                    IAtomContainer molecule = (IAtomContainer) iteratingSMILESReader.next();

                                    int maxFingerprints = 1024;

                                    String[] descriptorValues_ = new String[maxFingerprints];


                                    for (int i = 0; i < maxFingerprints; ++i) {

                                        descriptorValues_[i] = "";

                                    }


                                    BitSet fingerprint = new Fingerprinter().getFingerprint(molecule);

                                    writer.append(testFile.getName());

                                    Test_Ready.setText("Processing : " + testFile.getName() + " ...");


                                    for (int i = 0; i < maxFingerprints && continue; ++i) {

                                        if (fingerprint.get(i) == true) {

                                            writer.append("1");

                                            writer.flush();

                                        } else if (fingerprint.get(i) == false) {

                                            writer.append("0");

                                            writer.flush();

                                        }

                                        // progressBar.setValue(i);

                                        // progressBar.repaint();


                                    }


                                }

                                writer.close();

                                if(continue){

                                   Test_Ready.setText("Process Completed!");

                                   JOptionPane.showMessageDialog(null, "\t\tProcess Completed!", "Completed",

                                        JOptionPane.INFORMATION_MESSAGE);

                                } else {

                                      //other message here.

                                }

                                openTestFile.setEnabled(true);

                                saveTestFile.setEnabled(true);

                                refreshTab.setVisible(true);

                                about.setVisible(true);

                            }


                        } catch (Exception ex) {

                            ex.printStackTrace();

                        }


                }

            })).start();

        }

    }

code for creating the stop button:
stopScreeningBase = new JButton("STOP");

        stopScreeningBase.setFont(font);

        stopScreeningBase.setBounds(519, 255, 70, 30);

        panel2.add(stopScreeningBase);

        stopScreeningBase.addActionListener(new Stop_Convert());


code for stop action:
public class Stop_Convert implements ActionListener {

        private Convert_Test_File  convertTestFile;


        public Stop_Convert(Convert_Test_File  convertTestFile){

                 this.convertTestFile = convertTestFile;

        }


        private Stop_Convert() {

            this.convertTestFile = convertTestFile;

        }


        public void actionPerformed(ActionEvent e) {

                convertTestFile.stop();

        }

    }


#10
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
When you create your actionListener it needs the convert_test_file actionlistener, like:

Convert_Test_File  convertTestFile = new Convert_Test_File();

Stop_Convert stopConvert = new Stop_Convert(convertTestFile);


buttonStart.addActionListener(convertTestFile();

buttonStop.addActionListener(stopConvert);



#11
xxxxjayxxx

xxxxjayxxx

    Programmer

  • Members
  • PipPipPipPip
  • 123 posts
ok I manage to stop the program but after I stop the program, when I click on the convert button it doesnt run ..

#12
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
set
continue = true;
as first line of its actionperformed.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users