Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Remove a button item from a table
| | |
Re: Remove a button item from a table [message #634009 is a reply to message #633757] |
Wed, 20 October 2010 05:15   |
Eclipse User |
|
|
|
/*******************************************************************************
* 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 11:27   |
Eclipse User |
|
|
|
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();
}
});
}
|
|
|
Re: Remove a button item from a table [message #634293 is a reply to message #634126] |
Thu, 21 October 2010 03:15   |
Eclipse User |
|
|
|
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 #634403 is a reply to message #634386] |
Thu, 21 October 2010 11:05  |
Eclipse User |
|
|
|
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
|
|
|
Goto Forum:
Current Time: Mon Jul 07 14:56:33 EDT 2025
Powered by FUDForum. Page generated in 0.05081 seconds
|