Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
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 09:32 Go to next message
H.ORTIZ  is currently offline 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 09:50 Go to previous messageGo to next message
Grant Gayed is currently offline Grant Gayed
Messages: 2148
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 04:04 Go to previous messageGo to next message
H.ORTIZ  is currently offline 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 09:27 Go to previous messageGo to next message
Grant Gayed is currently offline Grant Gayed
Messages: 2148
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();
>
> }
>
> }
>
>
>
Re: Horizontal scroll bar for table widget [message #502933 is a reply to message #502503] Thu, 10 December 2009 06:17 Go to previous messageGo to next message
H.ORTIZ  is currently offline H.ORTIZ
Messages: 22
Registered: July 2009
Junior Member
Thanks a lot Grant, it works fine now.
Re: Horizontal scroll bar for table widget [message #1039642 is a reply to message #502933] Fri, 12 April 2013 07:47 Go to previous message
hortiz Mising name is currently offline hortiz Mising name
Messages: 82
Registered: July 2009
Member
Hi again !

The given solution works fine but I've just find out that calling pack() method for every table makes the scrolling very slow (I use the table as a log viewer, it contains about 300.000 items)

Here is the "SetData" listener part of my code:
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);
				LogEntry entry = tableItems.get(index);
				if (entry != null) {
					item.setData(entry);
					item.setText(new String[] {entry.getDate(), entry.getTime(), entry.getMessage()});
					table.getColumn(2).pack();
				}
			}
		});


How can I detect (if possible) the end of the "SetData" process to call the method pack only once ?

Thanks
Helene
Previous Topic:SWT Javadoc
Next Topic:Optimize the positioning of controls onto StyledText
Goto Forum:
  


Current Time: Fri Aug 29 01:38:39 EDT 2014

Powered by FUDForum. Page generated in 0.37019 seconds