Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Horizontal scroll bar for table widget(How can SWT detect when an horizontal scroll bar is necessary for a table ?)
Horizontal scroll bar for table widget [message #501939] |
Fri, 04 December 2009 14:32 |
H.ORTIZ Messages: 22 Registered: July 2009 |
Junior Member |
|
|
Hi,
I use a Table widget to display some items.
The table has two columns, and I set their widths to 50 and 100.
Sometimes, the content of the second column is larger than the column, so I expect SWT to detect it and so to display an horizontal scroll bar...
Here is a snippet that shows the problem.
Thanks for your help,
Helene
package tests;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
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 TableTest {
protected final static int LOG_IMAGE_HEIGHT = 20;
protected static int nbColumns = 2;
protected static int[] columnWidths = new int[] {50, 100};
public static void main(String args[]) {
new TableTest();
}
public TableTest() {
Display display = new Display();
Shell shell = new Shell(display);
shell.setBounds(10,10,400,200);
shell.setLayout(new GridLayout());
Table table = new Table(shell, SWT.NONE);
table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
for (int i = 0; i < nbColumns; i++) {
TableColumn column = new TableColumn(table, SWT.NONE);
column.setWidth(columnWidths[i]);
}
table.setLinesVisible(true);
TableItem item = new TableItem(table, SWT.NONE);
item.setText(0, "first");
item.setText(1, "short text");
item = new TableItem(table, SWT.NONE);
item.setText(0, "last");
item.setText(1, "a longer text that should make the horizontal scroll bar appear");
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) display.sleep();
}
display.dispose();
}
}
|
|
|
Re: Horizontal scroll bar for table widget [message #501942 is a reply to message #501939] |
Fri, 04 December 2009 14:50 |
Grant Gayed Messages: 2150 Registered: July 2009 |
Senior Member |
|
|
Hi Helene,
It's only in a Table/Tree with no columns that a horizontal scrollbar may be
shown based on item lengths. When a Table/Tree has columns then a
horizontal scrollbar is shown based solely on the combined width of all the
columns. Having an item text that's too long to fit within a column simply
truncates the text at the column's end bound. This is standard behaviour on
all platforms.
In your case if you want the full second column text to be visible then you
should pack() the second column, and if this makes the column too wide to
fit within the Table's viewport then a horizontal scrollbar will be shown.
Grant
"H.ORTIZ" <hortiz@objetdirect.com> wrote in message
news:hfb6hm$uu7$1@build.eclipse.org...
> Hi,
>
> I use a Table widget to display some items.
> The table has two columns, and I set their widths to 50 and 100.
>
> Sometimes, the content of the second column is larger than the column, so
I expect SWT to detect it and so to display an horizontal scroll bar...
>
> Here is a snippet that shows the problem.
> Thanks for your help,
> Helene
>
>
> package tests;
>
> import org.eclipse.swt.SWT;
> import org.eclipse.swt.layout.GridData;
> import org.eclipse.swt.layout.GridLayout;
> import org.eclipse.swt.widgets.Display;
> 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 TableTest {
>
> protected final static int LOG_IMAGE_HEIGHT = 20;
> protected static int nbColumns = 2;
> protected static int[] columnWidths = new int[] {50, 100};
>
>
> public static void main(String args[]) {
> new TableTest();
> }
>
> public TableTest() {
> Display display = new Display();
> Shell shell = new Shell(display);
> shell.setBounds(10,10,400,200);
> shell.setLayout(new GridLayout());
>
> Table table = new Table(shell, SWT.NONE);
> table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
>
> for (int i = 0; i < nbColumns; i++) {
> TableColumn column = new TableColumn(table, SWT.NONE);
> column.setWidth(columnWidths[i]);
> }
>
> table.setLinesVisible(true);
>
> TableItem item = new TableItem(table, SWT.NONE);
> item.setText(0, "first");
> item.setText(1, "short text");
>
> item = new TableItem(table, SWT.NONE);
> item.setText(0, "last");
> item.setText(1, "a longer text that should make the horizontal scroll bar
appear");
>
> shell.open();
> while (!shell.isDisposed()) {
> if (!display.readAndDispatch()) display.sleep();
> }
> display.dispose();
> }
> }
>
|
|
|
Re: Horizontal scroll bar for table widget [message #502206 is a reply to message #501942] |
Mon, 07 December 2009 09:04 |
H.ORTIZ Messages: 22 Registered: July 2009 |
Junior Member |
|
|
Hi Grant,
Thanks for your reply, it works fine but in fact I use a virtual table, I didn't use it in the snippet I provided to simplify it.
I tried your solution in the case of a virtual table and it didn't work...
What am I doing wrong (see snippet below) ?
Thanks again !
public class CopyOfTableTest {
protected final static int LOG_IMAGE_HEIGHT = 20;
protected static int nbColumns = 2;
protected static int[] columnWidths = new int[] {50, 100};
public static void main(String args[]) {
new CopyOfTableTest();
}
public CopyOfTableTest() {
Display display = new Display();
Shell shell = new Shell(display);
shell.setBounds(10,10,400,200);
shell.setLayout(new GridLayout());
final Table table = new Table(shell, SWT.VIRTUAL);
table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
for (int i = 0; i < nbColumns; i++) {
TableColumn column = new TableColumn(table, SWT.NONE);
column.setWidth(columnWidths[i]);
}
table.setLinesVisible(true);
table.addListener(SWT.SetData, new Listener() {
public void handleEvent(Event event) {
TableItem item = (TableItem)event.item;
int index = table.indexOf(item);
item = table.getItem(index);
item.setText(new String[] {"toto", "a longer text that should make the horizontal scroll bar appear / a longer text that should make the horizontal scroll bar appear"});
}
});
table.setItemCount(1);
table.getColumn(0).pack();
table.getColumn(1).pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) display.sleep();
}
display.dispose();
}
}
|
|
|
Re: Horizontal scroll bar for table widget [message #502503 is a reply to message #502206] |
Tue, 08 December 2009 14:27 |
Grant Gayed Messages: 2150 Registered: July 2009 |
Senior Member |
|
|
In the snippet the TableColumn.pack()s are happening before the TableItem's
texts have been set. If you pack() the columns at the end of the SetData
callback, when the TableItem has had its texts set, then they will pack to
the right size and cause the horizontal scrollbar to show.
Grant
"H.ORTIZ" <hortiz@objetdirect.com> wrote in message
news:hfigf7$68t$1@build.eclipse.org...
> Hi Grant,
>
> Thanks for your reply, it works fine but in fact I use a virtual table, I
didn't use it in the snippet I provided to simplify it.
> I tried your solution in the case of a virtual table and it didn't work...
> What am I doing wrong (see snippet below) ?
> Thanks again !
>
>
>
>
> public class CopyOfTableTest {
>
> protected final static int LOG_IMAGE_HEIGHT = 20;
> protected static int nbColumns = 2;
> protected static int[] columnWidths = new int[] {50, 100};
>
>
> public static void main(String args[]) {
> new CopyOfTableTest();
> }
>
> public CopyOfTableTest() {
> Display display = new Display();
> Shell shell = new Shell(display);
> shell.setBounds(10,10,400,200);
> shell.setLayout(new GridLayout());
>
> final Table table = new Table(shell, SWT.VIRTUAL);
> table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
>
> for (int i = 0; i < nbColumns; i++) {
> TableColumn column = new TableColumn(table, SWT.NONE);
> column.setWidth(columnWidths[i]);
> }
>
> table.setLinesVisible(true);
>
>
> table.addListener(SWT.SetData, new Listener() {
> public void handleEvent(Event event) {
> TableItem item = (TableItem)event.item;
> int index = table.indexOf(item);
> item = table.getItem(index);
> item.setText(new String[] {"toto", "a longer text that should make the
horizontal scroll bar appear / a longer text that should make the horizontal
scroll bar appear"});
> }
> });
>
> table.setItemCount(1);
> table.getColumn(0).pack();
> table.getColumn(1).pack();
>
>
> shell.open();
> while (!shell.isDisposed()) {
> if (!display.readAndDispatch()) display.sleep();
> }
> display.dispose();
>
> }
>
> }
>
>
>
|
|
| | |
Goto Forum:
Current Time: Wed Apr 24 18:25:10 GMT 2024
Powered by FUDForum. Page generated in 0.03353 seconds
|