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 ?
17 Replies - 1206 Views - Last Post: 08 May 2011 - 07:23 PM
Replies To: How to solve the hang problem ?
#2
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.
#3
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
#4
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.
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:
You should also check out this link for some more information on Swing Workers I could be forgetting about.
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.
#5
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 

It dont allow me to do the extends

It dont allow me to do the extends
#7
Re: How to solve the hang problem ?
Posted 05 May 2011 - 06:03 AM
xxxjayxxx, 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 

It dont allow me to do the extends

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 ?
#8
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.

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 ?

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 ?
#9
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.
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.
#10
Re: How to solve the hang problem ?
Posted 05 May 2011 - 07:10 AM
macosxnerd101, 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.
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
#11
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.
#12
Re: How to solve the hang problem ?
Posted 05 May 2011 - 07:34 AM
The tutorials are in the Java Tutorials Section on DIC.
#13
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.
anyone can help ?
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 ?
#14
Re: How to solve the hang problem ?
Posted 05 May 2011 - 12:24 PM
exiles.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.
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:
You should also check out this link for some more information on Swing Workers I could be forgetting about.
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.
#15
Re: How to solve the hang problem ?
Posted 05 May 2011 - 07:19 PM
Isit something like this ?
But in the end still hangs
Is there anything I had did wrong ?
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
|
|

New Topic/Question
Reply




MultiQuote








|