2

I have been using this code to implement a Popup JDialog of sorts, like what you see when your anti-virus is scanning your system or updates itself:

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

public class PopupDialog extends JDialog {

    public PopupDialog() throws HeadlessException {
        createUI();
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                PopupDialog popupDialog = new PopupDialog();
                popupDialog.setVisible(true);
            }
        });
    }

    private void createUI() {
        setTitle("Popup Dialog");
        setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);

        addComponentsToFrame();

        //This call will give screens viable area, which takes into account the taskbar, which could be at the bottom, top,left or right of the screen.
        Rectangle maxBounds = GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds();

        //get screen size
        int sWidth = maxBounds.width, sHeight = maxBounds.height;

        setSize(275, 225);//set frame size
        Dimension appDim = getSize();
        //get app size
        int aWidth = appDim.width, aHeight = appDim.height;

        //set location like a tooltip would be except its a custom dialog like an AV
        setLocation(sWidth - aWidth, (sHeight - aHeight));
    }

    private void addComponentsToFrame() {
        JLabel label = new JLabel("Popup Dialog");
        getContentPane().add(label, BorderLayout.CENTER);
    }
}

But my question is: is there any class or package in java that will do this for me? and if not how would I go about allowing the JDialog to slide up from the taskbar (or offscreen)? or somehow become visible in a slow manner like a ToolTip Popup would from the system tray. Thanks.

EDIT The reason i want to use a JDialog or Frame is because i want to be able to fully skin the Popup window, using setUndecorated(true); and adding custom exit icons, backgrounds etc

David Kroukamp
  • 36,155
  • 13
  • 81
  • 138

2 Answers2

3

You mean like the examples here: http://docs.oracle.com/javase/tutorial/uiswing/misc/systemtray.html ?

Morfic
  • 15,178
  • 3
  • 51
  • 61
  • I know how to do a system tray, however i want to be able to customize it as if it was a frame, i.e add a button and labels etc., however, i do want the ability of the slow 'Popup' of the tooltips provided by the system tray. Basically i want a frame because i will be able to skin it comletely. i.e undecorated and add labels with images for the exit icons etc – David Kroukamp Jun 18 '12 at 23:44
  • 1
    Sorry, none that I know of till now. However the guy here managed a nice translucent effect, and if you scroll down you might see some others suggested 2-3 libraries: http://stackoverflow.com/questions/3240415/how-to-create-a-notification-in-swing – Morfic Jun 18 '12 at 23:52
  • ThanksThat really helped, I guess I'll have to do it manually :( lol – David Kroukamp Jun 18 '12 at 23:54
  • 3
    No problem, if you're really keen on it, you might consider sharing your result by results via an open source library. I think others will be really happy to get their hands on something like that. – Morfic Jun 18 '12 at 23:57
  • Hehe yeh maybe I will do that. Thank you for your help! – David Kroukamp Jun 19 '12 at 07:55
1

First, you can make your frame be always on top, this will ensure it is always visible. Next, you could position the frame off the bottom of the screen and slide it up programmatically. This should be fairly smooth even on an older XP machine. There is no standard Java API to do this but you can do it yourself pretty easily. Another option instead of sliding is to make the window fully transparent and fade it in. This API was added in recent (last 2 years) Java 6 updates, so it should be available everywhere.

Josh Marinacci
  • 1,715
  • 1
  • 14
  • 15