aspose file tools*
The moose likes Applets and the fly likes Alert Applet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Applets
Bookmark "Alert Applet" Watch "Alert Applet" New topic
Author

Alert Applet

chris gelo
Greenhorn

Joined: Oct 26, 2012
Posts: 7
Hi all, any help with this would be much appreciated. I created an applet that uses javax.mail to check for new email messages in a specified account. The purpose is to alert a user to new urgent messages that arrive in that account. I have it working well as long as the applet is being displayed on the screen. If the user moves away from the screen it no longer alerts. Of course it will when they go back to the page but the idea is to draw attention to a new urgent email message. I'm having a problem with the applet life cycle but I'm not sure if there is any way to keep the applets thread running when not looking directly a the website. Here's the code

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.net.URL;


public class EmailAlert extends Applet implements ActionListener, Runnable {

/**
*
*/
private static final long serialVersionUID = 1L;
Button clearButton;
static CheckMail checkMail = new CheckMail();
boolean running = true;
Thread thread;
AudioClip alertSound;
URL soundToPlay = getClass().getResource("Horn.wav");
AudioClip player = Applet.newAudioClip(soundToPlay);

public void init()
{
setLayout(new FlowLayout());
setSize(100,100);
clearButton = new Button("Clear Alert");
clearButton.addActionListener(this);
add(clearButton);
thread = new Thread(this);
thread.start();

}
public void paint(Graphics g)
{
int width = getWidth();
int height = getHeight();

g.drawRect(0, 0, (int)(0.5 * width), (int)(0.5 * height));
this.clearButton.setLocation((int)(width * 0.15), height - 25);
if (checkMail.getAlert())
{
player.play();
g.setColor(Color.RED);
g.fillRect( 0, 0, (int)(getWidth()- 0.5 * getWidth()), (int)(getHeight() - 0.5 * getHeight()));
}
else
{
g.setColor(Color.GREEN);
g.fillRect( 0, 0, (int)(getWidth()- 0.5 * getWidth()), (int)(getHeight() - 0.5 * getHeight()));
}

}

@Override
public void actionPerformed(ActionEvent evt) {
if(evt.getSource() == clearButton)
checkMail.clearAlert();
}
@Override
public void run() {
while (running)
try
{

checkMail.getAlertStatus();
thread.sleep(20000);
repaint();
}
catch (InterruptedException e)
{
System.out.println(e);
}

}
}
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61653
    
  67

Of course not. If the user isn't on the applet's page, it isn't loaded or running.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
chris gelo
Greenhorn

Joined: Oct 26, 2012
Posts: 7
Any thoughts on an alternate method that would achieve the result I'm looking for?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18907
    
    8

A Java application which runs independently of the browser and which produces task bar notifications when it detects the presence of mail messages.
chris gelo
Greenhorn

Joined: Oct 26, 2012
Posts: 7
Is there any way to keep an applet running when the user minimizes the webpage containing the applet? I guess would be a better question
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18907
    
    8

But the applet does keep running, doesn't it? And it does change its appearance when notifying, doesn't it? It's just that the user can't tell that until he/she un-minimizes the browser.
chris gelo
Greenhorn

Joined: Oct 26, 2012
Posts: 7
Hi Paul, thanks I was thinking along the same lines after running into the prob. just trying to salvage some of the time I wasted trying it through an applet.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18907
    
    8

Well, it wouldn't be much effort to modify that to be an application. In fact if you'd written it to use a JPanel inside a JApplet in the first place there would be essentially no effort at all.
chris gelo
Greenhorn

Joined: Oct 26, 2012
Posts: 7
one part of it is suppose to play a horn sound, it'd doesn't play that sound until the webpage is maximized again
chris gelo
Greenhorn

Joined: Oct 26, 2012
Posts: 7
maybe if I put the horn sound in the thread ?
chris gelo
Greenhorn

Joined: Oct 26, 2012
Posts: 7
That did it, as long as I call the horn noise in the thread the user will hear it even if the screen is minimized. That way it draws there attention to it. Don't know why I called it in the paint method, not very bright
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18907
    
    8

Yeah... that was hard for anybody else to see because you didn't format your code when you posted it. In future it would help if you used the "Code" tags around code when you post it.

And yes, the paint() method will be called every time the JVM decides it's time to redisplay that component. For example if you maximized some other application over the browser, and then minimized it so that the browser became visible again, then paint() would probably be called.
 
 
subject: Alert Applet