Hi
I got several Threads:
one to Listen the Joystick input and fire an event when a significant change was detected, works very good with jinput.
the other one is a swing jFrame shown for several seconds when a event was fired.
The basic was my app does is listen to the joystick -> execute a action and displays a JFrame what happend.
I dont wanna that 2 JFrames show up at the same time, when a new JFrame should be opened the older one should dispose before the new one shows up.
1 2 3 4 5 6 7
| ... while(queue.getNextEvent(event)) { StringBuffer buffer = new StringBuffer(); Component comp = event.getComponent(); JoyStickAction.action(comp, event.getValue()); } ... |
the joystick listener executing the action class
1 2 3 4 5
| ... if(display_window) { new DisplayInfo("title", window_text, 250, 50, 1000).start(); } ... |
at the end of the action class
DisplayInfo is the class that opens the JFrame I wanna close when already a instance was createt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
| import javax.swing.*; import java.awt.*; public class DisplayInfo extends Thread { String title; String text;
int width = 400; int height = 100;
int time = 3000;
static int instances = 0; static DisplayInfo instance;
public DisplayInfo(String title, String text, int width, int height, int time) { if(instances>0) { instance.stopInstance(); } ++instances; instance = this; this.title = title; this.text = text; this.width = width; this.height = height; this.time = time; }
public void run() { JFrame frame = new JFrame(title); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setUndecorated(true);
JLabel label = new JLabel(instances+" - "+text, JLabel.CENTER); label.setForeground(new Color(0xBB, 0xBB, 0xBB)); frame.getContentPane().add(label);
frame.setSize(width, height);
Toolkit toolkit = Toolkit.getDefaultToolkit(); Dimension screenSize = toolkit.getScreenSize();
int x = (screenSize.width - frame.getWidth()) / 2; int y = ((screenSize.height - frame.getHeight()) / 3) * 2;
frame.getContentPane().setBackground(new Color(0x33, 0x33, 0x33));
frame.setLocation(x ,y); frame.setVisible(true);
try { Thread.currentThread().sleep(time); } catch(InterruptedException ie) { } frame.dispose(); }
public void stopInstance() { --instances; this.stop(); } } |
I already tested something with instance counting and trying to close the old one but that wont work.
Probably the the thread wont be passed by reference to "instance"?
It's the way I would do it in PHP

How could I get it work?
Hopefully you understand what I wanna say...