Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Delayed resize of TableEditor widgets
Delayed resize of TableEditor widgets [message #507268] Tue, 12 January 2010 17:03 Go to next message
Roger Shimada is currently offline Roger ShimadaFriend
Messages: 4
Registered: January 2010
Junior Member
I have a table where there is a Text with border in the second column and a Button in the third column using TableEditors.

There isn't enough height for the bordered Text and the Button, so I added an SWT.MeasureItem Listener to set the height.

The initial paint of the table does not seem to be using the Listener. 1.5 seconds after the initial paint, the bordered Text and Button are redrawn. I'm using Eclipse 3.5 on Windows XP.

What am I doing wrong?

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.TableEditor;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
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.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Listener;
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 TableEditorStuff {

public static void main(String args[]) {
Display display = new Display();
final Shell shell = new Shell(display);
shell.setText("Bounded Text and Button resize after 1.5 seconds");
shell.setBounds(100, 100, 600, 100);

final String PROJECTPATH_KEY = "ProjectPath";
final String TEXT_KEY = "Text";
final String projectPath[][] = new String[][] {
{ "project1", "" },
{ "project2", "" },
};
final int fudgeHeight;

Table table = new Table(shell, SWT.BORDER);
table.setHeaderVisible(true);
GridData gridData = new GridData();
gridData.horizontalSpan = 3;
GridLayout gridLayout = new GridLayout();
gridLayout.numColumns = 3;
// uncomment the next two lines and the initial paint is really ugly
// table.setLayout(gridLayout);
// table.setLayoutData(gridData);
TableColumn projColumn = new TableColumn(table, SWT.LEFT);
projColumn.setText("Project");
projColumn.setWidth(100);
TableColumn pathColumn = new TableColumn(table, SWT.LEFT);
pathColumn.setText("Enter a zip name here");
pathColumn.setWidth(400);
TableColumn btnColumn = new TableColumn(table, SWT.NONE);
btnColumn.setWidth(80);

fudgeHeight = shell.getFont().getFontData()[0].getHeight() * 5 / 2;
// Need extra height for border on pathText which helps browseButton too
table.addListener(SWT.MeasureItem, new Listener() {
public void handleEvent(Event event) {
event.height = fudgeHeight;
}
});
for (int i = 0; i < projectPath.length; ++i) {
TableItem item = new TableItem(table, SWT.NONE);

item.setText(0, projectPath[i][0]);

// resized after 1.5 seconds
Text pathText = new Text(table, SWT.SINGLE | SWT.BORDER);
pathText.setText(projectPath[i][1]);
pathText.setData(PROJECTPATH_KEY, new Integer(i));
pathText.addModifyListener(new ModifyListener() {
public void modifyText(ModifyEvent e) {
Text pathText = (Text) e.widget;
int i = ((Integer) pathText.getData(PROJECTPATH_KEY)).intValue();
projectPath[i][1] = pathText.getText();
}
});
pathText.pack();
TableEditor pathEdit = new TableEditor(table);
pathEdit.grabHorizontal = true;
// pathEdit.minimumHeight = pathText.getSize().y;
pathEdit.setEditor(pathText, item, 1);

// resized after 1.5 seconds
Button browseButton = new Button(table, SWT.NONE);
browseButton.setText("Browse...");
browseButton.setData(TEXT_KEY, pathText);
browseButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
FileDialog fileDlog = new FileDialog(shell, SWT.SAVE);
fileDlog.setFilterExtensions(new String[] { "*.zip"});
String path = fileDlog.open();
if (path != null)
((Text) e.widget.getData(TEXT_KEY)).setText(path); // fires pathText's modifyText()
}
});
browseButton.pack();
TableEditor browseEdit = new TableEditor(table);
// browseEdit.minimumHeight = browseButton.getSize().y;
browseEdit.minimumWidth = browseButton.getSize().x;
browseEdit.setEditor(browseButton, item, 2);
}
table.pack();

shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
shell.dispose();
}
}
Re: Delayed resize of TableEditor widgets [message #507772 is a reply to message #507268] Thu, 14 January 2010 15:29 Go to previous messageGo to next message
Grant Gayed is currently offline Grant GayedFriend
Messages: 2150
Registered: July 2009
Senior Member
Hi,

The MeasureItem listener is called once Shell.open() has been invoked, but
by this point the editors have already set their initial heights based on
the Table's default item height. The heights self-correct 1.5s later
because the editors have timers that re-layout at this interval, presumably
because there's no way for them to be notified when a Table's item height
changes.

So a modified version of your snippet is shown below, which creates the
editors >after< the Table's item height has been changed from its default by
the MeasureItem listener. An alternative approach would have been to call
layout() on each of the TableEditors after invoking Shell.open().

public class TableEditorStuff {
public static void main(String args[]) {
Display display = new Display();
final Shell shell = new Shell(display);
shell.setText("Bounded Text and Button resize after 1.5 seconds");
shell.setBounds(100, 100, 600, 100);

final String PROJECTPATH_KEY = "ProjectPath";
final String TEXT_KEY = "Text";
final String projectPath[][] = new String[][] { { "project1", "" }, {
"project2", "" }, };
final int fudgeHeight;

Table table = new Table(shell, SWT.BORDER);
table.setHeaderVisible(true);
GridData gridData = new GridData();
gridData.horizontalSpan = 3;
GridLayout gridLayout = new GridLayout();
gridLayout.numColumns = 3;
TableColumn projColumn = new TableColumn(table, SWT.LEFT);
projColumn.setText("Project");
projColumn.setWidth(100);
TableColumn pathColumn = new TableColumn(table, SWT.LEFT);
pathColumn.setText("Enter a zip name here");
pathColumn.setWidth(400);
TableColumn btnColumn = new TableColumn(table, SWT.NONE);
btnColumn.setWidth(80);

fudgeHeight = shell.getFont().getFontData()[0].getHeight() * 5 / 2;
// Need extra height for border on pathText which helps browseButton too
table.addListener(SWT.MeasureItem, new Listener() {
public void handleEvent(Event event) {
event.height = fudgeHeight;
}
});
for (int i = 0; i < projectPath.length; ++i) {
new TableItem(table, SWT.NONE);
}
table.pack();
shell.open();
for (int i = 0; i < projectPath.length; ++i) {
TableItem item = table.getItem(i);

item.setText(0, projectPath[i][0]);

// resized after 1.5 seconds
Text pathText = new Text(table, SWT.SINGLE | SWT.BORDER);
pathText.setText(projectPath[i][1]);
pathText.setData(PROJECTPATH_KEY, new Integer(i));
pathText.addModifyListener(new ModifyListener() {
public void modifyText(ModifyEvent e) {
Text pathText = (Text) e.widget;
int i = ((Integer)
pathText.getData(PROJECTPATH_KEY)).intValue();
projectPath[i][1] = pathText.getText();
}
});
pathText.pack();
TableEditor pathEdit = new TableEditor(table);
pathEdit.grabHorizontal = true;
// pathEdit.minimumHeight = pathText.getSize().y;
pathEdit.setEditor(pathText, item, 1);

// resized after 1.5 seconds
Button browseButton = new Button(table, SWT.NONE);
browseButton.setText("Browse...");
browseButton.setData(TEXT_KEY, pathText);
browseButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
FileDialog fileDlog = new FileDialog(shell, SWT.SAVE);
fileDlog.setFilterExtensions(new String[] { "*.zip" });
String path = fileDlog.open();
if (path != null) ((Text)
e.widget.getData(TEXT_KEY)).setText(path);
}
});
browseButton.pack();
TableEditor browseEdit = new TableEditor(table);
// browseEdit.minimumHeight = browseButton.getSize().y;
browseEdit.minimumWidth = browseButton.getSize().x;
browseEdit.setEditor(browseButton, item, 2);
}
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) display.sleep();
}
display.dispose();
}
}

Grant


"Roger Shimada" <roger.shimada@us.lawson.com> wrote in message
news:hiia1q$1pf$1@build.eclipse.org...
> I have a table where there is a Text with border in the second column and
a Button in the third column using TableEditors.
>
> There isn't enough height for the bordered Text and the Button, so I added
an SWT.MeasureItem Listener to set the height.
>
> The initial paint of the table does not seem to be using the Listener.
1.5 seconds after the initial paint, the bordered Text and Button are
redrawn. I'm using Eclipse 3.5 on Windows XP.
>
> What am I doing wrong?
>
> import org.eclipse.swt.SWT;
> import org.eclipse.swt.custom.TableEditor;
> import org.eclipse.swt.events.ModifyEvent;
> import org.eclipse.swt.events.ModifyListener;
> 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.Display;
> import org.eclipse.swt.widgets.Event;
> import org.eclipse.swt.widgets.FileDialog;
> import org.eclipse.swt.widgets.Listener;
> 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 TableEditorStuff {
>
> public static void main(String args[]) {
> Display display = new Display();
> final Shell shell = new Shell(display);
> shell.setText("Bounded Text and Button resize after 1.5 seconds");
> shell.setBounds(100, 100, 600, 100);
>
> final String PROJECTPATH_KEY = "ProjectPath";
> final String TEXT_KEY = "Text";
> final String projectPath[][] = new String[][] {
> { "project1", "" },
> { "project2", "" },
> };
> final int fudgeHeight;
>
> Table table = new Table(shell, SWT.BORDER);
> table.setHeaderVisible(true);
> GridData gridData = new GridData();
> gridData.horizontalSpan = 3;
> GridLayout gridLayout = new GridLayout();
> gridLayout.numColumns = 3;
> // uncomment the next two lines and the initial paint is really ugly
> // table.setLayout(gridLayout);
> // table.setLayoutData(gridData);
> TableColumn projColumn = new TableColumn(table, SWT.LEFT);
> projColumn.setText("Project");
> projColumn.setWidth(100);
> TableColumn pathColumn = new TableColumn(table, SWT.LEFT);
> pathColumn.setText("Enter a zip name here");
> pathColumn.setWidth(400);
> TableColumn btnColumn = new TableColumn(table, SWT.NONE);
> btnColumn.setWidth(80);
>
> fudgeHeight = shell.getFont().getFontData()[0].getHeight() * 5 / 2;
> // Need extra height for border on pathText which helps browseButton too
> table.addListener(SWT.MeasureItem, new Listener() {
> public void handleEvent(Event event) {
> event.height = fudgeHeight;
> }
> });
> for (int i = 0; i < projectPath.length; ++i) {
> TableItem item = new TableItem(table, SWT.NONE);
>
> item.setText(0, projectPath[i][0]);
>
> // resized after 1.5 seconds
> Text pathText = new Text(table, SWT.SINGLE | SWT.BORDER);
> pathText.setText(projectPath[i][1]);
> pathText.setData(PROJECTPATH_KEY, new Integer(i));
> pathText.addModifyListener(new ModifyListener() {
> public void modifyText(ModifyEvent e) {
> Text pathText = (Text) e.widget;
> int i = ((Integer) pathText.getData(PROJECTPATH_KEY)).intValue();
> projectPath[i][1] = pathText.getText();
> }
> });
> pathText.pack();
> TableEditor pathEdit = new TableEditor(table);
> pathEdit.grabHorizontal = true;
> // pathEdit.minimumHeight = pathText.getSize().y;
> pathEdit.setEditor(pathText, item, 1);
>
> // resized after 1.5 seconds
> Button browseButton = new Button(table, SWT.NONE);
> browseButton.setText("Browse...");
> browseButton.setData(TEXT_KEY, pathText);
> browseButton.addSelectionListener(new SelectionAdapter() {
> public void widgetSelected(SelectionEvent e) {
> FileDialog fileDlog = new FileDialog(shell, SWT.SAVE);
> fileDlog.setFilterExtensions(new String[] { "*.zip"});
> String path = fileDlog.open();
> if (path != null)
> ((Text) e.widget.getData(TEXT_KEY)).setText(path); // fires pathText's
modifyText()
> }
> });
> browseButton.pack();
> TableEditor browseEdit = new TableEditor(table);
> // browseEdit.minimumHeight = browseButton.getSize().y;
> browseEdit.minimumWidth = browseButton.getSize().x;
> browseEdit.setEditor(browseButton, item, 2);
> }
> table.pack();
>
> shell.open();
> while (!shell.isDisposed()) {
> if (!display.readAndDispatch())
> display.sleep();
> }
> shell.dispose();
> }
> }
Re: Delayed resize of TableEditor widgets [message #507839 is a reply to message #507772] Thu, 14 January 2010 20:21 Go to previous messageGo to next message
Roger Shimada is currently offline Roger ShimadaFriend
Messages: 4
Registered: January 2010
Junior Member
Hi Grant,

Thanks a bunch for looking into this.

I didn't mention that I ran into this problem in a Wizard. So the Shell.open() in my posted code is part of the test setup.

I tried setting the minimumHeight fields in the TableEditors (with and without grabVertical) which didn't make a difference.

Any ideas about how to get the initial paint right on a Wizard page?
Re: Delayed resize of TableEditor widgets [message #508016 is a reply to message #507839] Fri, 15 January 2010 10:08 Go to previous messageGo to next message
Grant Gayed is currently offline Grant GayedFriend
Messages: 2150
Registered: July 2009
Senior Member
One approach that would probably work here, though I would classify it as a
hack, is to asyncExec the creation of the table items and editors, as this
will likely be run after the shell has been opened. In your wizard page
creation method this would look like:

display.asyncExec(new Runnable() {
public void run() {
if (table.isDisposed()) return;
...the for loop that creates the items and editors...
}
});

However a better approach, if possible, would be to run the for loop that
creates the items and editors once your page has been shown. I'm not too
familiar with the wizards framework, but perhaps there's some way to know
when your page is first shown? Scanning through the javadoc, if your page
is a subclass of WizardPage, perhaps you could override setVisible() to call
super and then do your for loop if this is the first time this has been
invoked with true?

Grant


"Roger Shimada" <roger.shimada@us.lawson.com> wrote in message
news:hinuc6$s3a$1@build.eclipse.org...
> Hi Grant,
>
> Thanks a bunch for looking into this.
>
> I didn't mention that I ran into this problem in a Wizard. So the
Shell.open() in my posted code is part of the test setup.
>
> I tried setting the minimumHeight fields in the TableEditors (with and
without grabVertical) which didn't make a difference.
>
> Any ideas about how to get the initial paint right on a Wizard page?
Re: Delayed resize of TableEditor widgets [message #508107 is a reply to message #508016] Fri, 15 January 2010 23:36 Go to previous message
Roger Shimada is currently offline Roger ShimadaFriend
Messages: 4
Registered: January 2010
Junior Member
I'm beginning regret having posted this topic!

I tried both the asyncExec() and setVisible() approaches. The results were similiar to the original: the TableEditor widgets get redrawn after 1.5 seconds with the requested height.

I'll put up with this for now. The alternative is to remove the SWT.MeasureItem listener, which results in descenders in the Text getting chopped off and a hard to read (squished) Button.

Thanks again, Grant!
Previous Topic:Menu bar is invisible in standalone SWT application (MacOS)
Next Topic:How to show a custom cursor while hovering over a menu bar?
Goto Forum:
  


Current Time: Sun Dec 06 02:34:16 GMT 2020

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

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

Back to the top