Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

JDialog notification problem

Status
Not open for further replies.

GarrettSF

Programmer
Jul 22, 2008
11
US
I have a class that makes a JDialog popup notification display, letting you know that you have new email. I have set the popup to be always on top. The problem is, I can't figure out how to keep the popup dialogs from making the current window, for example FireFox, become deactivated. When the Firefox window becomes deactivated, you need to click inside the window for it used again.

Is there anyway to make the popups always display on top of other windows but force them to display in a 'ghosted out' state until the user clicks on them?

Thanks in advance,
Garrett
 
You can use JDIC (JDesktop Integration Components).
This way you can add an element to the windows task bar (like the msn icon), and from this element you can show messages that don't deactivate the focused window.
 
I am writing a program that will take an ArrayList of JPanels, create a JDialog using one of the JPanels, and animate the JDialog into display, similar to popup notifications of MSN. The program should terminate after all of the JDialogs have been displayed and disposed but it doesn't.

Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
 
public class NotificationIO
{
private int howFar2Move;
	 
	public NotificationIO(ArrayList<JPanel> panelsForNotifs)
	{
		howFar2Move = panelsForNotifs.size();
		for(int i = 0;i<panelsForNotifs.size();i++){
			JPanel infoPanel = panelsForNotifs.get(i);
			JDialog dialog = new JDialog(new JFrame(),"New Post Alert");
			infoPanel.setMaximumSize(new Dimension(200,100));
			dialog.setSize(200,100);
			dialog.getContentPane().add(infoPanel);
			dialog.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
			Toolkit toolkit = Toolkit.getDefaultToolkit();
			Dimension d = toolkit.getScreenSize();
			int x = d.width - dialog.getWidth();
			int y = d.height - getScreenBottomInset(toolkit);
			dialog.setLocation(x,y);
			dialog.setAlwaysOnTop(true);		
			dialog.setFocusableWindowState(false);
                dialog.addWindowListener(new WindowAdapter() {
                    public void windowOpened(WindowEvent we) {
                        we.getWindow().setFocusableWindowState(true);
                        we.getWindow().removeWindowListener(this);
                    }
                });
			dialog.setVisible(true);
			start(dialog,howFar2Move);
			closeFrameTimer(dialog);
			howFar2Move--;
			}
		}	
 
	private void start(JDialog dialog,int far)
	{
		final int howFar = far;
		final JDialog currentDialog = dialog;
		
		Thread thread = new Thread(new Runnable(){
			public void run(){
				boolean exit = false;
				while(!exit){
					Toolkit toolkit = Toolkit.getDefaultToolkit();
					int bottomInset = getScreenBottomInset(toolkit);
					int speed = 4;
					boolean done = false;
		
					while(!done){
						try{
							Thread.sleep(10);
						}catch(InterruptedException e){
							System.out.println("interrupted");
							done = true;
							}
						Dimension d = toolkit.getScreenSize();
						Rectangle r = currentDialog.getBounds();
						currentDialog.setLocation(r.x, r.y-speed);
						if((d.height - r.y) >= ((r.height*howFar) + bottomInset)){
							toolkit.beep();
							done = true;
							}
						}
					exit = true;
					}
				}
			});
		thread.setPriority(Thread.NORM_PRIORITY);
		thread.start();
		}
		
	private void closeFrameTimer(JDialog pop)
	{
		final JDialog popup = pop;
		final java.util.Timer timer = new java.util.Timer();
		int delay = 20000 - (1000 * howFar2Move * 2);

		timer.schedule(new TimerTask(){
			public void run(){
				popup.dispose();
				timer.cancel();
				}
			},delay);
		}
 
	private int getScreenBottomInset(Toolkit toolkit)
	{
		GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
		GraphicsDevice gd = ge.getDefaultScreenDevice();
		GraphicsConfiguration gc = gd.getDefaultConfiguration();
		Insets insets = toolkit.getScreenInsets(gc);
		return insets.bottom;
		}
		
	public static void main(String[] args)
	{
		final ArrayList<JPanel> panels = new ArrayList<JPanel>();
		JPanel testPanel = new JPanel();
		JLabel testLabel = new JLabel("BLAHBLAH  -- 1");
		testPanel.add(testLabel);
		panels.add(testPanel);
		JPanel testPanel2 = new JPanel();
		JLabel testLabel2 = new JLabel("BLAH  -- 2");
		testPanel2.add(testLabel2);
		panels.add(testPanel2);
		JPanel testPanel3 = new JPanel();
		JLabel testLabel3 = new JLabel("BLAH  -- 3");
		testPanel3.add(testLabel3);
		panels.add(testPanel3);
		
		Thread notificationThread = new Thread(){
			public void run(){
				SwingUtilities.invokeLater(new Runnable(){
					public void run(){				
						new NotificationIO(panels);
						}
					});
				}
			};
		notificationThread.start();
		}
	}

Any help would be greatly appreciated,
Garrett
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top