Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-28-2011, 03:45 AM   PM User | #1
shoppermom96
New to the CF scene

 
Join Date: Mar 2011
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
shoppermom96 is an unknown quantity at this point
Help!

I need help with this creating a UI class which implements a JFrame. You can implement any type of layout for the components. The UI will implement a counter that will start at zero and will be implemented every second by a second thread that is spawned by the UI application. The counter should be shown as a label on the UI application. Create a button that will allow the UI thread to pause the second thread if it is running and start the thread if it has been paused. The button will act as a toggle. This design shows how you can create background tasks that will not interfere with the UI thread. The UI will maintain control over the thread that it spawns.

After hours and hours of confusing myself beyond belief this is what I have:

its horrible and pathetic I know please help

Code:
import java.awt.*;
import java.applet.*;
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;


public class MyGui {
private static int counter = 0;
public static void main(String[] args) {
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask(){
public void run() {
counter++;
System.out.println(counter);
}
}, new Date(), 1000);
}


//public class TimePrinter extends MyGui {

JLabel l1,l2;
JTextField tf1,tf2;
JButton b1,b2;
//public static void main(String[] args) {
	public void MyThread(){
		final JFrame jf=new JFrame();
		jf.setTitle("My frame");
		jf.setSize(500,300);

		final JLabel l1=new JLabel("Counter");
		final JLabel l2=new JLabel("label 2");

		final JTextField tf1=new JTextField(10);
		final JTextField tf2=new JTextField(5);

		final JButton b1=new JButton("Please");
		final JButton b2=new JButton("Resume");

		tf1.setText("i am a text field");

		tf2.setText("i am a text field and can not be changed");
		tf2.setEditable(false);

		final Container pane=jf.getContentPane();
		pane.setLayout(new GridLayout(3,2) );

		pane.add(l1);
		pane.add(tf1);
		pane.add(l2);
		pane.add(tf2);
		pane.add(b1);
		pane.add(b2);

		jf.setDefaultCloseOperation(jf.EXIT_ON_CLOSE);
		jf.setVisible(true);

		}

		  public void init(){
		     setLayout(new BorderLayout());
		     TextField clockField = new TextField();
		     add(clockField,"Center");
		     // Create the thread.
		     Thread clockThread = new Thread();
		     clockThread.start();
		  }
		 
		  private void add(TextField clockField, String string) {
			// TODO Auto-generated method stub
			
		}

		private void setLayout(BorderLayout borderLayout) {
			// TODO Auto-generated method stub
			
// TODO, add your application code
//MyThread thread = new MyThread();
//thread.start();{

//while (true) {
    // Do work

    // Pause the thread
  //  synchronized (thread) {
    //    thread.pleaseWait = true;
   // }

    // Do work

    // Resume the thread
  //  synchronized (thread) {
       // thread.pleaseWait = false;
      //  thread.notify();
    }

    // Do work
}

class MyThread extends Thread {
    boolean pleaseWait = false;

    // This method is called when the thread runs
    public void Go() {
        while (true) {
            // Do work

            // Check if should wait
            synchronized (this) {
                while (pleaseWait) {
                    try {
                        wait();
                    } catch (Exception e) {
                    }
                }
            }

            // Do work
        }
    }{
}



//new MyGUI();


public void MyThread(){
final JFrame jf=new JFrame();
jf.setTitle("My frame");
jf.setSize(500,300);

final JLabel l1=new JLabel("Counter");
final JLabel l2=new JLabel("label 2");

final JTextField tf1=new JTextField(10);
final JTextField tf2=new JTextField(5);

final JButton b1=new JButton("Pause");
final JButton b2=new JButton("Resume");

tf1.setText("i am a text field");

tf2.setText("i am a text field and can not be changed");
tf2.setEditable(false);

final Container pane=jf.getContentPane();
pane.setLayout(new GridLayout(3,2) );

pane.add(l1);
pane.add(tf1);
pane.add(l2);
pane.add(tf2);
pane.add(b1);
pane.add(b2);

jf.setDefaultCloseOperation(jf.EXIT_ON_CLOSE);
jf.setVisible(true);

}

  public void init(){
     setLayout(new BorderLayout());
     TextField clockField = new TextField();
     add(clockField,"Center");
     // Create the thread.
     Thread clockThread = new Thread();
     clockThread.start();
  }
 
  private void add(TextField clockField, String string) {
	// TODO Auto-generated method stub
	
}

private void setLayout(BorderLayout borderLayout) {
	// TODO Auto-generated method stub
	
}

//public void stop(){
  

  public void destroy(){
     boolean running = false;
     Object clockThread = null;
  }

  public void run(){

     boolean running;
	while (running == true)   {
        Object cal = Calendar.getInstance();
        String time = ((Calendar) cal).get(Calendar.MINUTE)+" : "+((Calendar) cal).get(Calendar.SECOND)+" : "+((Calendar) cal).get(Calendar.MILLISECOND);
        AbstractButton clockField;
		clockField.setText(time);
        try   {
            Thread clockThread;
		// Wait 500milliseconds before continuing
           clockThread.sleep(100);
        }
        catch (InterruptedException e)   {
           System.out.println(e);
        }
     }
  }

{;



// Create and start the thread
MyThread thread = new MyThread();
thread.start();

while (true) {
    // Do work

    // Pause the thread
    synchronized (thread) {
        thread.pleaseWait = true;
    }

    // Do work

    // Resume the thread
    synchronized (thread) {
        thread.pleaseWait = false;
        thread.notify();
    }

    // Do work
}

class MyThread extends MyGui {
    boolean pleaseWait = false;

    // This method is called when the thread runs
    public void run() {
        while (true) {
            // Do work

            // Check if should wait
            synchronized (this) {
                while (pleaseWait) {
                    try {
                        wait();
                    } catch (Exception e) {
                    }
                }
            }

            // Do work
        }
    }
}}}

Last edited by WA; 03-28-2011 at 09:03 AM..
shoppermom96 is offline   Reply With Quote
Old 03-28-2011, 03:48 AM   PM User | #2
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,765
Thanks: 29
Thanked 453 Times in 447 Posts
jmrker will become famous soon enough
Wrong forum.

This is the Javascript forum, not the Java forum.
You will probably get more useful answers from the correct forum.
jmrker is offline   Reply With Quote
Old 03-29-2011, 04:40 AM   PM User | #3
mimis40
New Coder

 
Join Date: Mar 2011
Location: Utah
Posts: 30
Thanks: 0
Thanked 6 Times in 6 Posts
mimis40 is on a distinguished road
What is the exact problem? I tried looking at your code and there were some variables that weren't initialized. What errors was the program giving you?
mimis40 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:49 PM.


Advertisement
Log in to turn off these ads.