Tiger Member
Posts : 7 Join date : 2011-04-17
| Subject: how to check if window is visible? Wed May 11, 2011 11:27 pm | |
| hello, i have a simple program that opens nested in the icontray ( windows ). this part works fine, I can launch the program from there, however if I can click more then once and the program opens several times. so i'm wondering how can i check it the window is already open and if it is, simple minimize it back to the icon tray? my code thus far; - Code:
-
public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { ShippingQuote application = new ShippingQuote(); application.getJFrame().setVisible(true);
if (SystemTray.isSupported()) { SystemTray tray = SystemTray.getSystemTray();
trayIcon.setImageAutoSize(true); trayIcon.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {
ShippingQuote application = new ShippingQuote(); application.getJFrame().setVisible(true); } });
try { tray.add(trayIcon); } catch (AWTException e) { System.err.println("TrayIcon could not be added."); } } } });
|
|
Skilletrockz Super Moderator
Posts : 45 Join date : 2011-05-11
| Subject: Re: how to check if window is visible? Wed May 11, 2011 11:28 pm | |
| It won't quite work with what you have, your listener is spawning a new instance each time (assuming that the getJFrame indicates that the ShippingQuote contains a frame / is a frame). Here's a quick mish-mash I put together. I choose to use a mouseevent instead since I added the display text. Clicking the balloons would trigger the action event which is also not super if you wanted to use the balloon. I also don't like the double click force, so I like the click control instead. - Code:
-
public class Frametest extends JFrame { public static void main(String[] argv) { final Frametest f = new Frametest(); f.setSize(100, 100); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); if (SystemTray.isSupported()) { SystemTray tray = SystemTray.getSystemTray(); try { ImageIcon i = new ImageIcon("C:\\java.jpeg"); TrayIcon ti = new TrayIcon(i.getImage()); ti.setImageAutoSize(true); ti.addMouseListener(new MouseAdapter(){ @Override public void mouseClicked(MouseEvent e) { super.mouseClicked(e); e.consume(); if (e.getButton() == MouseEvent.BUTTON1) { TrayIcon ti = (TrayIcon)e.getSource(); ti.displayMessage("Frame Toggle", f.isVisible() ? "Closing frame..." : "Opening frame...", MessageType.INFO); f.setVisible(!f.isVisible()); } } }); tray.add(ti); } catch (Exception ex) { System.err.println(ex.getMessage()); } } else { f.setVisible(true); } } }
|
|
Tiger Member
Posts : 7 Join date : 2011-04-17
| Subject: Re: how to check if window is visible? Wed May 11, 2011 11:29 pm | |
| thanks! i'll give this a shot
|
|
Sponsored content
| Subject: Re: how to check if window is visible? | |
| |
|