Invalid Thread Access
Hi I am in need of a great help here.
i have initialise a class in the actionbar advisor to the menu
private NewProjectDialog newProjectDialog;
newProjectDialog = new NewProjectDialog();
register(newProjectDialog);
but whenever i run the application, the error keeps prompting out "Invalid Thread Access" anyone knows whats happening?
This is the main for the NewProjectDialog class:
public static void main(String[] args) {
new NewProjectDialog().run();
}
Thanks alot
Are you using Eciplse SWT Components?
We can't help you if we don't know the code within the classes.Maybe we could if we knew the answer to veppadai's question, but you were very cryptic with what you provided us with.
Ya it is a SWT/Jface component. So is there any explanation for the error?
Oh i wasnt really sure about this forum cause i am here for the first time i was afraid if i did all the codes out people might get turned off =D
So here is the codings
This class is in the predefined ApplicationActionBarAdvisor class
public class ApplicationActionBarAdvisor extends ActionBarAdvisor {
private NewProjectDialog newProjectDialog;
public ApplicationActionBarAdvisor(IActionBarConfigurer configurer) {
super(configurer);
}
protected void makeActions(IWorkbenchWindow window) {
newProjectDialog = new NewProjectDialog();
register(newProjectDialog);
}
protected void fillMenuBar(IMenuManager menuBar) {
MenuManager EAIMenu = new MenuManager("&File", "file");
MenuManager newMenu = new MenuManager("&New", "new");
newMenu.add(newProjectDialog);
EAIMenu.add(newMenu);
}
This is the openfiledialog class which i created.
public class NewProjectDialog extends Action implements IWorkbenchAction
{
public final static String ID = "edu.nyp.EI07_027.ui.dialog";
//private static Action action;
public NewProjectDialog() {
setId(ID);
setText("Project");
setToolTipText("Browse for a new project");
//action = this;
}
public void run(){
Display display= new Display();
Shell shell = new Shell(display);
shell.setText("Directory Browser");
createContents(shell);
shell.pack();
shell.open();
shell.setSize(750,150);
while (!shell.isDisposed()){
if(!display.readAndDispatch()){
display.sleep();
}
}
}
private void createContents(final Shell shell)
{
shell.setLayout(new GridLayout(6, true));
new Label(shell, SWT.NONE).setText("Project Name:");
final Text text = new Text(shell, SWT.BORDER);
GridData data1 = new GridData(GridData.FILL_HORIZONTAL);
data1.horizontalSpan=3;
text.setLayoutData(data1);
new Label(shell, SWT.NONE).setText("Directory:");
final Text text1 = new Text(shell, SWT.BORDER);
GridData data = new GridData(GridData.FILL_HORIZONTAL);
data.horizontalSpan=3;
text1.setLayoutData(data);
Button button = new Button(shell, SWT.PUSH);
button.setText("Browse");
new Label(shell, SWT.NONE).setText("Project Folder:");
final Text text2 = new Text(shell, SWT.BORDER);
text2.setLayoutData(data);
Button button1 = new Button(shell, SWT.PUSH);
button1.setText("Ok");
Button button2 = new Button(shell, SWT.PUSH);
button2.setText("Cancel");
button2.addSelectionListener(new SelectionAdapter(){
public void widgetSelected(SelectionEvent event){
shell.close();
shell.dispose();
}
});
button.addSelectionListener(new SelectionAdapter(){
public void widgetSelected(SelectionEvent event){
DirectoryDialog dlg = new DirectoryDialog(shell);
dlg.setFilterPath(text1.getText());
dlg.setText("New Project");
dlg.setMessage("Select a project location");
String dir=dlg.open();
if (dir!=null){
text1.setText(dir);
}
}
});
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new NewProjectDialog().run();
}
public void dispose() {
}
}
It's okay. Everyone does stupid things sometimes, and people here chew you up if you mess up.By the way, put .[.code] .[./code] tags around any code. It makes it a lot more readable (take out the periods though!)
Unfortunately... I don't know anything about Eclipse SWT, but I'm reading up on what it is...Not sure I can help you with zero knowledge though.
use the force luke
yodaa at 2007-7-12 20:40:01 >

public class ApplicationActionBarAdvisor extends ActionBarAdvisor {
private NewProjectDialog newProjectDialog;
public ApplicationActionBarAdvisor(IActionBarConfigurer configurer) {
super(configurer);
}
protected void makeActions(IWorkbenchWindow window) {
newProjectDialog = new NewProjectDialog();
register(newProjectDialog);
}
protected void fillMenuBar(IMenuManager menuBar) {
MenuManager EAIMenu = new MenuManager("&File", "file");
MenuManager newMenu = new MenuManager("&New", "new");
newMenu.add(newProjectDialog);
EAIMenu.add(newMenu);
}
This is the openfiledialog class which i created.
public class NewProjectDialog extends Action implements IWorkbenchAction
{
public final static String ID = "edu.nyp.EI07_027.ui.dialog";
//private static Action action;
public NewProjectDialog() {
setId(ID);
setText("Project");
setToolTipText("Browse for a new project");
//action = this;
}
public void run(){
Display display= new Display();
Shell shell = new Shell(display);
shell.setText("Directory Browser");
createContents(shell);
shell.pack();
shell.open();
shell.setSize(750,150);
while (!shell.isDisposed()){
if(!display.readAndDispatch()){
display.sleep();
}
}
}
private void createContents(final Shell shell)
{
shell.setLayout(new GridLayout(6, true));
new Label(shell, SWT.NONE).setText("Project Name:");
final Text text = new Text(shell, SWT.BORDER);
GridData data1 = new GridData(GridData.FILL_HORIZONTAL);
data1.horizontalSpan=3;
text.setLayoutData(data1);
new Label(shell, SWT.NONE).setText("Directory:");
final Text text1 = new Text(shell, SWT.BORDER);
GridData data = new GridData(GridData.FILL_HORIZONTAL);
data.horizontalSpan=3;
text1.setLayoutData(data);
Button button = new Button(shell, SWT.PUSH);
button.setText("Browse");
new Label(shell, SWT.NONE).setText("Project Folder:");
final Text text2 = new Text(shell, SWT.BORDER);
text2.setLayoutData(data);
Button button1 = new Button(shell, SWT.PUSH);
button1.setText("Ok");
Button button2 = new Button(shell, SWT.PUSH);
button2.setText("Cancel");
button2.addSelectionListener(new SelectionAdapter(){
public void widgetSelected(SelectionEvent event){
shell.close();
shell.dispose();
}
});
button.addSelectionListener(new SelectionAdapter(){
public void widgetSelected(SelectionEvent event){
DirectoryDialog dlg = new DirectoryDialog(shell);
dlg.setFilterPath(text1.getText());
dlg.setText("New Project");
dlg.setMessage("Select a project location");
String dir=dlg.open();
if (dir!=null){
text1.setText(dir);
}
}
});
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new NewProjectDialog().run();
}
public void dispose() {
}
}
Yes but where do you get this exception? Show the stack trace and the line of code it refers to.
ejpa at 2007-7-12 20:40:01 >

Unhandled event loop exceptionReason:Invalid thread accessThis is what i get nothing else, no trace or any line where they show the error exists.
"Unhandled event loop exception"Could your while loop be the cause of this?
I am not really sure this problem has been bugging me for 4 days, i tried everything i could possibly think off
http://help.eclipse.org/help31/index.jsp?topic=/org.eclipse.platform.doc.isv/guide/swt_threading.htm
org.eclipse.swt.SWTException: Invalid thread access
at org.eclipse.swt.SWT.error(SWT.java:2942)
at org.eclipse.swt.SWT.error(SWT.java:2865)
at org.eclipse.swt.SWT.error(SWT.java:2836)
at org.eclipse.swt.widgets.Display.checkDisplay(Display.java:629)
at org.eclipse.swt.widgets.Display.create(Display.java:690)
at org.eclipse.swt.graphics.Device.<init>(Device.java:129)
at org.eclipse.swt.widgets.Display.<init>(Display.java:390)
at org.eclipse.swt.widgets.Display.<init>(Display.java:381)
at edu.nyp.EI07_02_7.ui.rcp.NewProjectDialog.run(NewProjectDialog.java:38)
at org.eclipse.jface.action.Action.runWithEvent(Action.java:996)
at org.eclipse.jface.action.ActionContributionItem.handleWidgetSelection(ActionContributionItem.java:538)
at org.eclipse.jface.action.ActionContributionItem.access$2(ActionContributionItem.java:488)
at org.eclipse.jface.action.ActionContributionItem$5.handleEvent(ActionContributionItem.java:400)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:66)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:843)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:3125)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:2758)
at org.eclipse.ui.internal.Workbench.runEventLoop(Workbench.java:1699)
at org.eclipse.ui.internal.Workbench.runUI(Workbench.java:1663)
at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:367)
at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:143)
at edu.nyp.EI07_02_7.ui.rcp.Application.run(Application.java:18)
at org.eclipse.core.internal.runtime.PlatformActivator$1.run(PlatformActivator.java:226)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:376)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:163)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.eclipse.core.launcher.Main.invokeFramework(Main.java:334)
at org.eclipse.core.launcher.Main.basicRun(Main.java:278)
at org.eclipse.core.launcher.Main.run(Main.java:973)
at org.eclipse.core.launcher.Main.main(Main.java:948)
at edu.nyp.EI07_02_7.ui.rcp.NewProjectDialog.run(NewProjectDialog.java:38)Line 38
First of all, I'm not sure about this advice because I'm just now studying threads, but I always thought that you should never call "run()" directly, that it is a callback method, and that calling it is a sign of an error. Perhaps start() needs to be called instead. I don't know if this applies here or not or if it has anything to do with your error:
>
> public static void main(String[] args) {
> new NewProjectDialog().run();
>
> }
> First of all, I'm not sure about this advice because
> I'm just now studying threads, but I always thought
> that you should never call "run()" directly,
Normally you are correct. However in this specific case I really don't know.
This is because the advice given is correct for classes that implement Runnable. However I don't see that directly here so it's a bit hard to say.
If one of the interfaces implemented or the superclass extended implements Runnable then yes run should not be called. But it's also wrong because I don't see the thread (that one would call start on) being set up either.
Frankly I think the OP should do one of the following
- find a SWT forum
- give up using SWT
> Normally you are correct. However in this specific> case I really don't know. ......Thanks cotton.m for helping to clarify that.
public class OpenFileDialog extends Action {
public final static String ID = "edu.nyp.EI07_027.ui.OpenProjectDialog";
public OpenFileDialog()
{
setId(ID);
setText("Project");
setToolTipText("Browse for a new project");
}
private static final String[] FILTER_NAMES ={
"OpenOffice.org Spreadsheet Files (*.sxc)",
"Microsoft Excel Spreadsheet Files (*.xls)",
"Comma Separated Values Files (*.csv)", "All Files (*.*)"
};
private static final String[] FILTER_EXTS = {"*.sxc", "*.xls", "*.csv", "*.*"};
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Shell shell = new Shell();
FileDialog dlg= new FileDialog(shell, SWT.OPEN);
dlg.setFilterNames(FILTER_NAMES);
dlg.setFilterExtensions(FILTER_EXTS);
String fn = dlg.open();
}
}
openFileDialog = new OpenFileDialog();
register(openFileDialog);
}
protected void fillMenuBar(IMenuManager menuBar) {
MenuManager EAIMenu = new MenuManager("&File", "file");
MenuManager newMenu = new MenuManager("&New", "new");
MenuManager helpMenu = new MenuManager("&Open", "open");
//MenuManager newSubMenu = new MenuManager("&Project", "project");
//newSubMenu.add(openFileDialog);
helpMenu.add(openFileDialog);
Replying that person who ask me to give up SWT.
Firstly, if you are in my position you will be asking the same question.
Secondly, I started SWT on my own with no guidance or any previous Java knowledge.
Thirdly, if I have the choice of not to use Java trust me i wont even consider it. *No offence to Java in my that sentence
> Replying that person who ask me to give up SWT.
> Firstly, if you are in my position you will be asking
> the same question.
This reply speaks volumes about what the true nature of your problem is. You do not know how to properly read. You skim and glean only what information you consider to be appropriate which considering you don't know what you are talking about may or may not actually be helpful to you.
So let's try again shall we...?
You are using SWT. You are having problems using SWT. Several people have posted replies to say among other things that they do not know SWT so are only guessing as to what you actual problem might be.
Are you with me so far here?
Now my advice to you is that you should do one of the following.
Give up SWT OR (or is an important word here) you should find a SWT forum to ask your SWT related questions.
i didn't say stop using Java. I didn't say you HAD to give up SWT. If you are stuck with it for whatever reason that you don't want divulge here for some mysterious reason then fine. But take your SWT questions to a SWT forum. Because your questions are not suitable for this forum. They require specific knowledge and experience with SWT because they are SWT related errors.
Okay?
