Hello,
I'm currently working on a project that executes queries to the database. This process takes a long time to be finished, so I would like the user to be able to use the JFrame while its processing. The solution I found is using Threads.
I never used it before and after some tutorials online I found a way to use it. The problem is, the JFrame is still freezing even when the query is executed within a thread. (I think)
Because the project is private I can't post the code here. I did however create an example code to make my (maybe mistake or problem) clear. The project has the follwing structure:
[Package]
[--> class] Main
[--> class] Form (extends JFrame)
[--> class] Timer (implements Runnable)
The way I coded this is as follows:
Main class:
package Package; import java.awt.Color; import javax.swing.ImageIcon; import javax.swing.JFrame; public class Main { public static void main(String[] args) { Form form = new Form(); form.setVisible(true); form.setSize(288, 435); form.setResizable(false); form.setLayout(null); form.getContentPane().setBackground(Color.black); form.setIconImage(new ImageIcon(form.getClass().getResource("http://www.java-forums.org/images/Marque-it.ico")).getImage()); form.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }
Form class:
package Package; import java.awt.Color; import java.awt.Dimension; import java.awt.Font; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.io.BufferedReader; import java.io.DataInputStream; import java.io.File; import java.io.FileInputStream; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; import javax.swing.*; public class Form extends JFrame implements MouseListener { private static final long serialVersionUID = 1L; private Thread[] treadList; public Form () { // Use a null layout setLayout(null); // Load buttons and images ect loadDesign(); // Create new threads for(int i = 0; i < treadList.length; i++) { treadList[i] = new treadList(new Timer(5000, this)); treadList[i].start(); } } }
Timer class:
package Package; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.net.MalformedURLException; import java.net.URL; //public class Timer implements Runnable { public class Timer implements Runnable { Toolkit toolkit; int seconds; private Form frm; public Timer(int seconds, Form frm) { toolkit = Toolkit.getDefaultToolkit(); this.seconds = seconds; this.frm = frm; } javax.swing.Timer timer = new javax.swing.Timer(seconds, new ActionListener() { public void actionPerformed(ActionEvent e) { // Do query what takes long time } }); @Override public void run() { // TODO Auto-generated method stub System.out.println(Thread.currentThread()); timer.start(); } }
I don't know what is wrong with this code? Although I need to say I don't have much experience with programming in Java. I also tried to use a SwingWorker but it's the same result.
Can someone help me?
Kind regards,
Mike Feonx