I have a class Banner that extends JFrame implements ActionListener and is responsible for thread handling via actionPerfmored.
"launch banner", "pause banner" works. "resume banner" seems to be dead alltogether

import java.awt.*;
import javax.swing.*;

public class BannerP extends JPanel implements Runnable {
	int width, height, xPos=10;
	boolean stopT;
	
	public BannerP(int width, int height) {
		this.setPreferredSize(new Dimension(width, height/3));
		this.width = width;
		this.height = height;
	}

	public void run() {
		while(true) {

			if(stopT) {
				synchronized (this) {
					while(stopT) {
						try {
							wait();
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
					}//while
				}//sync	
			} //if
			
			if(xPos<=width)xPos+=5;
			else xPos=0;
			repaint();
			
			try {
				Thread.sleep(20);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}				

		}// end while
	} //end run
	
	public void paint(Graphics g) {
		super.paint(g);
		g.setFont(new Font("Arial", Font.BOLD, 20));
		g.drawString("banner", xPos, height/3);
	}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.Thread;

public class Banner extends JFrame implements ActionListener {
	public JPanel  up, middle, bott;
	public BannerP bnrP;
	private JButton bnr[], tmr[];
	private Thread bnrT, tmrT;
	
	public Banner() {
		this.setSize(600, 500);
		
		up = new JPanel();
			up.setLayout(new FlowLayout(FlowLayout.CENTER));
			bnr = new JButton[3];
			bnr[0] = new JButton("launch banner");
			bnr[1] = new JButton("pause banner");
			bnr[2] = new JButton("resume banner");
			bnr[0].addActionListener(this);
			bnr[1].addActionListener(this);
			bnr[2].addActionListener(this);
			up.add(bnr[0]); 
			up.add(bnr[1]);
			up.add(bnr[2]);	
		this.getContentPane().add(BorderLayout.PAGE_START, up);
		
		middle = new JPanel(new FlowLayout(FlowLayout.CENTER));		
			bnrP = new BannerP(this.getWidth(), this.getHeight());
			bnrT = new Thread(bnrP);
			
			middle.add(bnrP);
		this.getContentPane().add(BorderLayout.CENTER, middle);
		//-------------------
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
	} //end Banner
	
	public void actionPerformed(ActionEvent e) {
		if(e.getActionCommand()=="launch banner") { bnrT.start(); bnr[0].setEnabled(false); }
		else if(e.getActionCommand()=="pause banner") { bnrP.stopT = true; }
		else if(e.getActionCommand()=="resume banner") { 
			bnrP.stopT = false; 
			synchronized(bnrT) {bnrT.notify(); } 
		}	
	}
	
	public static void main(String agrs[]) {
		Banner banner = new Banner();
	
	}
}

Recommended Answers

All 4 Replies

read around and found an option volatile to prevent the variables from being cached. got nowhere with that either, however it seems that my run() is broken as after being wake()'ed it doesnt continue. determined with System.out.println in various places throught the method

Possibly it's a lock problem. This may be completely wrong, but...
BannerP has a wait loop that's synched on "this". You try to exit the loop with a notify in Banner, but that's enclosed in a synch on the same object, so the notify will never be executed as long as BannerP is waiting for it. You could see if this is true by putting in a couple of quick prints at the appropriate places.
You probably don't need all those synchs anyway.

if removing either of synchs i get IllegalMonitorStateException.
looking at changing with notifyAll, no luck so far

This may be more re-architecting than you want to do but...

You are using a wait loop to run an animation, and you are having all kinds of problems with the Thread management. The "right" way to do this in Java/swing is to use a Swing Timer. You set up a Swing Timer to fire every 20 mSec and update the scrolling banner. The whole pause/resume thing comes down to stopping and restarting the timer. No thread problems no worries. It's up to you.
http://java.sun.com/docs/books/tutorial/uiswing/misc/timer.html

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.