Hi. (Sorry for my english.)
I spent last 10 whole days digging menu things in TrayIcon with no
success :((.
I have three guestions.
I can make JPopupWindow above MS-Windows task bar
( SwingUtilities.windowForComponent( JPupupMenu ).window.setAlwaysOnTop( true ) ) ,
but how to get keyboard focus/input ????
Looking back to normal PopupMenu ... when it pop up -> it blocks Event
Dispatch Thread (EDT) (even if I fire it manually .show() from new
Thread , I also tryed install new EventQueue ).
How to make non-blocking PopupMenu ? How to pop up via .show() without
"origin" ? ( MouseEvent of TrayIcon returns null in .getComponent() ).
I am using Java 6.0 b105 and u1 b03 at WinXP.
Does anybody know solutions ?
Thanks !
On Feb 24, 1:49 pm, kermi...@gmail.com wrote:
> I spent last 10 whole days digging menu things in TrayIcon with no
> success :((.
Read the documentation.
> I have three guestions.
There are no three questions, just fragments of things not making
sense. Maybe you want
URL url = getClass().getResource("/images/trayicon.gif");
if(url == null) {
return;
}
Image ii = new ImageIcon(url);
PopupMenu pm = new PopupMenu("Tray Menu");
MenuItem mi = new MenuItem("Item");
mi.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("item action");
}
});
pm.add(mi);
TrayIcon ti = new TrayIcon(ii, "Tray Icon", pm);
kermitas@gmail.com - 26 Feb 2007 11:26 GMT
Thank you for your response.
This, what you wrote, is what I did in first minutes of my work.
Can I ask you to run following very simple program ?
It changes tray icon and JPanel background every 0.5 second. The
question is : is program continue to work when you popup tray icon
menu (right click) ?
//====================================
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 ){}
}
}
}
//====================================
I am using WinXP and Java 6.0 b105 (or Java 6.0 u1 b03).
In my system all GUI operations stops while menu is on screen. I make
some works subclassing EventQueue. My suggestion is maby java goes in
some gui-lock while showing tray icon popup menu ?
Thank you for your time!
kermitas@gmail.com - 26 Feb 2007 15:15 GMT
PS: This is the last event that goes thru EventQueue after I right
click tray icon, and before menu shows (and blocks everythig):
java.awt.event.InvocationEvent[INVOCATION_DEFAULT,runnable=sun.awt.windows.WTrayIconPeer
$1@128e20a,notifier=null,catchExceptions=false,when=1172501444578] on
sun.awt.windows.WToolkit@1e0cf70
kermitas@gmail.com - 22 Mar 2007 17:58 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 !