RefreshProblem: Adding new TableItem with TableEditor [message #645167] |
Wed, 15 December 2010 13:45  |
Eclipse User |
|
|
|
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 
all the best,
gerald
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.03753 seconds