Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Question about thread

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Question about thread

    Hi guys,
    I am new in java programming ,
    I wrote this pregame which is a simple GUI program with 2 buttons one to get the full date , and the other to get the time only .
    I used Thread in this program in order to make the time update every 1 second.

    here is the code :

    import java.util.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
    public class GUI extends JFrame {
    	private JButton B1;
    	private JButton B2;
    	private JLabel L1;
    	private JPanel P1;
    	private JPanel P2; 
    	private MYThread TH;
    	String S ="";
     
    	public GUI (){
    		super ("Time");
    		TH = new MYThread();
    		B1 = new JButton("Get full Time");
    		B2 = new JButton("Get Time");
    		L1 = new JLabel();
    		P1 = new JPanel();
    		P2 = new JPanel();
    		P1.add(B1);
    		P1.add(B2);
    		P2.add(L1);
    		add(P1 , BorderLayout.SOUTH);
    		add(P2 , BorderLayout.CENTER);
    		B1.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent arg0) {
    				try {
    				 S = "1";
    				 TH.start();
    				}
    				catch (IllegalThreadStateException e){
     
    				 }	
    			}
    		});
    		B2.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent arg0) {
    				 S = "2";
    				 try {
    				 TH.start();
    				 }
    				 catch (IllegalThreadStateException e){				 
    				 }
    			}
    		});
    	}
    	class MYThread extends Thread{
    		String T ;
    		public void run() {
    			super.run();
     
    			while (true){
    				if(S.equals("1")){
    					Calendar C = new GregorianCalendar();
    					T = C.getTime() + "";
     
    					L1.setText(T);
    					try {
    						MYThread.sleep(1000);
    					}
    					catch (Exception e){					
    					}
     
    				}
    				if(S.equals("2")){
    					GregorianCalendar C = new GregorianCalendar();
    					L1.setText(C.get(Calendar.HOUR)+":"+C.get(Calendar.MINUTE)+":"+C.get(Calendar.SECOND));
    					try {
    						MYThread.sleep(1000);
    					}
    					catch (Exception e){					
    					}
    				}
    			}
    		}
    	}
    }

    My question is :
    Can I use " TH.start(); " more than once in the same class ??
    I used "TH.start() " in the Action Listener .

    Sorry for my English


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Question about thread

    What happened when you tried? Also, you shouldn't have empty catch clauses. You're probably ignoring error messages that could help you understand what's going on.

    You might want to use a Swing Timer instead of a thread. Updating the GUI on a thread other than the EDT is a big no-no.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    May 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Question about thread

    When I tried the program the first time it throw "java.lang.IllegalThreadStateException" ,
    so that I tried to put it in a try catch blocks but I didn't know what to write in the Catch blocks so I left them empty !
    the program works fine , but I don't know if this code is right or wrong !
    thank you KevinWorkman

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Question about thread

    Sounds like you found out that you can't call Thread.start() more than once, which the API tells you: Thread (Java Platform SE 6)

    By surrounding it by a try and a catch, you're simply ignoring the error. That's not the right thing to do.

    But you shouldn't be using multiple threads to interact with the GUI like this anyway. Use a Swing Timer instead.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    May 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Question about thread

    That what I wanted to know
    Thank you so much KevinWorkman for your help
    Last edited by Suzie_K; May 4th, 2012 at 01:47 PM.

Similar Threads

  1. What is this thread for exactly?
    By javapenguin in forum Computer Support
    Replies: 2
    Last Post: July 2nd, 2011, 10:37 AM
  2. Thread Question
    By _lithium_ in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 11th, 2011, 08:57 PM
  3. thread pool question
    By balexios in forum Threads
    Replies: 3
    Last Post: October 24th, 2010, 03:47 AM
  4. Thread to EDT
    By Asido in forum Threads
    Replies: 3
    Last Post: October 15th, 2010, 01:13 PM
  5. Thread Sleep, Timer, Button Question
    By tabutcher in forum Java Theory & Questions
    Replies: 1
    Last Post: May 1st, 2010, 02:54 AM