Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Table row heigth with GridLayout
Table row heigth with GridLayout [message #445393] Thu, 04 November 2004 21:37 Go to next message
jmi is currently offline jmiFriend
Messages: 84
Registered: July 2009
Member
Hi,

With the code below, i have 2 tables. If you run the code, you'll see that
the result table is about 3 rows heigth. I would like to limit it to 1 row
because it is a result table.

Thanks for your help

JMi

CODE
=====
import java.text.Collator;
import java.util.Locale;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
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;

public class _Test1Table extends Composite {

private Table _detailsTable = null;

private Table _resultTable = null;

public _Test1Table(final Composite argParent, final int argStyle) {
this(argParent, argStyle, 0, 0, 3);
}

public _Test1Table(final Composite argParent, final int argStyle,
final int argMarginHeigth, final int argMarginWidth,
final int argVerticalSpacing) {
super(argParent, SWT.NONE);
createContents(argStyle, argMarginHeigth, argMarginWidth,
argVerticalSpacing);
}

protected void createContents(final int argStyle, final int
argMarginHeigth,
final int argMarginWidth, final int argVerticalSpacing) {
try {
GridLayout layout = new GridLayout();
layout.numColumns = 1;
layout.marginHeight = argMarginHeigth;
layout.marginWidth = argMarginWidth;
layout.verticalSpacing = argVerticalSpacing;
setLayout(layout);
// Main Table
_detailsTable = new Table(this, argStyle);
_detailsTable.setHeaderVisible(true);
_detailsTable.setLinesVisible(true);
TableColumn column1 = new TableColumn(_detailsTable, SWT.NONE);
column1.setText("Column 1");
TableColumn column2 = new TableColumn(_detailsTable, SWT.NONE);
column2.setText("Column 2");
column2.setAlignment(SWT.RIGHT);
TableItem item = new TableItem(_detailsTable, SWT.NONE);
item.setText(new String[] { "a", "3" });
item = new TableItem(_detailsTable, SWT.NONE);
item.setText(new String[] { "b", "2" });
item = new TableItem(_detailsTable, SWT.NONE);
item.setText(new String[] { "c", "1" });
// column1.pack();
// column2.pack();
column1.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event e) {
// sort column 1
TableItem[] items = _detailsTable.getItems();
Collator collator = Collator.getInstance(Locale
.getDefault());
for (int i = 1; i < items.length; i++) {
String value1 = items[i].getText(0);
for (int j = 0; j < i; j++) {
String value2 = items[j].getText(0);
if (collator.compare(value1, value2) < 0) {
String[] values = { items[i].getText(0),
items[i].getText(1) };
items[i].dispose();
TableItem item = new
TableItem(_detailsTable,
SWT.NONE, j);
item.setText(values);
items = _detailsTable.getItems();
break;
}
}
}
}
});
column2.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event e) {
// sort column 2
TableItem[] items = _detailsTable.getItems();
Collator collator = Collator.getInstance(Locale
.getDefault());
for (int i = 1; i < items.length; i++) {
String value1 = items[i].getText(1);
for (int j = 0; j < i; j++) {
String value2 = items[j].getText(1);
if (collator.compare(value1, value2) < 0) {
String[] values = { items[i].getText(0),
items[i].getText(1) };
items[i].dispose();
TableItem item = new
TableItem(_detailsTable,
SWT.NONE, j);
item.setText(values);
items = _detailsTable.getItems();
break;
}
}
}
}
});
// _detailsTable position
GridData data = new GridData();
data.horizontalAlignment = GridData.FILL;
data.verticalAlignment = GridData.FILL;
data.grabExcessHorizontalSpace = true;
data.grabExcessVerticalSpace = true;
_detailsTable.setLayoutData(data);

// Main Table
_resultTable = new Table(this, argStyle);
_resultTable.setHeaderVisible(false);
_resultTable.setLinesVisible(true);

TableColumn col1 = new TableColumn(_resultTable, SWT.NONE);
TableColumn col2 = new TableColumn(_resultTable, SWT.NONE);
col2.setAlignment(SWT.RIGHT);
TableItem item2 = new TableItem(_resultTable, SWT.NONE);
item2.setText(new String[] { "a", "3" });
col1.pack();
col2.pack();

// _resultTable position
data = new GridData();
data.horizontalAlignment = GridData.FILL;
data.grabExcessHorizontalSpace = true;
//_resultTable.setSize(10, 10);
_resultTable.setLayoutData(data);

addColumnResizeListener(_detailsTable, _resultTable);
column1.pack();
column2.pack();

} catch (final Exception argEx) {
System.out.println(argEx.getMessage());
}
}

private void addColumnResizeListener(final Table argDetailsTable,
final Table argResultTable) {
TableColumn detailsColumn = null;
final int colCount = argDetailsTable.getColumnCount();
for (int i = 0; i < colCount; i++) {
detailsColumn = argDetailsTable.getColumn(i);
final int colNum = i;
detailsColumn.addListener(SWT.Resize, new Listener() {
public void handleEvent(Event e) {
if (argResultTable == null)
return;
argResultTable.getColumn(colNum).setWidth(
((TableColumn) e.widget).getWidth());
}
});
}
}

public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
_Test1Table test = new _Test1Table(shell, SWT.BORDER);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
Re: Table row heigth with GridLayout [message #445424 is a reply to message #445393] Fri, 05 November 2004 12:57 Go to previous messageGo to next message
Veronika Irvine is currently offline Veronika IrvineFriend
Messages: 1272
Registered: July 2009
Senior Member
You need to set the data.heightHint to something like:

// _resultTable position
data = new GridData();
data.horizontalAlignment = GridData.FILL;
data.grabExcessHorizontalSpace = true;
data.heightHint = Math.max (0, _resultTable.getItemHeight() -
_resultTable.computeTrim(0, 0, 0, 0).height +
2*_resultTable.getBorderWidth() + 2*_resultTable.getGridLineWidth());
//_resultTable.setSize(10, 10);
_resultTable.setLayoutData(data);

"JM Delsaux" <JMDelsaux@skynet.be> wrote in message
news:cme7eb$ofn$1@eclipse.org...
> Hi,
>
> With the code below, i have 2 tables. If you run the code, you'll see
> that the result table is about 3 rows heigth. I would like to limit it to
> 1 row because it is a result table.
>
> Thanks for your help
>
> JMi
>
> CODE
> =====
> import java.text.Collator;
> import java.util.Locale;
>
> import org.eclipse.swt.SWT;
> import org.eclipse.swt.graphics.Rectangle;
> import org.eclipse.swt.layout.FillLayout;
> import org.eclipse.swt.layout.GridLayout;
> import org.eclipse.swt.layout.GridData;
> import org.eclipse.swt.widgets.Composite;
> import org.eclipse.swt.widgets.Display;
> import org.eclipse.swt.widgets.Event;
> 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;
>
> public class _Test1Table extends Composite {
>
> private Table _detailsTable = null;
>
> private Table _resultTable = null;
>
> public _Test1Table(final Composite argParent, final int argStyle) {
> this(argParent, argStyle, 0, 0, 3);
> }
>
> public _Test1Table(final Composite argParent, final int argStyle,
> final int argMarginHeigth, final int argMarginWidth,
> final int argVerticalSpacing) {
> super(argParent, SWT.NONE);
> createContents(argStyle, argMarginHeigth, argMarginWidth,
> argVerticalSpacing);
> }
>
> protected void createContents(final int argStyle, final int
> argMarginHeigth,
> final int argMarginWidth, final int argVerticalSpacing) {
> try {
> GridLayout layout = new GridLayout();
> layout.numColumns = 1;
> layout.marginHeight = argMarginHeigth;
> layout.marginWidth = argMarginWidth;
> layout.verticalSpacing = argVerticalSpacing;
> setLayout(layout);
> // Main Table
> _detailsTable = new Table(this, argStyle);
> _detailsTable.setHeaderVisible(true);
> _detailsTable.setLinesVisible(true);
> TableColumn column1 = new TableColumn(_detailsTable, SWT.NONE);
> column1.setText("Column 1");
> TableColumn column2 = new TableColumn(_detailsTable, SWT.NONE);
> column2.setText("Column 2");
> column2.setAlignment(SWT.RIGHT);
> TableItem item = new TableItem(_detailsTable, SWT.NONE);
> item.setText(new String[] { "a", "3" });
> item = new TableItem(_detailsTable, SWT.NONE);
> item.setText(new String[] { "b", "2" });
> item = new TableItem(_detailsTable, SWT.NONE);
> item.setText(new String[] { "c", "1" });
> // column1.pack();
> // column2.pack();
> column1.addListener(SWT.Selection, new Listener() {
> public void handleEvent(Event e) {
> // sort column 1
> TableItem[] items = _detailsTable.getItems();
> Collator collator = Collator.getInstance(Locale
> .getDefault());
> for (int i = 1; i < items.length; i++) {
> String value1 = items[i].getText(0);
> for (int j = 0; j < i; j++) {
> String value2 = items[j].getText(0);
> if (collator.compare(value1, value2) < 0) {
> String[] values = { items[i].getText(0),
> items[i].getText(1) };
> items[i].dispose();
> TableItem item = new
> TableItem(_detailsTable,
> SWT.NONE, j);
> item.setText(values);
> items = _detailsTable.getItems();
> break;
> }
> }
> }
> }
> });
> column2.addListener(SWT.Selection, new Listener() {
> public void handleEvent(Event e) {
> // sort column 2
> TableItem[] items = _detailsTable.getItems();
> Collator collator = Collator.getInstance(Locale
> .getDefault());
> for (int i = 1; i < items.length; i++) {
> String value1 = items[i].getText(1);
> for (int j = 0; j < i; j++) {
> String value2 = items[j].getText(1);
> if (collator.compare(value1, value2) < 0) {
> String[] values = { items[i].getText(0),
> items[i].getText(1) };
> items[i].dispose();
> TableItem item = new
> TableItem(_detailsTable,
> SWT.NONE, j);
> item.setText(values);
> items = _detailsTable.getItems();
> break;
> }
> }
> }
> }
> });
> // _detailsTable position
> GridData data = new GridData();
> data.horizontalAlignment = GridData.FILL;
> data.verticalAlignment = GridData.FILL;
> data.grabExcessHorizontalSpace = true;
> data.grabExcessVerticalSpace = true;
> _detailsTable.setLayoutData(data);
>
> // Main Table
> _resultTable = new Table(this, argStyle);
> _resultTable.setHeaderVisible(false);
> _resultTable.setLinesVisible(true);
>
> TableColumn col1 = new TableColumn(_resultTable, SWT.NONE);
> TableColumn col2 = new TableColumn(_resultTable, SWT.NONE);
> col2.setAlignment(SWT.RIGHT);
> TableItem item2 = new TableItem(_resultTable, SWT.NONE);
> item2.setText(new String[] { "a", "3" });
> col1.pack();
> col2.pack();
>
> // _resultTable position
> data = new GridData();
> data.horizontalAlignment = GridData.FILL;
> data.grabExcessHorizontalSpace = true;
> //_resultTable.setSize(10, 10);
> _resultTable.setLayoutData(data);
>
> addColumnResizeListener(_detailsTable, _resultTable);
> column1.pack();
> column2.pack();
>
> } catch (final Exception argEx) {
> System.out.println(argEx.getMessage());
> }
> }
>
> private void addColumnResizeListener(final Table argDetailsTable,
> final Table argResultTable) {
> TableColumn detailsColumn = null;
> final int colCount = argDetailsTable.getColumnCount();
> for (int i = 0; i < colCount; i++) {
> detailsColumn = argDetailsTable.getColumn(i);
> final int colNum = i;
> detailsColumn.addListener(SWT.Resize, new Listener() {
> public void handleEvent(Event e) {
> if (argResultTable == null)
> return;
> argResultTable.getColumn(colNum).setWidth(
> ((TableColumn) e.widget).getWidth());
> }
> });
> }
> }
>
> public static void main(String[] args) {
> Display display = new Display();
> Shell shell = new Shell(display);
> shell.setLayout(new FillLayout());
> _Test1Table test = new _Test1Table(shell, SWT.BORDER);
> shell.open();
> while (!shell.isDisposed()) {
> if (!display.readAndDispatch())
> display.sleep();
> }
> display.dispose();
> }
>
>
Re: Table row heigth with GridLayout [message #445445 is a reply to message #445424] Fri, 05 November 2004 20:19 Go to previous message
jmi is currently offline jmiFriend
Messages: 84
Registered: July 2009
Member
Thanks Veronica, it runs :-))


"Veronika Irvine" <veronika_irvine@oti.com> wrote in message
news:cmfthu$coi$2@eclipse.org...
> You need to set the data.heightHint to something like:
>
> // _resultTable position
> data = new GridData();
> data.horizontalAlignment = GridData.FILL;
> data.grabExcessHorizontalSpace = true;
> data.heightHint = Math.max (0, _resultTable.getItemHeight() -
> _resultTable.computeTrim(0, 0, 0, 0).height +
> 2*_resultTable.getBorderWidth() + 2*_resultTable.getGridLineWidth());
> //_resultTable.setSize(10, 10);
> _resultTable.setLayoutData(data);
>
> "JM Delsaux" <JMDelsaux@skynet.be> wrote in message
> news:cme7eb$ofn$1@eclipse.org...
>> Hi,
>>
>> With the code below, i have 2 tables. If you run the code, you'll see
>> that the result table is about 3 rows heigth. I would like to limit it
>> to 1 row because it is a result table.
>>
>> Thanks for your help
>>
>> JMi
>>
>> CODE
>> =====
>> import java.text.Collator;
>> import java.util.Locale;
>>
>> import org.eclipse.swt.SWT;
>> import org.eclipse.swt.graphics.Rectangle;
>> import org.eclipse.swt.layout.FillLayout;
>> import org.eclipse.swt.layout.GridLayout;
>> import org.eclipse.swt.layout.GridData;
>> import org.eclipse.swt.widgets.Composite;
>> import org.eclipse.swt.widgets.Display;
>> import org.eclipse.swt.widgets.Event;
>> 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;
>>
>> public class _Test1Table extends Composite {
>>
>> private Table _detailsTable = null;
>>
>> private Table _resultTable = null;
>>
>> public _Test1Table(final Composite argParent, final int argStyle) {
>> this(argParent, argStyle, 0, 0, 3);
>> }
>>
>> public _Test1Table(final Composite argParent, final int argStyle,
>> final int argMarginHeigth, final int argMarginWidth,
>> final int argVerticalSpacing) {
>> super(argParent, SWT.NONE);
>> createContents(argStyle, argMarginHeigth, argMarginWidth,
>> argVerticalSpacing);
>> }
>>
>> protected void createContents(final int argStyle, final int
>> argMarginHeigth,
>> final int argMarginWidth, final int argVerticalSpacing) {
>> try {
>> GridLayout layout = new GridLayout();
>> layout.numColumns = 1;
>> layout.marginHeight = argMarginHeigth;
>> layout.marginWidth = argMarginWidth;
>> layout.verticalSpacing = argVerticalSpacing;
>> setLayout(layout);
>> // Main Table
>> _detailsTable = new Table(this, argStyle);
>> _detailsTable.setHeaderVisible(true);
>> _detailsTable.setLinesVisible(true);
>> TableColumn column1 = new TableColumn(_detailsTable,
>> SWT.NONE);
>> column1.setText("Column 1");
>> TableColumn column2 = new TableColumn(_detailsTable,
>> SWT.NONE);
>> column2.setText("Column 2");
>> column2.setAlignment(SWT.RIGHT);
>> TableItem item = new TableItem(_detailsTable, SWT.NONE);
>> item.setText(new String[] { "a", "3" });
>> item = new TableItem(_detailsTable, SWT.NONE);
>> item.setText(new String[] { "b", "2" });
>> item = new TableItem(_detailsTable, SWT.NONE);
>> item.setText(new String[] { "c", "1" });
>> // column1.pack();
>> // column2.pack();
>> column1.addListener(SWT.Selection, new Listener() {
>> public void handleEvent(Event e) {
>> // sort column 1
>> TableItem[] items = _detailsTable.getItems();
>> Collator collator = Collator.getInstance(Locale
>> .getDefault());
>> for (int i = 1; i < items.length; i++) {
>> String value1 = items[i].getText(0);
>> for (int j = 0; j < i; j++) {
>> String value2 = items[j].getText(0);
>> if (collator.compare(value1, value2) < 0) {
>> String[] values = { items[i].getText(0),
>> items[i].getText(1) };
>> items[i].dispose();
>> TableItem item = new
>> TableItem(_detailsTable,
>> SWT.NONE, j);
>> item.setText(values);
>> items = _detailsTable.getItems();
>> break;
>> }
>> }
>> }
>> }
>> });
>> column2.addListener(SWT.Selection, new Listener() {
>> public void handleEvent(Event e) {
>> // sort column 2
>> TableItem[] items = _detailsTable.getItems();
>> Collator collator = Collator.getInstance(Locale
>> .getDefault());
>> for (int i = 1; i < items.length; i++) {
>> String value1 = items[i].getText(1);
>> for (int j = 0; j < i; j++) {
>> String value2 = items[j].getText(1);
>> if (collator.compare(value1, value2) < 0) {
>> String[] values = { items[i].getText(0),
>> items[i].getText(1) };
>> items[i].dispose();
>> TableItem item = new
>> TableItem(_detailsTable,
>> SWT.NONE, j);
>> item.setText(values);
>> items = _detailsTable.getItems();
>> break;
>> }
>> }
>> }
>> }
>> });
>> // _detailsTable position
>> GridData data = new GridData();
>> data.horizontalAlignment = GridData.FILL;
>> data.verticalAlignment = GridData.FILL;
>> data.grabExcessHorizontalSpace = true;
>> data.grabExcessVerticalSpace = true;
>> _detailsTable.setLayoutData(data);
>>
>> // Main Table
>> _resultTable = new Table(this, argStyle);
>> _resultTable.setHeaderVisible(false);
>> _resultTable.setLinesVisible(true);
>>
>> TableColumn col1 = new TableColumn(_resultTable, SWT.NONE);
>> TableColumn col2 = new TableColumn(_resultTable, SWT.NONE);
>> col2.setAlignment(SWT.RIGHT);
>> TableItem item2 = new TableItem(_resultTable, SWT.NONE);
>> item2.setText(new String[] { "a", "3" });
>> col1.pack();
>> col2.pack();
>>
>> // _resultTable position
>> data = new GridData();
>> data.horizontalAlignment = GridData.FILL;
>> data.grabExcessHorizontalSpace = true;
>> //_resultTable.setSize(10, 10);
>> _resultTable.setLayoutData(data);
>>
>> addColumnResizeListener(_detailsTable, _resultTable);
>> column1.pack();
>> column2.pack();
>>
>> } catch (final Exception argEx) {
>> System.out.println(argEx.getMessage());
>> }
>> }
>>
>> private void addColumnResizeListener(final Table argDetailsTable,
>> final Table argResultTable) {
>> TableColumn detailsColumn = null;
>> final int colCount = argDetailsTable.getColumnCount();
>> for (int i = 0; i < colCount; i++) {
>> detailsColumn = argDetailsTable.getColumn(i);
>> final int colNum = i;
>> detailsColumn.addListener(SWT.Resize, new Listener() {
>> public void handleEvent(Event e) {
>> if (argResultTable == null)
>> return;
>> argResultTable.getColumn(colNum).setWidth(
>> ((TableColumn) e.widget).getWidth());
>> }
>> });
>> }
>> }
>>
>> public static void main(String[] args) {
>> Display display = new Display();
>> Shell shell = new Shell(display);
>> shell.setLayout(new FillLayout());
>> _Test1Table test = new _Test1Table(shell, SWT.BORDER);
>> shell.open();
>> while (!shell.isDisposed()) {
>> if (!display.readAndDispatch())
>> display.sleep();
>> }
>> display.dispose();
>> }
>>
>>
>
>
Previous Topic:Accessing Object Instances in via OLE
Next Topic:Color and dispose
Goto Forum:
  


Current Time: Thu Oct 22 13:13:32 GMT 2020

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

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

Back to the top