Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2007

Tip: Looking for answers? Try searching our database.

Strange GUI freezing TrayIcon + PopupMenu, please help

Thread view: 
kermitas@gmail.com - 27 Feb 2007 10:21 GMT
Can anybody help me ?

I wrote very simple program demostrating some GUI activities (changing
JPanel background color and tray icon). The problem is that my program
freez when I popup tray icon menu (right click) ! :(

This hapens on Windows XP and 2000 on Java 6.0 b105 and u1 b03.

I also tryed popup menu manually via .show() from new thread, but it
still blocks my program GUI.

Can you just copy & paste this program, run it and tell me behaviour
on your computer ???? Thank you very much.

Maby somebody know what I am doing wrong and how to use PopupMenu
without blocking other GUI operations ???

//====================================
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class IsTrayIconMenuBlocking3
{
       public static void main( String[] args ) throws Exception
       {
               // --- JFrame & JPanel section
               final JPanel jp = new JPanel();

               JFrame jf = new JFrame();
               jf.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
               jf.add( jp );
               jf.setSize( 300 , 300 );
               jf.setVisible( true );

               // --- menu item action
               ActionListener itemExitAction = new ActionListener()
               {
                       public void actionPerformed( ActionEvent e )
                       {
                               System.out.println( "item action:
exit" );
                               System.exit( 0 );
                       }
               };

               // --- popup menu
               PopupMenu pm = new PopupMenu( "Tray Menu" );
               MenuItem mi = new MenuItem( "Exit" );
               mi.addActionListener( itemExitAction);
               pm.add( mi );

               // --- system tray & tray icon
               final TrayIcon ti = new
TrayIcon( ((ImageIcon)UIManager.getIcon("OptionPane.questionIcon")).getImage() ,
"Tray Icon" , pm );
               SystemTray st = SystemTray.getSystemTray();
               ti.setImageAutoSize( true );
               st.add( ti );

               // --- color & icon changing loop
               final Image[] trayIcons = new Image[3];
               trayIcons[0] =
((ImageIcon)UIManager.getIcon("OptionPane.errorIcon")).getImage();
               trayIcons[1] =
((ImageIcon)UIManager.getIcon("OptionPane.warningIcon")).getImage();
               trayIcons[2] =
((ImageIcon)UIManager.getIcon("OptionPane.informationIcon")).getImage();

               Runnable colorChanger = new Runnable()
               {
                       private int counter = 0;
                       private int icon_no = 0;

                       public void run()
                       {
                               System.out.println( "Hello from EDT "
+ counter++ );

                               if( jp.getBackground() == Color.RED )

jp.setBackground( Color.BLUE );
                               else
                                       jp.setBackground( Color.RED );

                               ti.setImage( trayIcons[icon_no++] );
                               if( icon_no == trayIcons.length )
icon_no = 0;
                       }
               };

               while( true )
               {

javax.swing.SwingUtilities.invokeLater( colorChanger);
                       try{Thread.sleep( 500 );} catch ( Exception e )
{}
               }

       }

}

//====================================

Once again, thanks !!
Artur Stanek, PL
Chris Uppal - 27 Feb 2007 19:25 GMT
> I wrote very simple program demostrating some GUI activities (changing
> JPanel background color and tray icon). The problem is that my program
> freez when I popup tray icon menu (right click) ! :(

I tried your program, and the same thing happens here on WinXP.

> Maby somebody know what I am doing wrong and how to use PopupMenu

I woudn't be surprised if the popup menu is run in a modal loop from inside the
Windows implementation of AWT.  If that /is/ so, then I don't think you'll be
able to avoid blocking the EDT while the menu is popped up.

   -- chris
gethostbyname@gmail.com - 28 Feb 2007 02:28 GMT
On 27 fev, 16:25, "Chris Uppal" <chris.up...@metagnostic.REMOVE-
THIS.org> wrote:
> kermi...@gmail.com wrote:
> > I wrote very simple program demostrating some GUI activities (changing
[quoted text clipped - 10 lines]
>
>     -- chris

I am a newbie to JAVA. :-)
I has a doubt about the

"jf.setVisible( true ); "

Isn't recommended to put it after the addition of components to the
JFrame in main? Before the loop while in that example.

Sometimes, when I don't do it on WinXP, the window components aren't
drawn in window on first time. I need to resize the window to
components to apper.

thanks you
gethostbyname
kermitas@gmail.com - 22 Mar 2007 17:59 GMT
Hi,

thanks for help.

I am closing this subject.

My solution can be found at http://forums.java.net/jive/thread.jspa?threadID=23466&tstart=0
.

( java.net Forums  » Java Desktop Technologies  » Swing & AWT »
TrayIcon PopupMenu or JPopupMenu ?  )

Bye !


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.