how to run threads in background

hi everyone!

I need some hints on my problem!

I have an application with some working threads. When the user minimizes the app the app should go to the system tray and the threads should keep working.

I already know how to put an icon with popup menu in the systray. Now, how can I run the working threads in background?

Can anybody help me?

C u,

Filipe

[396 byte] By [filipea] at [2007-11-26 23:49:42]
# 1
What do you mean?The threads will continue to run, you don't need to do anything.
itchyscratchya at 2007-7-11 15:26:34 > top of Java-index,Desktop,Core GUI APIs...
# 2
I'm running a GUI. I guess if I want to put it in the systray I need to kill the app or not? What happens if I put something in the systray? Will the app keep running? I thought not...
filipea at 2007-7-11 15:26:34 > top of Java-index,Desktop,Core GUI APIs...
# 3
all thread run in the backgroud.. may be your you saying that how would i start itthis is how!!Thread a=new Thread( new className_or_object,5000);a.start();//start the
G_Abubakra at 2007-7-11 15:26:34 > top of Java-index,Desktop,Core GUI APIs...
# 4

I said that because as far I understood the following code it just creates an icon and popup menu, right?

So, when the user minimizes the app I need to kill it, create the icon and popup menu and leave the threads working.

public class SystemTrayDemo extends JFrame

{

private static final long serialVersionUID = 1;

public SystemTrayDemo()

{

JPopupMenu menu = new JPopupMenu("Menu");

JMenuItem menuItem1 = new JMenuItem("Menu1");

JMenuItem menuItem2 = new JMenuItem("Menu2");

JMenuItem menuItem3 = new JMenuItem("Menu3");

JMenuItem menuItem4 = new JMenuItem("Exit");

menu.add(menuItem1);

menu.add(menuItem2);

menu.add(menuItem3);

menu.add(menuItem4);

menuItem4.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent evt) {

System.exit(0);

}});

ImageIcon icon = new ImageIcon("database.png");

TrayIcon trayIcon = new TrayIcon(icon, "Hello System Tray", menu);

SystemTray tray = SystemTray.getDefaultSystemTray();

tray.addTrayIcon(trayIcon);

}

public static void main(String[] args)

{

try

{

javax.swing.UIManager.setLookAndFeel("com.sun.java.swing. plaf.windows.WindowsLookAndFeel");

}

catch(Exception e)

{

System.out.println(e);

}

new SystemTrayDemo();

}}

filipea at 2007-7-11 15:26:34 > top of Java-index,Desktop,Core GUI APIs...
# 5
yes the system will keep running. As long as thread is alive.
G_Abubakra at 2007-7-11 15:26:34 > top of Java-index,Desktop,Core GUI APIs...
# 6

Create another class with Runnable interface.. with a run method then call like this;

public class Frame1 implements Runnable{

//...

public void run(){}

}

call it

Thread t=new Thread(new Frame1,4000);

If i understood your question right

G_Abubakra at 2007-7-11 15:26:34 > top of Java-index,Desktop,Core GUI APIs...
# 7

Forgive my bad english...

The first thing I want to do is when the user minimizes the GUI it goes to the systray instead of go to the taskbar.

I'm already able to put an icon in the systray when the minimize window event is captured. But the window is still minimized in the taskbar and if close it the tray icon also disapears...

Hope u understand

Tanks

filipea at 2007-7-11 15:26:34 > top of Java-index,Desktop,Core GUI APIs...
# 8
I'm not sure if this will work, but...If your JFrame object is called "w", try calling "w.setVisible(false);" when you add yourself into the SysTray.And, of course, call "w.setVisible(true);" when you take yourself out of the SysTray.
KathyMcDonnella at 2007-7-11 15:26:34 > top of Java-index,Desktop,Core GUI APIs...
# 9

the window is still minimized in the taskbar and if close it the tray icon also disapears

Then make sure your frame's default close operation is something other than "EXIT_ON_CLOSE" if you're using that option to handle closure (which is normal) - and/or remove any WindowListeners which close the application on frame closure if you're using that technique.

itchyscratchya at 2007-7-11 15:26:34 > top of Java-index,Desktop,Core GUI APIs...
# 10

actually, I was having problems with the image not appearing as well. I'm on an Windows XP OS and the only image format that seems to work is .gif. I don't know why the other formats don't work.

As to the running the thread in the background, I'm not sure if this works or not, but I found something that sounded promising, I intend to test it later myself:

SwingUtilities.invokeLater(new Runnable() {

public void run() {

yourMethodHere();

}

});

I found this in the TrayIconDemo at:

http://java.sun.com/docs/books/tutorial/uiswing/examples/misc/TrayIconDemoProject/src/misc/TrayIconDemo.java

Hitenna at 2007-7-11 15:26:34 > top of Java-index,Desktop,Core GUI APIs...
# 11

the window is still minimized in the taskbar and if close it the tray icon also disapears

In addition to itchyscratchy's comment, you could use the dispose(); method, which will perform whatever action you set the setDefaultCloseOperation(); method. I would recommend the HIDE_ON_CLOSE operation in order to keep your program running while no window being open.

Hitenna at 2007-7-11 15:26:34 > top of Java-index,Desktop,Core GUI APIs...