Strange GUI freezing TrayIcon + PopupMenu, please help
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) ! :(
I checked it 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.*;
publicclass IsTrayIconMenuBlocking3
{
publicstaticvoid 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()
{
publicvoid 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()
{
privateint counter = 0;
privateint icon_no = 0;
publicvoid 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
[5052 byte] By [
kermitasa] at [2007-11-26 19:47:39]

# 1
Yes. It happens to me too.
I have tried using SwingWorker but nothing changes.
It seems to me that PopupMenu blocks the EDT, try
to put on you test frame a popup, probably when pop-up
your gui stops to change colors.
package test;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class IsTrayIconMenuBlocking3
{
public static void main(String[] args) throws Exception
{
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);
WorkerTray vTray = new WorkerTray();
vTray.execute();
ColorChanger vColor = new ColorChanger(jp, vTray.get());
vColor.execute();
}
}
package test;
import java.awt.Color;
import java.awt.Image;
import java.awt.TrayIcon;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;
import javax.swing.UIManager;
public class ColorChanger extends SwingWorker<Boolean, Void>
{
JPaneljp;
TrayIconti;
private int counter= 0;
private int icon_no= 0;
Image[]trayIcons = new Image[3];
public ColorChanger(JPanel aPanel, TrayIcon anIcon)
{
jp = aPanel;
ti = anIcon;
trayIcons[0] = ((ImageIcon) UIManager.getIcon("OptionPane.errorIcon"))
.getImage();
trayIcons[1] = ((ImageIcon) UIManager
.getIcon("OptionPane.warningIcon")).getImage();
trayIcons[2] = ((ImageIcon) UIManager
.getIcon("OptionPane.informationIcon")).getImage();
}
@Override
protected Boolean doInBackground() throws Exception
{
boolean vCicle = true;
while (vCicle)
{
System.out.println("Hello from EDT " + counter++);
if (jp.getBackground() == Color.RED)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
jp.setBackground(Color.BLUE);
}
});
}
else
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
jp.setBackground(Color.RED);
}
});
}
ti.setImage(trayIcons[icon_no++]);
if (icon_no == trayIcons.length)
icon_no = 0;
Thread.currentThread().sleep(500);
}
return new Boolean(true);
}
}
package test;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.SwingWorker;
import javax.swing.UIManager;
public class WorkerTray extends SwingWorker<TrayIcon, Void>
{
TrayIcon wTray;
public WorkerTray()
{
}
@Override
protected TrayIcon doInBackground() throws Exception
{
PopupMenu pm = new PopupMenu("Tray Menu");
MenuItem mi = new MenuItem("Exit");
// mi.addActionListener(itemExitAction);
pm.add(mi);
// system tray & tray icon
wTray = new TrayIcon(((ImageIcon) UIManager
.getIcon("OptionPane.questionIcon")).getImage(), "Tray Icon", pm);
mi.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent aE)
{
System.out.println("quiquiquiquqi");
System.exit(0);
}
});
SystemTray st = SystemTray.getSystemTray();
wTray.setImageAutoSize(true);
st.add(wTray);
return wTray;
}
}
silsa at 2007-7-9 22:34:38 >
