Results 1 to 3 of 3
  1. #1
    Feonx is offline Member
    Join Date
    Oct 2011
    Posts
    2
    Rep Power
    0

    Default Threading not functioning

    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:
    Java Code:
    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("images/Marque-it.ico")).getImage());
            	form.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	}
    }
    Form class:
    Java Code:
    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:
    Java Code:
    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

  2. #2
    Feonx is offline Member
    Join Date
    Oct 2011
    Posts
    2
    Rep Power
    0

    Default Re: Threading not functioning

    Realy?, can nobody give me some advice of things to try? or show me a (maybe beter) example of how to do this?

    Please can someone help me out?

    Kind reguards,

    Mike Feonx

  3. #3
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: Threading not functioning

    Cross posted at Threading not functioning Please read the links provided in that post, and please be forthright when posting the same question in different forums.

Similar Threads

  1. JDialog Not Functioning Properly
    By StevenTNorris in forum AWT / Swing
    Replies: 5
    Last Post: 10-26-2011, 06:24 PM
  2. Creating Jar Files with functioning input files
    By appleLove in forum NetBeans
    Replies: 1
    Last Post: 04-10-2011, 10:37 PM
  3. Replies: 6
    Last Post: 12-15-2009, 02:33 AM
  4. Word Wrap not functioning correctly?
    By zerkz in forum New To Java
    Replies: 2
    Last Post: 10-08-2009, 06:41 PM
  5. Applet not functioning
    By Shadaw in forum Java Applets
    Replies: 2
    Last Post: 12-18-2008, 05:15 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •