Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Cannot remove first item when using TableEditor and Table.Remove(0) is used
Cannot remove first item when using TableEditor and Table.Remove(0) is used [message #653824] Fri, 11 February 2011 12:54 Go to next message
M. A. Tanaka is currently offline M. A. TanakaFriend
Messages: 2
Registered: February 2011
Junior Member
Hi

I am unsure if it is a bug or not, but the behaviour is definitely strange for me.

I am trying to implement a table where rows can be removed by pressing a button. However I have some problems,
when TableItems to be removed contains TableEditors. I use the Table.remove(int) method to remove the TableItems.

When 0 is given as argument to the remove method, then I would have expected first row to be removed, however
the second row is removed.

If I continue to invoke remove with the argument 0, all rows are removed except the first row.
However Table.getItemCount is decrementd until it reaches 0 however first row is still visible.

When -1 is given an exception occurs, which I would have expected.

When 1 is given the third row is removed instead of second row.

When tableItem() - 2 is given as argument the last item is being removed (I would have expected the second last item to be removed)
if I continue invoke remove with tableItem() - 2 then no more rows are removed

When tableItem() - 1 is given as argument nothing happens (I would have expected the last item to be removed)
if I continue invoke remove with tableItem() - 1 nothing happens

When tableItem() is given as argument an exception occurs (I would have expected)


The interesting thing is if table items, which only has String items is used, then it behaves as expected
i.e. if 0 is given the first item is removed until it reaches zero,
and if table.getItemCount() - 1 is given it is the last item that is removed as expected

I have attached an example where I create 4 rows, where each created TableItem has a TableEditor with a Text widget.
The Text Widget contains the row index.

There is an additional method createItemUsingStringOnly, which creates TableItem that has text only.

I am running on 32 bit Windows XP and SWT 3.6.1.

I hope that someone can help me

thanks in advance

import java.util.Calendar;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.TableEditor;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;

public class Main {
private static final int COLUMN_WIDTH = 100;

public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new GridLayout());

Composite composite = new Composite(shell, SWT.NONE);
composite.setLayout(new GridLayout());
composite.setLayoutData(createGridDataForComposite());

final Table table = new Table(composite, SWT.V_SCROLL | SWT.H_SCROLL | SWT.FULL_SELECTION);
table.setSize(300, 100);
table.setHeaderVisible(true);
createColumns(table);

for (int i = 0; i < 4; i++) {
createItem(table);
// createItemUsingStringOnly(table);
}

addRemoveButtonNegativeOne(composite, table);

table.layout(true);
table.pack();

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

private static void addRemoveButtonNegativeOne(Composite composite, final Table table) {
createDeleteButton(composite, table, "delete by -1", new Getter() {
@Override
public int get(Table table) {
return -1;
}
});

createDeleteButton(composite, table, "delete by 0", new Getter() {
@Override
public int get(Table table) {
return 0;
}
});

createDeleteButton(composite, table, "delete by 1", new Getter() {
@Override
public int get(Table table) {
return 1;
}
});

createDeleteButton(composite, table, "delete by itemCount()-2", new Getter() {
@Override
public int get(Table table) {
return table.getItemCount() - 2;
}
});

createDeleteButton(composite, table, "delete by itemCount()-1", new Getter() {
@Override
public int get(Table table) {
return table.getItemCount() - 1;
}
});

createDeleteButton(composite, table, "delete by itemCount()", new Getter() {
@Override
public int get(Table table) {
return table.getItemCount();
}
});
}

private static void createDeleteButton(Composite composite, final Table table, String buttonText, final Getter getter) {
Button button = new Button(composite, SWT.NONE);
button.setText(buttonText);
button.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
int length = table.getItems().length;
System.out.println("Table lenght: " + length);
if(length > 0) {
table.remove(getter.get(table));
table.notifyListeners(SWT.Resize, new Event());
}
}
});
}

static interface Getter {
int get(Table table);
}



private static void addAddButton(Composite composite, final Table table) {
Button button = new Button(composite, SWT.NONE);
button.setText("add");
button.addSelectionListener(new SelectionAdapter() {

@Override
public void widgetSelected(SelectionEvent e) {
table.showItem(createItem(table));
table.notifyListeners(SWT.Resize, new Event());
}
});
}

private static TableItem createItem(final Table table) {
int index = table.getItemCount();
TableItem tableItem = new TableItem(table, SWT.NONE);
final TableEditor editor = new TableEditor (table);
Text text = new Text(table, SWT.NONE);
text.setText(String.valueOf(Calendar.getInstance().getTimeIn Millis()));
text.setToolTipText(String.valueOf(Calendar.getInstance().ge tTimeInMillis()));
text.pack();

editor.minimumWidth = COLUMN_WIDTH;
editor.setEditor(text, tableItem, 0);

final TableEditor editor1 = new TableEditor (table);
Text text1 = new Text(table, SWT.NONE);
text1.setText(String.valueOf(index));
text1.setToolTipText(String.valueOf(index));
text1.pack();

editor1.minimumWidth = COLUMN_WIDTH;
editor1.setEditor(text1, tableItem, 1);


tableItem.addDisposeListener(new DisposeListener() {

@Override
public void widgetDisposed(DisposeEvent e) {
System.out.println("Disposing: " + editor1.getEditor().getToolTipText());
editor.dispose();
editor1.dispose();
}
});

return tableItem;
}

private static TableItem createItemUsingStringOnly(final Table table) {
int itemCount = table.getItemCount();
TableItem tableItem = new TableItem(table, SWT.NONE);
tableItem.setText(String.valueOf(itemCount));

return tableItem;
}

private static void createColumns(final Table table) {
for(int i = 0; i < 2; ++i) {
TableColumn tableColumn = new TableColumn(table, SWT.BORDER);
tableColumn.setText(String.valueOf(i));
tableColumn.setWidth(COLUMN_WIDTH);
}
}

private static GridData createGridDataForComposite() {
GridData layoutData = new GridData();
layoutData.heightHint = 400;
layoutData.widthHint = 300;
return layoutData;
}
}


[Updated on: Fri, 11 February 2011 12:55]

Report message to a moderator

Re: Cannot remove first item when using TableEditor and Table.Remove(0) is used [message #654692 is a reply to message #653824] Wed, 16 February 2011 17:22 Go to previous messageGo to next message
Grant Gayed is currently offline Grant GayedFriend
Messages: 2150
Registered: July 2009
Senior Member
Hi,

The editors don't auto-update their position in response to changes in the
underlying Table, so you need to call layout() on them when this happens.
I've re-pasted your snippet below with a few added lines marked as "//
ADDED" which takes care of this. Also, the Texts need to be disposed in
addition to their editors (see the javadoc of TableEditor.dispose()).

import java.util.Calendar;
import java.util.Enumeration;
import java.util.Vector;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.TableEditor;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;

public class Main {
private static final int COLUMN_WIDTH = 100;
static Vector tableEditors = new Vector(); // ADDED

public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new GridLayout());

Composite composite = new Composite(shell, SWT.NONE);
composite.setLayout(new GridLayout());
composite.setLayoutData(createGridDataForComposite());

final Table table = new Table(composite, SWT.V_SCROLL | SWT.H_SCROLL
| SWT.FULL_SELECTION);
table.setSize(300, 100);
table.setHeaderVisible(true);
createColumns(table);

for (int i = 0; i < 4; i++) {
createItem(table);
// createItemUsingStringOnly(table);
}

addRemoveButtonNegativeOne(composite, table);

table.layout(true);
table.pack();

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

private static void addRemoveButtonNegativeOne(Composite composite,
final Table table) {
createDeleteButton(composite, table, "delete by -1", new Getter() {
@Override
public int get(Table table) {
return -1;
}
});

createDeleteButton(composite, table, "delete by 0", new Getter() {
@Override
public int get(Table table) {
return 0;
}
});

createDeleteButton(composite, table, "delete by 1", new Getter() {
@Override
public int get(Table table) {
return 1;
}
});

createDeleteButton(composite, table, "delete by itemCount()-2", new
Getter() {
@Override
public int get(Table table) {
return table.getItemCount() - 2;
}
});

createDeleteButton(composite, table, "delete by itemCount()-1", new
Getter() {
@Override
public int get(Table table) {
return table.getItemCount() - 1;
}
});

createDeleteButton(composite, table, "delete by itemCount()", new
Getter() {
@Override
public int get(Table table) {
return table.getItemCount();
}
});
}

private static void createDeleteButton(Composite composite, final Table
table, String buttonText, final Getter getter) {
Button button = new Button(composite, SWT.NONE);
button.setText(buttonText);
button.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
int length = table.getItems().length;
System.out.println("Table lenght: " + length);
if(length > 0) {
table.remove(getter.get(table));
table.notifyListeners(SWT.Resize, new Event());
}
}
});
}

static interface Getter {
int get(Table table);
}



private static void addAddButton(Composite composite, final Table table)
{
Button button = new Button(composite, SWT.NONE);
button.setText("add");
button.addSelectionListener(new SelectionAdapter() {

@Override
public void widgetSelected(SelectionEvent e) {
table.showItem(createItem(table));
table.notifyListeners(SWT.Resize, new Event());
}
});
}

private static TableItem createItem(final Table table) {
int index = table.getItemCount();
TableItem tableItem = new TableItem(table, SWT.NONE);
final TableEditor editor = new TableEditor (table);
final Text text = new Text(table, SWT.NONE);
text.setText(String.valueOf(Calendar.getInstance().getTimeIn Millis()));
text.setToolTipText(String.valueOf(Calendar.getInstance().ge tTimeInMillis()));
text.pack();

editor.minimumWidth = COLUMN_WIDTH;
editor.setEditor(text, tableItem, 0);

final TableEditor editor1 = new TableEditor (table);
final Text text1 = new Text(table, SWT.NONE);
text1.setText(String.valueOf(index));
text1.setToolTipText(String.valueOf(index));
text1.pack();

editor1.minimumWidth = COLUMN_WIDTH;
editor1.setEditor(text1, tableItem, 1);

tableEditors.add(editor); // ADDED
tableEditors.add(editor1); // ADDED
tableItem.addDisposeListener(new DisposeListener() {

@Override
public void widgetDisposed(DisposeEvent e) {
System.out.println("Disposing: " +
editor1.getEditor().getToolTipText());
editor.dispose();
editor1.dispose();
// ADDED
tableEditors.remove(editor);
tableEditors.remove(editor1);
text.dispose();
text1.dispose();
Enumeration enumerator = tableEditors.elements();
while (enumerator.hasMoreElements()) {
((TableEditor)enumerator.nextElement()).layout();
}

}
});

return tableItem;
}

private static TableItem createItemUsingStringOnly(final Table table) {
int itemCount = table.getItemCount();
TableItem tableItem = new TableItem(table, SWT.NONE);
tableItem.setText(String.valueOf(itemCount));

return tableItem;
}

private static void createColumns(final Table table) {
for(int i = 0; i < 2; ++i) {
TableColumn tableColumn = new TableColumn(table, SWT.BORDER);
tableColumn.setText(String.valueOf(i));
tableColumn.setWidth(COLUMN_WIDTH);
}
}

private static GridData createGridDataForComposite() {
GridData layoutData = new GridData();
layoutData.heightHint = 400;
layoutData.widthHint = 300;
return layoutData;
}
}

Grant


"M. A. Tanaka" <meang@post.com> wrote in message
news:ij3b7p$bum$1@news.eclipse.org...
> Hi
>
> I am unsure if it is a bug or not, but the behaviour is definitely strange
> for me.
>
> I am trying to implement a table where rows can be removed by pressing a
> button. However I have some problems,
> when TableItems to be removed contains TableEditors. I use the
> Table.remove(int) method to remove the TableItems.
>
> When 0 is given as argument to the remove method, then I would have
> expected first row to be removed, however the second row is removed.
> If I continue to invoke remove with the argument 0, all rows are removed
> except the first row. However Table.getItemCount is decrementd until it
> reaches 0 however first row is still visible.
>
> When -1 is given an exception occurs, which I would have expected.
>
> When 1 is given the third row is removed instead of second row.
>
> When tableItem() - 2 is given as argument the last item is being removed
> (I would have expected the second last item to be removed)
> if I continue invoke remove with tableItem() - 2 then no more rows are
> removed
>
> When tableItem() - 1 is given as argument nothing happens (I would have
> expected the last item to be removed)
> if I continue invoke remove with tableItem() - 1 nothing happens
>
> When tableItem() is given as argument an exception occurs (I would have
> expected)
>
>
> The interesting thing is if table items, which only has String items is
> used, then it behaves as expected i.e. if 0 is given the first item is
> removed until it reaches zero,
> and if table.getItemCount() - 1 is given it is the last item that is
> removed as expected
>
> I have attached an example where I create 4 rows, where each created
> TableItem has a TableEditor with a Text widget. The Text Widget contains
> the row index.
>
> There is an additional method createItemUsingStringOnly, which creates
> TableItem that has text only.
>
> I am running on 32 bit Windows XP and SWT 3.6.1.
>
> I hope that someone can help me
> thanks in advance
>
> import java.util.Calendar;
>
> import org.eclipse.swt.SWT;
> import org.eclipse.swt.custom.TableEditor;
> import org.eclipse.swt.events.DisposeEvent;
> import org.eclipse.swt.events.DisposeListener;
> import org.eclipse.swt.events.SelectionAdapter;
> import org.eclipse.swt.events.SelectionEvent;
> import org.eclipse.swt.layout.GridData;
> import org.eclipse.swt.layout.GridLayout;
> import org.eclipse.swt.widgets.Button;
> import org.eclipse.swt.widgets.Composite;
> import org.eclipse.swt.widgets.Display;
> import org.eclipse.swt.widgets.Event;
> import org.eclipse.swt.widgets.Shell;
> import org.eclipse.swt.widgets.Table;
> import org.eclipse.swt.widgets.TableColumn;
> import org.eclipse.swt.widgets.TableItem;
> import org.eclipse.swt.widgets.Text;
>
> public class Main {
> private static final int COLUMN_WIDTH = 100;
>
> public static void main(String[] args) {
> Display display = new Display();
> Shell shell = new Shell(display);
> shell.setLayout(new GridLayout());
> Composite composite = new Composite(shell, SWT.NONE);
> composite.setLayout(new GridLayout());
> composite.setLayoutData(createGridDataForComposite());
> final Table table = new Table(composite, SWT.V_SCROLL |
> SWT.H_SCROLL | SWT.FULL_SELECTION);
> table.setSize(300, 100);
> table.setHeaderVisible(true);
> createColumns(table);
> for (int i = 0; i < 4; i++) {
> createItem(table);
> // createItemUsingStringOnly(table);
> }
>
> addRemoveButtonNegativeOne(composite, table);
> table.layout(true);
> table.pack();
> shell.pack();
> shell.open();
> while (!shell.isDisposed()) {
> if (!display.readAndDispatch())
> display.sleep();
> }
> display.dispose();
> }
>
> private static void addRemoveButtonNegativeOne(Composite composite,
> final Table table) {
> createDeleteButton(composite, table, "delete by -1", new Getter() {
> @Override
> public int get(Table table) {
> return -1;
> }
> });
> createDeleteButton(composite, table, "delete by 0", new Getter() {
> @Override
> public int get(Table table) {
> return 0;
> }
> });
> createDeleteButton(composite, table, "delete by 1", new Getter() {
> @Override
> public int get(Table table) {
> return 1;
> }
> });
>
> createDeleteButton(composite, table, "delete by itemCount()-2", new
> Getter() {
> @Override
> public int get(Table table) {
> return table.getItemCount() - 2;
> }
> });
> createDeleteButton(composite, table, "delete by itemCount()-1", new
> Getter() {
> @Override
> public int get(Table table) {
> return table.getItemCount() - 1;
> }
> });
> createDeleteButton(composite, table, "delete by itemCount()", new
> Getter() {
> @Override
> public int get(Table table) {
> return table.getItemCount();
> }
> });
> }
>
> private static void createDeleteButton(Composite composite, final Table
> table, String buttonText, final Getter getter) {
> Button button = new Button(composite, SWT.NONE);
> button.setText(buttonText);
> button.addSelectionListener(new SelectionAdapter() {
> @Override
> public void widgetSelected(SelectionEvent e) {
> int length = table.getItems().length;
> System.out.println("Table lenght: " + length);
> if(length > 0) {
> table.remove(getter.get(table));
> table.notifyListeners(SWT.Resize, new Event());
> } }
> });
> }
>
> static interface Getter {
> int get(Table table);
> }
>
> private static void addAddButton(Composite composite, final Table
> table) {
> Button button = new Button(composite, SWT.NONE);
> button.setText("add");
> button.addSelectionListener(new SelectionAdapter() {
>
> @Override
> public void widgetSelected(SelectionEvent e) {
> table.showItem(createItem(table));
> table.notifyListeners(SWT.Resize, new Event());
> }
> });
> }
>
> private static TableItem createItem(final Table table) {
> int index = table.getItemCount();
> TableItem tableItem = new TableItem(table, SWT.NONE);
> final TableEditor editor = new TableEditor (table);
> Text text = new Text(table, SWT.NONE);
> text.setText(String.valueOf(Calendar.getInstance().getTimeIn
> Millis()));
> text.setToolTipText(String.valueOf(Calendar.getInstance().ge
> tTimeInMillis()));
> text.pack();
>
> editor.minimumWidth = COLUMN_WIDTH;
> editor.setEditor(text, tableItem, 0);
>
> final TableEditor editor1 = new TableEditor (table);
> Text text1 = new Text(table, SWT.NONE);
> text1.setText(String.valueOf(index));
> text1.setToolTipText(String.valueOf(index));
> text1.pack();
>
> editor1.minimumWidth = COLUMN_WIDTH;
> editor1.setEditor(text1, tableItem, 1);
>
> tableItem.addDisposeListener(new DisposeListener() {
> @Override
> public void widgetDisposed(DisposeEvent e) {
> System.out.println("Disposing: " +
> editor1.getEditor().getToolTipText());
> editor.dispose(); editor1.dispose(); }
> });
> return tableItem;
> }
>
> private static TableItem createItemUsingStringOnly(final Table table) {
> int itemCount = table.getItemCount();
> TableItem tableItem = new TableItem(table, SWT.NONE);
> tableItem.setText(String.valueOf(itemCount));
> return tableItem;
> }
> private static void createColumns(final Table table) {
> for(int i = 0; i < 2; ++i) {
> TableColumn tableColumn = new TableColumn(table, SWT.BORDER);
> tableColumn.setText(String.valueOf(i));
> tableColumn.setWidth(COLUMN_WIDTH);
> }
> }
>
> private static GridData createGridDataForComposite() {
> GridData layoutData = new GridData();
> layoutData.heightHint = 400;
> layoutData.widthHint = 300;
> return layoutData;
> }
> }
>
>
>
Re: Cannot remove first item when using TableEditor and Table.Remove(0) is used [message #655207 is a reply to message #654692] Fri, 18 February 2011 16:15 Go to previous message
M. A. Tanaka is currently offline M. A. TanakaFriend
Messages: 2
Registered: February 2011
Junior Member
That solved it. Thanks a lot

Previous Topic:TabItem contents problems
Next Topic:Combo look-and-feel on Windows 7
Goto Forum:
  


Current Time: Thu Apr 02 16:31:40 GMT 2020

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

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

Back to the top