Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » TrayIcon Redraw?
TrayIcon Redraw? [message #666412] Wed, 20 April 2011 11:24 Go to next message
No real name is currently offline No real nameFriend
Messages: 5
Registered: April 2011
Junior Member
Hi,

I'm trying to write a little program that uses a TrayIcon. I want to change the image there when something happens.

I assign a new image to it though, and the icon doesn't update or redraw. The only way I can get it to do so is to mouse over, which defeats the whole object.

I tried disposing and instantiating a new TrayIcon, that doesn't work either.

This is on Win32 by the way.

Is this a bug? Should I be calling some extra method? (I've tried setVisible as well).
Re: TrayIcon Redraw? [message #666608 is a reply to message #666412] Thu, 21 April 2011 10:01 Go to previous messageGo to next message
Lakshmi ShanmugamFriend
Messages: 276
Registered: July 2009
Location: India
Senior Member
Hi,

There is no TrayIcon class in SWT, did you mean TrayItem?

Updating the tray item's image works fine for me on Windows XP. Here is an example snippet of TrayItem -> Snippet143.java

I tried to change the image by using this code in the above snippet, and it works fine. Does it work for you?
shell.addListener(SWT.MouseDown, new Listener() {
public void handleEvent(Event event) {
item.setImage(display.getSystemImage(SWT.ICON_ERROR));
}
});



Lakshmi P Shanmugam
Re: TrayIcon Redraw? [message #666609 is a reply to message #666608] Thu, 21 April 2011 10:06 Go to previous messageGo to next message
No real name is currently offline No real nameFriend
Messages: 5
Registered: April 2011
Junior Member
Yeah, sorry, I meant TrayItem.

In your code, you're changing it from a mouse event on the TrayItem?

Try changing it from a timer or a button press in another window. I'm on XP, and in those cases, it doesn't work.

[Updated on: Thu, 21 April 2011 10:07]

Report message to a moderator

Re: TrayIcon Redraw? [message #666612 is a reply to message #666609] Thu, 21 April 2011 10:12 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6559
Registered: July 2009
Senior Member
Am 21.04.11 12:06, schrieb gareth.foster@thomsonreuters.com:
> Yeah, sorry, I meant TrayItem.
>
> In you're code, you're changing it from a mouse event on the TrayItem?
>
> Try changing it from a timer or a button press in another window. I'm on
> XP, and in those cases, it doesn't work.

Timer? Makes me wonder of you are not syncing with the UI-Thread.

Tom
Re: TrayIcon Redraw? [message #666621 is a reply to message #666612] Thu, 21 April 2011 10:52 Go to previous messageGo to next message
No real name is currently offline No real nameFriend
Messages: 5
Registered: April 2011
Junior Member
I think I just went part way to solving this by writing a stripped down test case.

Even though the timer fires and changes the value of iTest, the code doesn't get to changing the icon until I mouseover, because it's stuck in the readAndDispatch/sleep methods.

What do I need to do to kick it out of there?

class CMain {


int iTest = 1;

public void test() {

final Display display = Display.getDefault();
Tray tray = display.getSystemTray();
TrayItem trayItem = new TrayItem(tray, SWT.NONE);
trayItem.setImage(display.getSystemImage(SWT.ICON_ERROR));

Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {

@Override
public void run() {
++ iTest;
} }, 0, 60 * 1000);

while (true) {
if (!Display.getDefault().readAndDispatch())
Display.getDefault().sleep();

if(iTest % 2 == 0) {
trayItem.setImage(display.getSystemImage(SWT.ICON_INFORMATIO N));
} else {
trayItem.setImage(display.getSystemImage(SWT.ICON_ERROR));
}

}
}

}
Re: TrayIcon Redraw? [message #666626 is a reply to message #666621] Thu, 21 April 2011 11:27 Go to previous messageGo to next message
No real name is currently offline No real nameFriend
Messages: 5
Registered: April 2011
Junior Member
Solved it, the solution was to use wake(), as talked about half way down this page.

http://book.javanb.com/swt-the-standard-widget-toolkit/ch05l ev1sec6.html
Re: TrayIcon Redraw? [message #666630 is a reply to message #666621] Thu, 21 April 2011 11:31 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6559
Registered: July 2009
Senior Member
Am 21.04.11 12:52, schrieb gareth.foster@thomsonreuters.com:
> I think I just went part way to solving this by writing a stripped down
> test case.
>
> Even though the timer fires and changes the value of iTest, the code
> doesn't get to changing the icon until I mouseover, because it's stuck
> in the readAndDispatch/sleep methods.
>
> What do I need to do to kick it out of there?
>

To make your test code run you'd have to invoke Display.wake() in your
TimerTask but I really don't understand why you you're not doing the
update from withing your TimerTask which makes whole lot more to me:

new TimerTask {
public void run() {
final int i = ++ iTest;
display.syncExec(new Runnable() {
public void run() {
if(i % 2 == 0) {
trayItem.setImage(
display.getSystemImage(SWT.ICON_INFORMATION));
} else {
trayItem.setImage(
display.getSystemImage(SWT.ICON_ERROR));
}
}
});
}
}

Tom
Re: TrayIcon Redraw? [message #666644 is a reply to message #666630] Thu, 21 April 2011 12:40 Go to previous messageGo to next message
No real name is currently offline No real nameFriend
Messages: 5
Registered: April 2011
Junior Member
I tried doing the update from there, it blew up moaning about accessing SWT widgets from outside the main thread. I thought I'd read this wasn't allowed?
Re: TrayIcon Redraw? [message #666651 is a reply to message #666644] Thu, 21 April 2011 12:49 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6559
Registered: July 2009
Senior Member
That's why you see a display.syncExec() in there which allows you to
sync back from an thread into the UI one.

Tom

Am 21.04.11 14:40, schrieb gareth.foster@thomsonreuters.com:
> I tried doing the update from there, it blew up moaning about accessing
> SWT widgets from outside the main thread. I thought I'd read this wasn't
> allowed?
Re: TrayIcon Redraw? [message #666652 is a reply to message #666644] Thu, 21 April 2011 12:50 Go to previous message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6559
Registered: July 2009
Senior Member
and one more not you can use Display#timerExec so avoid all the thread
syncing stuff.

Am 21.04.11 14:40, schrieb gareth.foster@thomsonreuters.com:
> I tried doing the update from there, it blew up moaning about accessing
> SWT widgets from outside the main thread. I thought I'd read this wasn't
> allowed?
Previous Topic:Remove a menu item
Next Topic:SWT on Windows XP Tablet PC Edition
Goto Forum:
  


Current Time: Fri Aug 07 19:54:43 GMT 2020

Powered by FUDForum. Page generated in 0.01445 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top