Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » RefreshProblem: Adding new TableItem with TableEditor (New EditorControls only appear if Scrollbar is hit)
RefreshProblem: Adding new TableItem with TableEditor [message #645167] Wed, 15 December 2010 18:45 Go to next message
gerald r. is currently offline gerald r.
Messages: 5
Registered: December 2010
Junior Member
Hi,

i am currently working on a table which allows to add new raws.
I am using TableEditor to create Buttons in each raw.

It almost works, but the new controls don't appear.
Instead they apear after hitting the scrollbar or forcing a redraw in some other way.

layout(true) does not help.

Here an example:

package test;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.TableEditor;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;

public class SWTTest {
	private static Shell shell;

	public static void main(String[] args) {
	    Display display = new Display();
	    shell = new Shell(display);
	    //creating table
	    Table table = new Table(shell, SWT.BORDER | SWT.MULTI);
	    table.setLinesVisible(true);
		table.setHeaderVisible(true);
	    table.setSize(200, 200);
	    
	    //creating columns
	    addColumns(table);
	    //creating initial items
	    for (int i = 0; i < 12; i++) {
	      TableItem item = new TableItem(table, SWT.NONE);
	      item.setText(new String[]{"Item " + i,"_____"});
	      
	    }
	    //adding the TableEditors
	    addEditors(table);
	    
	    

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

	private static void addColumns(Table table) {
		TableColumn tc = new TableColumn(table,SWT.NULL);
		tc.setText("Name");
		tc.setWidth(100);
		tc = new TableColumn(table,SWT.NULL);
		tc.setText("And so");
		tc.setWidth(90);
	}

	private static void addEditors(final Table table) {
		for(TableItem item:table.getItems()){
			createEditor(table, item, false);

		}
		
	}
	protected static void createEditor(final Table table,TableItem item,boolean isNew){
		TableEditor editor = new TableEditor(table);
		Button button = new Button(table,SWT.PUSH);
		if(isNew){
			button.setText("-");
		}else{
			button.setText("+");
		}
		
		button.pack();
		button.setData("item",item);
		button.addSelectionListener(new SelectionListener(){

			public void widgetDefaultSelected(SelectionEvent e) {
			}

			public void widgetSelected(SelectionEvent e) {
				//if button gets pushed, create a new item 
				Button button = (Button)e.widget;
				TableItem item = (TableItem)button.getData("item");
				int index = table.indexOf(item);
				try{
					//add the new item
					addItem(table,index,item.getText(0));
					//try to force layout, doesnt help :(
					table.layout();

				}catch(Throwable t){
					t.printStackTrace();
				}
				
				
			}
			
		});
		editor.minimumWidth=button.getSize().x;
		editor.setEditor(button,item,1);
		//force the layout directly with the editor, doesnt help either
		editor.layout();
	
	}

	protected static void addItem(final Table table, int index,String formerLabel) {
		
		TableItem item = new TableItem(table, SWT.NONE, index+1);
		item.setText(new String[]{formerLabel + "new","_____"});

		createEditor(table, item, true);

		
		
	}
}



In the example the first "adding" works. But it only does beacause a scrollbar is created and so the table needs to redraw.

After that every new Control only apears if i force a redraw.

Its tested with Windows XP and the Eclipse version 3.3 and 3.5.

I hope someone can help me, because i really thing this must be a trivial thing. But i cant make it work Sad

all the best,
gerald
Re: RefreshProblem: Adding new TableItem with TableEditor [message #645373 is a reply to message #645167] Thu, 16 December 2010 19:04 Go to previous messageGo to next message
Lakshmi Shanmugam
Messages: 274
Registered: July 2009
Location: India
Senior Member
Hi,

When a new item is added all the editors have to be laid out again. And calling editor.layout or table.layout doesn't do this.
Please see this thread for a discussion on this behavior. --> http://www.eclipse.org/forums/index.php?t=msg&goto=63438 6&S=f1cb13e717d98051e4be42274f216e0d#msg_634126
Though the topic there is removing an item, the reason and workaround are the same for adding an item.

HTH,


Lakshmi P Shanmugam
Re: RefreshProblem: Adding new TableItem with TableEditor [message #645736 is a reply to message #645373] Mon, 20 December 2010 10:20 Go to previous message
gerald r. is currently offline gerald r.
Messages: 5
Registered: December 2010
Junior Member
wow, thanks.

by using "table.notifyListeners(SWT.Resize, new Event());" it works now. Smile

Previous Topic:GridLayout using Groups members
Next Topic:[Browser] How to know when setURL has finished
Goto Forum:
  


Current Time: Mon Oct 20 08:19:01 GMT 2014

Powered by FUDForum. Page generated in 0.02735 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software