Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Remove a button item from a table
Remove a button item from a table [message #633757] Tue, 19 October 2010 09:53 Go to next message
Andrea  is currently offline Andrea Friend
Messages: 7
Registered: October 2010
Junior Member
HI,
I have a problem with elimination of a row from a table. In the second and third column of my table there are buttons. The problem is very similiar to this http://dev.eclipse.org/newslists/news.eclipse.platform.swt/m sg18512.html
but I can't understand how I can eliminate this row.

Thanks,
Andrea.
Re: Remove a button item from a table [message #633835 is a reply to message #633757] Tue, 19 October 2010 14:47 Go to previous messageGo to next message
Andrea  is currently offline Andrea Friend
Messages: 7
Registered: October 2010
Junior Member
Any solution? I'm trying without success...
Re: Remove a button item from a table [message #633988 is a reply to message #633835] Wed, 20 October 2010 08:10 Go to previous messageGo to next message
Andrea  is currently offline Andrea Friend
Messages: 7
Registered: October 2010
Junior Member
I'm trying to set "button.setVisible(false);" but I've some proglem with refresh.
Re: Remove a button item from a table [message #634009 is a reply to message #633757] Wed, 20 October 2010 09:15 Go to previous messageGo to next message
Andrea  is currently offline Andrea Friend
Messages: 7
Registered: October 2010
Junior Member
/*******************************************************************************
 * Copyright (c) 2000, 2004 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package swtsnippets;
 
/*
 * Table example snippet: remove selected items (using popup menu)
 *
 * For a list of all SWT example snippets see
 * http://www.eclipse.org/swt/snippets/
 */
import org.eclipse.swt.*;
import org.eclipse.swt.custom.TableEditor;
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.*;

public class Snippet53 {

public static void main (String [] args) {
	final Display display = new Display();
	Shell shell = new Shell(display);
	shell.setLayout(new GridLayout());
	final Table table = new Table(shell, SWT.MULTI | SWT.BORDER
			| SWT.FULL_SELECTION);
	GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
	data.heightHint = 200;
	table.setLayoutData(data);
	table.setLinesVisible(true);
	table.setHeaderVisible(true);
	
	createColumns(table);
	
	for (int i=0; i<8; i++) {
		TableItem item = new TableItem (table, SWT.NONE);
		item.setText (0,"Item " + i);
		item.setText (1,"Cane " + i);
		
		Button c = new Button(table, SWT.PUSH);
		c.setText("B"+i);
		
		c.setEnabled(true);
		TableEditor editor = new TableEditor(table);
		editor.grabHorizontal = editor.grabVertical = true;
		editor.setEditor(c, item, 2);
//		editor.setEditor(null, item, 2);
		
		item.setData("IO", editor);
	}
	Menu menu = new Menu (shell, SWT.POP_UP);
	table.setMenu (menu);
	MenuItem item = new MenuItem (menu, SWT.PUSH);
	item.setText ("Power Off");
	item.addListener (SWT.Selection, new Listener () {
		public void handleEvent (Event event) {
			TableItem ti = table.getItem(table.getSelectionIndex());
			TableEditor b = (TableEditor)ti.getData("IO");
			
			redispose(table,b);
			table.remove (table.getSelectionIndex());
		}


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

private static void redispose(Table table, TableEditor b) {
	Control [] controls = table.getChildren();
	System.out.println("->"+controls.length);
	for (int i = 0; i < controls.length; i++) {
		if(controls[i].equals(b.getEditor())){
			controls[i].setVisible(false);
			System.out.println("Bingo");
		}
	}
}

private static void resizeColumns(Table table) {
	for (int i=0; i<3; i++) {
		table.getColumn (i).pack ();
		System.out.println(table.getHeaderHeight()); 
	}
	
}

private static void createColumns(Table table) {
	// Create five columns
      TableColumn column = new TableColumn(table, SWT.CENTER);
      column.setText("Prima");
      
      column = new TableColumn(table, SWT.CENTER);
      column.setText("MAC Address");
      
      column = new TableColumn(table, SWT.CENTER);
      column.setText("Parked");
}
} 
Re: Remove a button item from a table [message #634126 is a reply to message #634009] Wed, 20 October 2010 15:27 Go to previous messageGo to next message
Lakshmi P ShanmugamFriend
Messages: 279
Registered: July 2009
Location: India
Senior Member
Hi,

'refresh' happens when TableEditor.layout() is called for *all* the tableEditors and it gets called when the table gets any of these events - SWT.KeyDown, SWT.KeyUp, SWT.MouseDown, SWT.MouseUp, SWT.Resize. So, after the item is removed, you can send any of these events using notifyListeners() .
Also, you can dispose of the button instead of making it invisible.
For eg:
item.addListener (SWT.Selection, new Listener () {
public void handleEvent (Event event) {
table.remove (table.getSelectionIndex());
table.notifyListeners(SWT.Resize, new Event());
}
});

Here is a different approach which you could try out to remove the button from the table.
When the table item is removed, it sends dispose event. You can then dispose the button and editor in the dispose listener of the tableitem.

for (int i=0; i<10; i++) {
TableItem item = new TableItem (table, SWT.NONE);
final Button button1 = new Button(table, SWT.CHECK);
final TableEditor editor = new TableEditor (table);
editor.grabHorizontal = editor.grabVertical = true;
editor.setEditor (button1, item, 2);

item.addDisposeListener(new DisposeListener() {
public void widgetDisposed(DisposeEvent e) {
button1.dispose();
editor.dispose();
}
});
}


Lakshmi P Shanmugam
Re: Remove a button item from a table [message #634293 is a reply to message #634126] Thu, 21 October 2010 07:15 Go to previous messageGo to next message
Daniel Krügler is currently offline Daniel KrüglerFriend
Messages: 853
Registered: July 2009
Senior Member
I was following this thread and would like to comment on it.

On 20.10.2010 17:27, Lakshmi Shanmugam wrote:
> 'refresh' happens when TableEditor.layout() is called for *all* the
> tableEditors and it gets called when the table gets any of these events
> - SWT.KeyDown, SWT.KeyUp, SWT.MouseDown, SWT.MouseUp, SWT.Resize. So,
> after the item is removed, you can send any of these events using
> notifyListeners() .
> Also, you can dispose of the button instead of making it invisible.
> For eg:
> item.addListener (SWT.Selection, new Listener () {
> public void handleEvent (Event event) {
> table.remove (table.getSelectionIndex());
> table.notifyListeners(SWT.Resize, new Event());
> }
> });

Yes, this approach seems to work, tested in a Win32 environment.

> Here is a different approach which you could try out to remove the
> button from the table.
> When the table item is removed, it sends dispose event. You can then
> dispose the button and editor in the dispose listener of the tableitem.
>
> for (int i=0; i<10; i++) {
> TableItem item = new TableItem (table, SWT.NONE);
> final Button button1 = new Button(table, SWT.CHECK);
> final TableEditor editor = new TableEditor (table);
> editor.grabHorizontal = editor.grabVertical = true;
> editor.setEditor (button1, item, 2);
>
> item.addDisposeListener(new DisposeListener() {
> public void widgetDisposed(DisposeEvent e) {
> button1.dispose();
> editor.dispose();
> }
> });
> }

Yes, this was my natural way of implementing it, too. But it seems
odd, that even with this code added, you need the explicit call of

table.notifyListeners(SWT.Resize, new Event());

following the call of Table.remove. Is this really intended? It looks
like an SWT bug to me that user code is required to invoke an explicit
resize event here.

Greetings from Bremen,

- Daniel
Re: Remove a button item from a table [message #634386 is a reply to message #634293] Thu, 21 October 2010 14:04 Go to previous messageGo to next message
Lakshmi P ShanmugamFriend
Messages: 279
Registered: July 2009
Location: India
Senior Member
Hi Daniel,

The problem here is that the TableItems are moved but the TableEditors are not moved when the TableItem is removed form the table. This doesn't happen automatically because the Table is not aware of its TableEditors. So, explicit notification is required so that the layout is called for each of the TableEditors.
I'm not sure if it should be considered as a bug.

Regards,


Lakshmi P Shanmugam
Re: Remove a button item from a table [message #634403 is a reply to message #634386] Thu, 21 October 2010 15:05 Go to previous message
Daniel Krügler is currently offline Daniel KrüglerFriend
Messages: 853
Registered: July 2009
Senior Member
On 21.10.2010 16:04, Lakshmi Shanmugam wrote:
> Hi Daniel,
>
> The problem here is that the TableItems are moved but the TableEditors
> are not moved when the TableItem is removed form the table. This doesn't
> happen automatically because the Table is not aware of its TableEditors.
> So, explicit notification is required so that the layout is called for
> each of the TableEditors.
> I'm not sure if it should be considered as a bug.

I accept and agree that the Table is not necessarily aware of
TableEditors, but aren't the TableEditors aware of the Table?
If so, the dispose step of the TableEditor could automagically
send this message to the Table. Are there situation where this
would be an unwanted behavior?

Greetings from Bremen,

Daniel Krügler
Previous Topic:Preventing TreeItem to expand/collapse
Next Topic:Will Apple allow SWT on their new Mac App Store?
Goto Forum:
  


Current Time: Sat Apr 20 04:33:58 GMT 2024

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

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

Back to the top