Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Alert Applet

 
chris gelo
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65105
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course not. If the user isn't on the applet's page, it isn't loaded or running.
 
chris gelo
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any thoughts on an alternate method that would achieve the result I'm looking for?
 
Paul Clapham
Sheriff
Posts: 21298
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21298
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21298
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
maybe if I put the horn sound in the thread ?
 
chris gelo
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21298
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic