How to solve the hang problem ?

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 1637 Views - Last Post: 08 May 2011 - 07:23 PM Rate Topic: -----

#1 xxxjayxxx  Icon User is offline

  • D.I.C Head

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

How to solve the hang problem ?

Posted 04 May 2011 - 08:43 PM

When I use my JFrame program to run some files it will eventually get hang. Mean even I click the "X" panel (exit the program) on my JFrame it will not detect anything. However it will return normal when the program finish reading the files i had selected. How can I solve this problem ?
Is This A Good Question/Topic? 0
  • +

Replies To: How to solve the hang problem ?

#2 exiles.prx  Icon User is offline

  • D.I.C Head

Reputation: 65
  • View blog
  • Posts: 239
  • Joined: 22-November 10

Re: How to solve the hang problem ?

Posted 04 May 2011 - 08:46 PM

Use a Swing Worker to read the files. This should prevent the GUI from hanging.
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 to solve the hang problem ?

Posted 04 May 2011 - 08:51 PM

Sorry I'm new to Java .. To use SwingWorker.. Isit something like this ?

public class Main extends SwingWorker{

This post has been edited by xxxjayxxx: 04 May 2011 - 08:52 PM

Was This Post Helpful? 0
  • +
  • -

#4 exiles.prx  Icon User is offline

  • D.I.C Head

Reputation: 65
  • View blog
  • Posts: 239
  • Joined: 22-November 10

Re: How to solve the hang problem ?

Posted 04 May 2011 - 09:06 PM

Well, you wouldn't want your main class to extend SwingWorker. What you want is a private inner class extending SwingWoker. You could probably even get away with using the SwingUtilities.invokeLater() method.

private MyWorker extends SwingWorker
{
     public Object doInBackground()
     {
           //code to read files
           return null;     //or return useful
     }
}


To use the swing worker you would create a new instance every time you need to utilize the worker (workers can not be reused) then call the workers execute method. ex:
MyWorker worker = new MyWorker();
worker.execute();



You should also check out this link for some more information on Swing Workers I could be forgetting about.
Was This Post Helpful? 1
  • +
  • -

#5 xxxjayxxx  Icon User is offline

  • D.I.C Head

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

Re: How to solve the hang problem ?

Posted 04 May 2011 - 09:17 PM

Sorry but I using the Jbutton where I click on the Jbutton to let me code in there :(
Posted Image
It dont allow me to do the extends :(
Was This Post Helpful? 0
  • +
  • -

#6 xxxjayxxx  Icon User is offline

  • D.I.C Head

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

Re: How to solve the hang problem ?

Posted 04 May 2011 - 09:41 PM

Help ... Anyone :(
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

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

Reputation: 8334
  • View blog
  • Posts: 31,858
  • Joined: 06-March 08

Re: How to solve the hang problem ?

Posted 05 May 2011 - 06:03 AM

View Postxxxjayxxx, on 05 May 2011 - 12:17 AM, said:

Sorry but I using the Jbutton where I click on the Jbutton to let me code in there :(
Posted Image
It dont allow me to do the extends :(

We'll neeed more details. Who is calling that method ? Under which context ?
Why not simply name it actionPerformed() to overload the original one ?
Was This Post Helpful? 0
  • +
  • -

#8 xxxjayxxx  Icon User is offline

  • D.I.C Head

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

Re: How to solve the hang problem ?

Posted 05 May 2011 - 06:27 AM

it is something like this... I am using the drag and drop JFrame. Which mean that if i need a button I just need to drag the button from the palette and drop it on the JFrame.

Posted Image
Well the calling method is from this button. Sorry what do you mean by simply naming it as actionperformed()? Does that mean that I need to create a button out using hard code method ?
Was This Post Helpful? 0
  • +
  • -

#9 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10596
  • View blog
  • Posts: 39,258
  • Joined: 27-December 08

Re: How to solve the hang problem ?

Posted 05 May 2011 - 06:45 AM

Dogstopper has a good tutorial on SwingWorker.

Also, don't use the drag-and-drop GUI Builder. It produces horrid, unmaintainable code that humans (including us) can neither read nor maintain. Swing is so straight-forward that you should be writing all your GUI code by hand. We have a bunch of good Swing tutorials on DIC if you want to check them out.
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 to solve the hang problem ?

Posted 05 May 2011 - 07:10 AM

View Postmacosxnerd101, on 05 May 2011 - 06:45 AM, said:

Dogstopper has a good tutorial on SwingWorker.

Also, don't use the drag-and-drop GUI Builder. It produces horrid, unmaintainable code that humans (including us) can neither read nor maintain. Swing is so straight-forward that you should be writing all your GUI code by hand. We have a bunch of good Swing tutorials on DIC if you want to check them out.


sorry .. i had try swing but I had quite a hard time as I can figure out how to put the component at the desire area on the JFrame i want... hmm ... thx ... the tutorial is under which category ? I can't find them :(

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

Was This Post Helpful? 0
  • +
  • -

#11 v0rtex  Icon User is offline

  • Caffeine: db "Never Enough!"
  • member icon

Reputation: 223
  • View blog
  • Posts: 773
  • Joined: 02-June 10

Re: How to solve the hang problem ?

Posted 05 May 2011 - 07:21 AM

Use the drag-and-drop GUI Builder to plan your application's appearance and then code the application yourself to avoid the horrific code that the GUI builder produces.
Was This Post Helpful? 0
  • +
  • -

#12 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10596
  • View blog
  • Posts: 39,258
  • Joined: 27-December 08

Re: How to solve the hang problem ?

Posted 05 May 2011 - 07:34 AM

The tutorials are in the Java Tutorials Section on DIC.
Was This Post Helpful? 0
  • +
  • -

#13 xxxjayxxx  Icon User is offline

  • D.I.C Head

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

Re: How to solve the hang problem ?

Posted 05 May 2011 - 10:34 AM

After hours of reading tutorial I had created a gui using swing ... But however I still don't understand the SwingWorker part... I still don't know how to implement SwingWorker to my code.
public void actionPerformed(ActionEvent e)  {
            try {
                FileWriter writer = new FileWriter(sfiles);
                for (File moleculeFile : Files.listFiles()) {
                    BitSet fingerprint = convertToFingerprint(moleculeFile); //call function
                    writer.append(moleculeFile.getName());
                    // Screen_Process.setText("Proceesing " + moleculeFile.getName());
                    writer.append(",");
                    {
                        for (int i = 0; i < fingerprint.size(); i++) {
                            if (i > 0) {
                                writer.append(",");
                                writer.flush();
                            }

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

                    }
                }
                writer.close();

            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }

anyone can help ?
Was This Post Helpful? 0
  • +
  • -

#14 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10596
  • View blog
  • Posts: 39,258
  • Joined: 27-December 08

Re: How to solve the hang problem ?

Posted 05 May 2011 - 12:24 PM

View Postexiles.prx, on 05 May 2011 - 12:06 AM, said:

Well, you wouldn't want your main class to extend SwingWorker. What you want is a private inner class extending SwingWoker. You could probably even get away with using the SwingUtilities.invokeLater() method.

private MyWorker extends SwingWorker
{
     public Object doInBackground()
     {
           //code to read files
           return null;     //or return useful
     }
}


To use the swing worker you would create a new instance every time you need to utilize the worker (workers can not be reused) then call the workers execute method. ex:
MyWorker worker = new MyWorker();
worker.execute();



You should also check out this link for some more information on Swing Workers I could be forgetting about.

You have to extend SwingWorker as exiles.prx demonstrated.
Was This Post Helpful? 0
  • +
  • -

#15 xxxjayxxx  Icon User is offline

  • D.I.C Head

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

Re: How to solve the hang problem ?

Posted 05 May 2011 - 07:19 PM

Isit something like this ?
public class Convert_Test_File implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            MyWorker workerParty = new MyWorker();
            try {
                workerParty.doInBackground();
            } catch (Exception ex) {
                Logger.getLogger(FYP__ZZC.class.getName()).log(Level.SEVERE, null, ex);
            }
    }
    }
    public class MyWorker extends SwingWorker{
         @Override
        protected Object doInBackground() throws Exception {
            try {
                FileWriter writer = new FileWriter(screeningbase);
                for (File moleculeFile : screeningBase.listFiles()) {
                    BitSet fingerprint = convertToFingerprint(moleculeFile); //call function
                    writer.append(moleculeFile.getName());
                    // Screen_Process.setText("Proceesing " + moleculeFile.getName());
                    writer.append(",");
                    {
                        for (int i = 0; i < fingerprint.size(); i++) {
                            if (i > 0) {
                                writer.append(",");
                                writer.flush();
                            }

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

                    }
                }
                writer.close();

            } catch (Exception ex) {
                ex.printStackTrace();
            }
            return null;
        }
        }

But in the end still hangs :( Is there anything I had did wrong ?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2