From: Tech Id on
Hello,

In the tiny piece of code below, I am not able to figure out whats
wrong with the custom drawing of code.
I want it to use for displaying text that spans multiple columns
(starting from column 0)

Thanks a lot in advance.
Techie



class test {

Display display;
Shell shell;

public test () {
display = new Display ();
shell = new Shell (display);

text_spanning_all_columns ();

shell.pack ();
shell.open ();
while (!shell.isDisposed()) {
if (!display.readAndDispatch ()) display.sleep ();
}
display.dispose ();
}

void text_spanning_all_columns () {
shell.setLayout (new FillLayout());
final Tree table = new Tree(shell, SWT.MULTI | SWT.FULL_SELECTION);
table.setHeaderVisible(true);
int columnCount = 4;
for (int i=0; i<columnCount; i++) {
TreeColumn column = new TreeColumn(table, SWT.NONE);
column.setText("Column " + i);
}
int itemCount = 8;
for (int i = 0; i < itemCount; i++) {
TreeItem item = new TreeItem(table, SWT.NONE);
item.setText(0, "item "+i+" a");
item.setText(3, "item "+i+" d");
TreeItem subItem = new TreeItem (item, SWT.NONE);
}
/*
* NOTE: MeasureItem, PaintItem and EraseItem are called repeatedly.
* Therefore, it is critical for performance that these methods be
* as efficient as possible.
*/
final String string = "text that spans two columns and much much
more";
GC gc = new GC(table);
final Point extent = gc.stringExtent(string);
gc.dispose();
final Color red = display.getSystemColor(SWT.COLOR_RED);
Listener paintListener = new Listener() {
public void handleEvent(Event event) {
switch(event.type) {
case SWT.MeasureItem: {
TreeItem item = (TreeItem) event.item;
if (item.getParentItem() != null) {
//event.width = extent.x/2;
//event.height = Math.max(event.height, extent.y + 2);
}
break;
}
case SWT.PaintItem: {

TreeItem item = (TreeItem) event.item;
if (item.getParentItem() != null) {
int offset = 0;
for (int i=0; i<event.index; i++)
offset += table.getColumn(i).getWidth();

event.gc.setForeground(red);
int y = event.y + (event.height - extent.y)/2;
event.gc.drawString(string, event.x - offset, y, true);
}
break;
}
}
}
};
table.addListener(SWT.MeasureItem, paintListener);
table.addListener(SWT.PaintItem, paintListener);
for (int i = 0; i < columnCount; i++) {
table.getColumn(i).pack();
}
}
}


public class abc {

public static void main(String [] args) {
new test();
}
}
From: Jeff Higgins on
On 7/6/2010 4:22 AM, Tech Id wrote:
> Hello,
>
> In the tiny piece of code below, I am not able to figure out whats
> wrong with the custom drawing of code.
> I want it to use for displaying text that spans multiple columns
> (starting from column 0)
>
> Thanks a lot in advance.
> Techie
>
>
>
> class test {
>
> Display display;
> Shell shell;
>
> public test () {
> display = new Display ();
> shell = new Shell (display);
>
> text_spanning_all_columns ();
>
> shell.pack ();
> shell.open ();
> while (!shell.isDisposed()) {
> if (!display.readAndDispatch ()) display.sleep ();
> }
> display.dispose ();
> }
>
> void text_spanning_all_columns () {
> shell.setLayout (new FillLayout());
> final Tree table = new Tree(shell, SWT.MULTI | SWT.FULL_SELECTION);
> table.setHeaderVisible(true);
> int columnCount = 4;
> for (int i=0; i<columnCount; i++) {
> TreeColumn column = new TreeColumn(table, SWT.NONE);
> column.setText("Column " + i);
> }
> int itemCount = 8;
> for (int i = 0; i< itemCount; i++) {
> TreeItem item = new TreeItem(table, SWT.NONE);
> item.setText(0, "item "+i+" a");
> item.setText(3, "item "+i+" d");
> TreeItem subItem = new TreeItem (item, SWT.NONE);
> }
> /*
> * NOTE: MeasureItem, PaintItem and EraseItem are called repeatedly.
> * Therefore, it is critical for performance that these methods be
> * as efficient as possible.
> */
> final String string = "text that spans two columns and much much
> more";
> GC gc = new GC(table);
> final Point extent = gc.stringExtent(string);
> gc.dispose();
> final Color red = display.getSystemColor(SWT.COLOR_RED);
> Listener paintListener = new Listener() {
> public void handleEvent(Event event) {
> switch(event.type) {
> case SWT.MeasureItem: {
> TreeItem item = (TreeItem) event.item;
> if (item.getParentItem() != null) {
> //event.width = extent.x/2;
> //event.height = Math.max(event.height, extent.y + 2);
> }
> break;
> }
> case SWT.PaintItem: {
>
> TreeItem item = (TreeItem) event.item;
> if (item.getParentItem() != null) {
> int offset = 0;
> for (int i=0; i<event.index; i++)
> offset += table.getColumn(i).getWidth();
>
> event.gc.setForeground(red);
> int y = event.y + (event.height - extent.y)/2;
> event.gc.drawString(string, event.x - offset, y, true);
> }
> break;
> }
> }
> }
> };
> table.addListener(SWT.MeasureItem, paintListener);
> table.addListener(SWT.PaintItem, paintListener);
> for (int i = 0; i< columnCount; i++) {
> table.getColumn(i).pack();
> }
> }
> }
>
>
> public class abc {
>
> public static void main(String [] args) {
> new test();
> }
> }

class test {

Display display;
Shell shell;

public test() {
display = new Display();
shell = new Shell(display);
text_spanning_all_columns();
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}

void text_spanning_all_columns () {
shell.setLayout (new FillLayout());
final Tree table = new Tree(shell, SWT.MULTI | SWT.FULL_SELECTION);
table.setHeaderVisible(true);
int columnCount = 4;
for (int i=0; i<columnCount; i++) {
TreeColumn column = new TreeColumn(table, SWT.NONE);
column.setText("Column " + i);
}
int itemCount = 8;
for (int i = 0; i < itemCount; i++) {
TreeItem item = new TreeItem(table, SWT.NONE);
item.setText(0, "item "+i+" a");
item.setText(3, "item "+i+" d");
TreeItem subItem = new TreeItem (item, SWT.NONE);
}
/*
* NOTE: MeasureItem, PaintItem and EraseItem are called repeatedly.
* Therefore, it is critical for performance that these methods be
* as efficient as possible.
*/
final String string =
"text that spans two columns and much much more";
GC gc = new GC(table);
final Point extent = gc.stringExtent(string);
gc.dispose();
final Color red = display.getSystemColor(SWT.COLOR_RED);
Listener paintListener = new Listener() {
public void handleEvent(Event event) {
switch(event.type) {
case SWT.MeasureItem: {
TreeItem item = (TreeItem) event.item;
if (item.getParentItem() != null) {
//event.width = extent.x/2;
//event.height = Math.max(event.height, extent.y + 2);
}
break;
}
case SWT.PaintItem: {
TreeItem item = (TreeItem) event.item;
if (item.getParentItem() != null) {
int offset = 0;
for (int i=0; i<event.index; i++) {
offset += table.getColumn(i).getWidth();
}
event.gc.setForeground(red);
int y = event.y + (event.height - extent.y)/2;
event.gc.drawString(string, event.x - offset, y, true);
}
break;
}
}
}
};
table.addListener(SWT.MeasureItem, paintListener);
table.addListener(SWT.PaintItem, paintListener);
for (int i = 0; i < columnCount; i++) {
table.getColumn(i).pack();
}
}
}

From: Lew on
Tech Id wrote:
> In the tiny piece of code below, I am not able to figure out whats
> wrong with the custom drawing of code.

Side notes:

Do not use TAB to indent source listings for Usenet, use up to four space
characters per indent level.

You should follow the naming conventions.
http://java.sun.com/docs/codeconv/index.html

> I want it to use for displaying text that spans multiple columns
> (starting from column 0)
....
> public class abc {
>
> public static void main(String [] args) {
> new test();
> }
> }

Aside from what Jeff told you, you need to ensure that GUI actions occur on
the Event Dispath Thread (EDT), as explained in the Swing tutorial, or strange
things will happen.

--
Lew
From: Tech Id on
I am unable to see what change you have made?
Can you please highlight it?
From: Jeff Higgins on
On 7/6/2010 1:18 PM, Tech Id wrote:
> I am unable to see what change you have made?
> Can you please highlight it?

Your code looks very much like Snippet239.

<http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet239.java?view=co>

Are you hoping to replicate the behavior of this Snippet239 in a Tree
widget? Have you asked at the Eclipse forums?

 |  Next  |  Last
Pages: 1 2
Prev: Struts - Lazy Validator Form
Next: Java and awk (jawk)