Skip to content

Instantly share code, notes, and snippets.

@rostyslav
Created July 5, 2011 21:51
Show Gist options
  • Save rostyslav/1066056 to your computer and use it in GitHub Desktop.
Save rostyslav/1066056 to your computer and use it in GitHub Desktop.
Sample notification alerter written in Java
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JWindow;
import javax.swing.UIManager;
public class Alerter extends JWindow
{
private Dimension screenSize;
private JLabel messageLabel;
private final static int ERROR_MESSAGE = 0;
private final static int INFORMATION_MESSAGE = 1;
private final static int WARNING_MESSAGE = 2;
private final static int QUESTION_MESSAGE = 3;
public Alerter()
{
//super("Alert");
screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
//setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
// setResizable(false);
messageLabel = new JLabel("", JLabel.CENTER);
setLayout(new BorderLayout());
setAlwaysOnTop(true);
getContentPane().add(messageLabel, BorderLayout.CENTER);
JButton button = new JButton("<HTML><FONT FACE = Tahoma>DISMISS</FONT></HTML>");
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout());
buttonPanel.add(button);
getContentPane().add(buttonPanel, BorderLayout.SOUTH);
button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
for (int i = 100; i > 0; i--)
{
setBounds((screenSize.width - 400), (screenSize.height - i) - 30, 400, 100);
try
{
Thread.sleep(1/1000);
}
catch (Exception er)
{
}
setVisible(true);
}
setVisible(false);
dispose();
System.exit(0);
}
});
}
public void alert(String message, int messageType)
{
switch (messageType)
{
case 0: // ERROR_MESSAGE
{
showError(message);
}
break;
case 1: // INFORMATION_MESSAGE
{
showInfo(message);
}
break;
case 2: //WARNING_MESSAGE
{
showWarning(message);
}
break;
case 3: //QUESTION_MESSAGE
{
askQuestion(message);
}
break;
default:
{}
break;
}
}
private void showWarning(String message)
{
messageLabel.setText("<HTML><FONT COLOR = YELLOW FACE = Tahoma>" + message + "</FONT></HTML>");
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
for (int i = 0; i < 100; i++)
{
setBounds((screenSize.width - 250), (screenSize.height - i) - 30, 250, 100);
Thread.sleep(1/1000);
setVisible(true);
}
Thread.sleep(10000);
for (int i = 100; i > 0; i--)
{
setBounds((screenSize.width - 250), (screenSize.height - i) - 30, 250, 100);
Thread.sleep(1/1000);
setVisible(true);
}
}
catch (Exception e)
{}
finally
{
setVisible(false);
dispose();
}
}
private void showInfo(String message)
{
messageLabel.setText("<HTML><FONT COLOR = BLUE FACE = Tahoma>" + message + "</FONT></HTML>");
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
for (int i = 0; i < 100; i++)
{
setBounds((screenSize.width - 250), (screenSize.height - i) - 30, 250, 100);
Thread.sleep(1/1000);
setVisible(true);
}
Thread.sleep(10000);
for (int i = 100; i > 0; i--)
{
setBounds((screenSize.width - 250), (screenSize.height - i) - 30, 250, 100);
Thread.sleep(1/1000);
setVisible(true);
}
}
catch (Exception e)
{}
finally
{
setVisible(false);
dispose();
}
}
private void showError(String message)
{
messageLabel.setText("<HTML><FONT COLOR = RED FACE = Tahoma>" + message + "</FONT></HTML>");
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
for (int i = 0; i < 100; i++)
{
setBounds((screenSize.width - 400), (screenSize.height - i) - 30, 400, 100);
Thread.sleep(1/1000);
setVisible(true);
}
Thread.sleep(10000);
for (int i = 100; i > 0; i--)
{
setBounds((screenSize.width - 400), (screenSize.height - i) - 30, 400, 100);
Thread.sleep(1/1000);
setVisible(true);
}
}
catch (Exception e)
{}
finally
{
setVisible(false);
dispose();
}
}
private void askQuestion(String message)
{
messageLabel.setText("<HTML><FONT COLOR = GREEN FACE = Tahoma>" + message + "</FONT></HTML>");
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
for (int i = 0; i < 100; i++)
{
setBounds((screenSize.width - 250), (screenSize.height - i) - 30, 250, 100);
Thread.sleep(1/1000);
setVisible(true);
}
Thread.sleep(10000);
for (int i = 100; i > 0; i--)
{
setBounds((screenSize.width - 250), (screenSize.height - i) - 30, 250, 100);
Thread.sleep(1/1000);
setVisible(true);
}
}
catch (Exception e)
{}
finally
{
setVisible(false);
dispose();
}
}
public static void main(String[] args)
{
Alerter alerter = new Alerter();
alerter.alert("Hello World", Alerter.ERROR_MESSAGE);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment