www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.dwt - Help me on toolbar problem

reply Sam Hu <samhu.samhu gmail.com> writes:
Content-Type: text/plain

Hi there,

I am reading SWT: A Developer's Notebook and tried some code in the book,sure I
also tried to re-organize the structure and to see what happens.In the attached
simple code,the Option menu,Window menu and toobar item CHECK does not work
properly.When I click on it,the application quit immediatley.I don't know why.

Can anyboy help me?
Thanks and best Regards,
Sam
Aug 08 2008
parent reply Frank Benoit <keinfarbton googlemail.com> writes:
Sam Hu schrieb:
 Hi there,
 
 I am reading SWT: A Developer's Notebook and tried some code in the book,sure
I also tried to re-organize the structure and to see what happens.In the
attached simple code,the Option menu,Window menu and toobar item CHECK does not
work properly.When I click on it,the application quit immediatley.I don't know
why.
 
 Can anyboy help me?
 Thanks and best Regards,
 Sam

This is a difference between Java and D. In Java when you have a 'final' variable, you can access it from an anonymous class, because that variable is copied into that anonymouse class in a hidden way. D does not do that. In you example you have 'radioItem1' as a local variable of the method createMainMenu(). It does only exist for the duration of createMainMenu() execution. After that the variable storage is reused for other stuff. (The variable was located on the stack) But the problem is, the SelectionListener uses the 'radioItem1' when the user clicks the menu. At this time the 'radioItem1' variable do no more exist. To solve this problem you need to ensure, the SelectionListener uses a valid reference. Use one the following possibilities. 1.) Make radioItem1 a member variable of Form 2.) Make radioItem1 a global variable 3.) The widget is passed in the Event object MenuItem item = cast(MenuItem) event.widget; 4.) Copy the reference into the SelectionListener at creation of it: radioItem1.addSelectionListener(new class(radioItem1) SelectionListener{ MenuItem radioItem1_; this( MenuItem a){ radioItem1_=a; } public void widgetSelected(SelectionEvent e) { if(radioItem1_.getSelection) { MessageBox.showInfo("RadioItem1 is selected.","HaHA"); } } public void widgetDefaultSelected(SelectionEvent e){} }); 5.) Not in this case, but for "Runnable" and "Listener" a template wrapper exist. See dgRunnable and dgListener. Frank
Aug 08 2008
parent reply Sam Hu <samhu.samhu gmail.com> writes:
Hi Frank,

Thank you so much for your prompt reply.
But I am still confused how come below code failed:

checkItem.addSelectionListener(new checkMenuClickListener(checkItem));    

I have passed a hanler to the constructor?

Thanks again,
Sam     
Aug 08 2008
parent reply Frank Benoit <keinfarbton googlemail.com> writes:
Sam Hu schrieb:
 Hi Frank,
 
 Thank you so much for your prompt reply.
 But I am still confused how come below code failed:
 
 checkItem.addSelectionListener(new checkMenuClickListener(checkItem));    
 
 I have passed a hanler to the constructor?
 
 Thanks again,
 Sam     

What does "code failed" in this case mean? Access violation? can you show a stacktrace? can you post a minimized example that is compilable (preferable one file) which i can try?
Aug 08 2008
parent reply Sam Hu <samhu.samhu gmail.com> writes:
Content-Type: text/plain

Thanks Frank .

I rewrote the program as the attached.But neither menuItem 'Checked Option' or
ToolbarItem 'Check Bar' work properly.when click on it,the app quit.

Thanks for your help.
Sam
Aug 08 2008
parent reply Sam Hu <samhu.samhu gmail.com> writes:
Again,I can not download the file I uploaded.So I post here:
/****************************************************/
module ToolBarApp;

import dwt.widgets.Widget;
import dwt.widgets.Shell;
import dwt.widgets.Display;
import dwt.widgets.Menu;
import dwt.widgets.MenuItem;
import dwt.widgets.ToolBar;
import dwt.widgets.ToolItem;
import dwt.widgets.MessageBox;
import dwt.DWT;
import dwt.events.SelectionEvent;
import dwt.events.SelectionListener;


import tango.io.Stdout;

public class Form
{
	private:
	Display display;
	Shell shell;
	Menu menu;
	MenuItem checkItem;
	
	ToolBar bar;
	ToolItem checkBarItem;
	
	
	public:
	this()
	{
		initializeComponents;
	}
	
	void initializeComponents()
	{
		display=new Display;
		shell =new Shell(display);
		
		shell.setSize(500,500);
		
		shell.setText("Example 01");
		
		//ToolBar:
		bar=createToolBar(checkBarItem);
		
		//main menu:
		menu=createMainMenu;
		shell.setMenuBar(menu);

		shell.open;
	
		while(!shell.isDisposed)
		{
			if(!display.readAndDispatch)
			display.sleep;
		}
		display.dispose;

		
	}
	
	private:
	Menu createMainMenu()
	{
		//Menu-->Option-->Checkd Option
		menu=new Menu(shell,DWT.BAR);
	
		final MenuItem options=new MenuItem(menu,DWT.CASCADE);
		options.setText("Options");
		final Menu optionMenu=new Menu(shell,DWT.DROP_DOWN);
		
		final MenuItem checkItem=new MenuItem(optionMenu,DWT.CHECK);
		checkItem.setText("Checked Option");
		checkItem.setAccelerator(DWT.CTRL+'C');
		checkItem.setSelection(true);
		options.setMenu(optionMenu);

	
		checkItem.addSelectionListener(new checkMenuClickListener(checkItem));	
		
		return menu;
	}
	
	ToolBar createToolBar(ref ToolItem checkBarItem)
	{
		
		final ToolBar bar =new ToolBar(shell,DWT.HORIZONTAL);
		bar.setSize(300,70);
		bar.setLocation(0,0);
		
		
		checkBarItem=new ToolItem(bar,DWT.CHECK);
		
		checkBarItem.setText("Check Bar");
		
		checkBarItem.addSelectionListener(new checkMenuClickListener(checkBarItem));
		
		
		return bar;
	}
	
	
	class checkMenuClickListener:SelectionListener
	{
		private:
		MenuItem item;
		ToolItem toolItem;
		public:
		this(MenuItem item)
		{
			
			this.item=item;
			
		}
		this(ToolItem toolItem)
		{
			this.toolItem=toolItem;
		}
		
		public void widgetSelected(SelectionEvent e)
		{
			if(checkItem.getSelection||checkBarItem.getSelection)
			{
				MessageBox.showInfo("Checked","HAHA");
			}
			else
				
				MessageBox.showInfo("Unchecked","HAHA");
			
			
				
		}
		public void widgetDefaultSelected(SelectionEvent e)
		{
			//leave blank
		}
	}
}

	int main(char[][] args)
	{
		scope Form form=new Form;
		
		return 0;
	}

/****************************************************/
Aug 08 2008
parent reply Sam Hu <samhu.samhu gmail.com> writes:
So sorry!!A mistake in the code,please refer to  below code:

/******************************************/
module ToolBarApp;

import dwt.widgets.Widget;
import dwt.widgets.Shell;
import dwt.widgets.Display;
import dwt.widgets.Menu;
import dwt.widgets.MenuItem;
import dwt.widgets.ToolBar;
import dwt.widgets.ToolItem;
import dwt.widgets.MessageBox;
import dwt.DWT;
import dwt.events.SelectionEvent;
import dwt.events.SelectionListener;


import tango.io.Stdout;

public class Form
{
	private:
	Display display;
	Shell shell;
	Menu menu;
	MenuItem checkItem;
	
	ToolBar bar;
	ToolItem checkBarItem;
	
	
	public:
	this()
	{
		initializeComponents;
	}
	
	void initializeComponents()
	{
		display=new Display;
		shell =new Shell(display);
		
		shell.setSize(500,500);
		
		shell.setText("Example 01");
		
		//ToolBar:
		bar=createToolBar(checkBarItem);
		
		//main menu:
		menu=createMainMenu;
		shell.setMenuBar(menu);

		shell.open;
	
		while(!shell.isDisposed)
		{
			if(!display.readAndDispatch)
			display.sleep;
		}
		display.dispose;

		
	}
	
	private:
	Menu createMainMenu()
	{
		//Menu-->Option-->Checkd Option
		menu=new Menu(shell,DWT.BAR);
	
		final MenuItem options=new MenuItem(menu,DWT.CASCADE);
		options.setText("Options");
		final Menu optionMenu=new Menu(shell,DWT.DROP_DOWN);
		
		final MenuItem checkItem=new MenuItem(optionMenu,DWT.CHECK);
		checkItem.setText("Checked Option");
		checkItem.setAccelerator(DWT.CTRL+'C');
		checkItem.setSelection(true);
		options.setMenu(optionMenu);

	
		checkItem.addSelectionListener(new checkMenuClickListener);	
		
		return menu;
	}
	
	ToolBar createToolBar(ref ToolItem checkBarItem)
	{
		
		final ToolBar bar =new ToolBar(shell,DWT.HORIZONTAL);
		bar.setSize(300,70);
		bar.setLocation(0,0);
		
		
		checkBarItem=new ToolItem(bar,DWT.CHECK);
		
		checkBarItem.setText("Check Bar");
		
		checkBarItem.addSelectionListener(new checkMenuClickListener);
		
		
		return bar;
	}
	
	
	class checkMenuClickListener:SelectionListener
	{
		
		private:
		MenuItem checkItem;
		ToolItem toolItem;
		public:
		this(MenuItem item)
		{
			
			this.checkItem=checkItem;
			
		}
		this(ToolItem toolItem)
		{
			this.toolItem=toolItem;
		}
		
		
		public void widgetSelected(SelectionEvent e)
		{
			if(checkItem.getSelection||checkBarItem.getSelection)
			{
				MessageBox.showInfo("Checked","HAHA");
			}
			else
				
				MessageBox.showInfo("Unchecked","HAHA");
			
			
				
		}
		public void widgetDefaultSelected(SelectionEvent e)
		{
			//leave blank
		}
	}
}

	int main(char[][] args)
	{
		scope Form form=new Form;
		
		return 0;
	}
/****************************************************/
Aug 08 2008
parent Frank Benoit <keinfarbton googlemail.com> writes:
import dwt.widgets.Widget;
import dwt.widgets.Shell;
import dwt.widgets.Display;
import dwt.widgets.Menu;
import dwt.widgets.MenuItem;
import dwt.widgets.ToolBar;
import dwt.widgets.ToolItem;
import dwt.widgets.MessageBox;
import dwt.DWT;
import dwt.events.SelectionEvent;
import dwt.events.SelectionListener;
import tango.io.Stdout;

public class Form {
     private:
     Display display;
     Shell shell;
     Menu menu;
     MenuItem checkItem;
     ToolBar bar;
     ToolItem checkBarItem;
     public:
     this() {
         initializeComponents;
     }
     void initializeComponents() {
         display=new Display;
         shell =new Shell(display);
         shell.setSize(500,500);
         shell.setText("Example 01");
         //ToolBar:
         bar=createToolBar(checkBarItem);
         //main menu:
         menu=createMainMenu;
         shell.setMenuBar(menu);
         shell.open;
         while(!shell.isDisposed) {
             if(!display.readAndDispatch)
             display.sleep;
         }
         display.dispose;
     }
     private:
     Menu createMainMenu() {
         //Menu-->Option-->Checkd Option
         menu=new Menu(shell,DWT.BAR);
         final MenuItem options=new MenuItem(menu,DWT.CASCADE);
         options.setText("Options");
         final Menu optionMenu=new Menu(shell,DWT.DROP_DOWN);
         final MenuItem checkItem=new MenuItem(optionMenu,DWT.CHECK);
         checkItem.setText("Checked Option");
         checkItem.setAccelerator(DWT.CTRL+'C');
         checkItem.setSelection(true);
         options.setMenu(optionMenu);
         checkItem.addSelectionListener(new 
checkMenuClickListener(checkItem));
         return menu;
     }
     ToolBar createToolBar(ref ToolItem checkBarItem) {
         final ToolBar bar =new ToolBar(shell,DWT.HORIZONTAL);
         bar.setSize(300,70);
         bar.setLocation(0,0);
         checkBarItem=new ToolItem(bar,DWT.CHECK);
         checkBarItem.setText("Check Bar");
         checkBarItem.addSelectionListener(new 
checkMenuClickListener(checkBarItem));
         return bar;
     }
     class checkMenuClickListener : SelectionListener {
         private:
         MenuItem checkItem;
         ToolItem toolItem;
         public:
         this(MenuItem item) {
             this.checkItem=checkItem;
         }
         this(ToolItem toolItem) {
             this.toolItem=toolItem;
         }
         public void widgetSelected(SelectionEvent e) {
             if( checkItem !is null ){
                 if(checkItem.getSelection||checkBarItem.getSelection) {
                     MessageBox.showInfo("Checked","HAHA");
                 }
                 else{
                     MessageBox.showInfo("Unchecked","HAHA");
                 }
             }
         }
         public void widgetDefaultSelected(SelectionEvent e) {
             //leave blank
         }
     }
}
int main(char[][] args) {
     scope Form form=new Form;
     return 0;
}
Aug 09 2008