Invalid thread acces Execption
For my application I have created a menu in the system tray. When you click left you get some dynamic data and when you click right on my menu you get a screen to add a new dynamic menuitem to my system tray menu. but when I try to add something to that menu I get this error:
Exception in thread "AWT-EventQueue-0" org.eclipse.swt.SWTException: Invalid thread access
import java.awt.Frame;
import java.awt.Dimension;
import javax.swing.JButton;
import java.awt.BorderLayout;
import javax.swing.SwingConstants;
import java.awt.event.KeyEvent;
import java.awt.Rectangle;
import javax.swing.JTextField;
import javax.swing.JLabel;
import org.eclipse.swt.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.widgets.*;
public class Test3 extends Frame {
private static final long serialVersionUID = 1L;
private JButton jButton = null;
private JTextField txtInput = null;
private JLabel lblInput = null;
private JLabel lblResult2 = null;
static Display display = new Display ();
static Shell shell = new Shell (display); // @jve:decl-index=0:
static MenuItem testMi = null;
/**
* This method initializes jButton
*
* @return javax.swing.JButton
*/
private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setHorizontalTextPosition(SwingConstants.C ENTER);
jButton.setMnemonic(KeyEvent.VK_UNDEFINED);
jButton.setBounds(new Rectangle(504, 246, 97, 29));
jButton.setText("Click");
//jButton.setSize(0,80);
//jButton.addActionListener(new java.awt.event.ActionListener());
jButton.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent e) {
//System.out.println("mouseClicked()"); // TODO Auto-generated Event stub mouseClicked()
lblResult2.setText(txtInput.getText());
testMi.setText("Dynamic");
}
});
//jButton.setRolloverEnabled(false);
//jButton.setPreferredSize(new Dimension(50, 10));
}
return jButton;
}
/**
* This method initializes txtInput
*
* @return javax.swing.JTextField
*/
private JTextField getTxtInput() {
if (txtInput == null) {
txtInput = new JTextField();
txtInput.setBounds(new Rectangle(187, 73, 416, 22));
}
return txtInput;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Test3 application = new Test3();
application.initialize();
application.setVisible(true);
Image image = new Image (display, 16, 16);
final Tray tray = display.getSystemTray ();
if (tray == null) {
System.out.println ("The system tray is not available");
}
else {
final TrayItem item = new TrayItem (tray, SWT.NONE);
item.setToolTipText("SWT TrayItem");
item.addListener (SWT.Show, new Listener () {
public void handleEvent (Event event) {
System.out.println("show");
}
});
item.addListener (SWT.Hide, new Listener () {
public void handleEvent (Event event) {
System.out.println("hide");
}
});
final Menu mainMenu = new Menu (shell, SWT.POP_UP);
for (int i =0; i <= 3; i++){
MenuItem mi = new MenuItem(mainMenu, SWT.PUSH);
mi.setText("Main Menu Item " + i);
mi.setEnabled(false);
if (i == 3 ){
mi.setEnabled(true);
mi.setText("Exit program");
mi.addListener(SWT.Selection, new Listener (){
public void handleEvent (Event event){
System.exit(0);
}
});
}
}
testMi = new MenuItem(mainMenu, SWT.PUSH);
testMi.setText("Static text");
item.addListener (SWT.Selection, new Listener () {
public void handleEvent (Event event) {
System.out.println("selection");
mainMenu.setVisible(true);
}
});
item.addListener (SWT.DefaultSelection, new Listener () {
public void handleEvent (Event event) {
System.out.println("default selection");
}
});
final Menu menu = new Menu (shell, SWT.POP_UP);
for (int i = 0; i < 8; i++) {
MenuItem mi = new MenuItem (menu, SWT.PUSH);
mi.setText ("Item" + i);
mi.addListener (SWT.Selection, new Listener () {
public void handleEvent (Event event) {
System.out.println("selection " + event.widget);
}
});
if (i == 0) menu.setDefaultItem(mi);
}
item.addListener (SWT.MenuDetect, new Listener () {
public void handleEvent (Event event) {
menu.setVisible (true);
}
});
item.setImage (image);
}
shell.setBounds(50, 50, 300, 200);
shell.open ();
while (!shell.isDisposed ()) {
if (!display.readAndDispatch ()) display.sleep ();
}
image.dispose ();
display.dispose ();
}
/**
* This is the default constructor
*/
public Test3() {
super();
initialize();
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
lblResult2 = new JLabel();
lblResult2.setBounds(new Rectangle(16, 117, 586, 30));
lblResult2.setText("");
lblInput = new JLabel();
lblInput.setBounds(new Rectangle(13, 70, 168, 26));
lblInput.setText(" Voer een lappie text in:");
this.setLayout(null);
this.setSize(627, 302);
this.setTitle("Frame");
this.setVisible(true);
this.add(getJButton(), null);
this.add(getTxtInput(), null);
this.add(lblInput, null);
this.add(lblResult2, null);
this.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent e) {
//lblResult2.setText("So you wanna close this window.. ;-) ");
System.exit(0);
}
});
}
} // @jve:decl-index=0:visual-constraint="10,10"
When you run this, you see a nice menu in the system tray..
add some text in the screen that popup and click on save.. now you see the error.. (In this example ill try to change the static menu into a dynamic one)
|