Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » StyledText in SWT Table(Highlight text similar to StyledText in a cell in SWT Table)
StyledText in SWT Table [message #526954] Tue, 13 April 2010 15:04 Go to next message
Brian is currently offline BrianFriend
Messages: 15
Registered: July 2009
Junior Member
What I'm trying to do is high-light text in a cell of a SWT Table similar to what can be done with StyledText. Is this possible? Can we have StyledText in a SWT Table?

The following snippet shows a blue background (or highlighting) that I would like to do in a table cell.

http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.swt.sni ppets/src/org/eclipse/swt/snippets/Snippet163.java?view=co

Thanks.
Re: StyledText in SWT Table [message #526970 is a reply to message #526954] Tue, 13 April 2010 15:31 Go to previous messageGo to next message
Brian is currently offline BrianFriend
Messages: 15
Registered: July 2009
Junior Member
I'm not sure why I didn't see this before, but I think I found what I was looking for

http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.swt.sni ppets/src/org/eclipse/swt/snippets/Snippet236.java?view=co

Re: StyledText in SWT Table [message #526990 is a reply to message #526970] Tue, 13 April 2010 15:38 Go to previous messageGo to next message
Grant Gayed is currently offline Grant GayedFriend
Messages: 2150
Registered: July 2009
Senior Member
Yes, custom draw is the ideal approach to use for doing special formatting
of Table/TreeItems. The snippet should include a link to the custom draw
article, which I'll add. In the meantime if you want to read about this see
http://www.eclipse.org/articles/article.php?file=Article-Cus tomDrawingTableAndTreeItems/index.html .

Grant


"Brian" <nebulour@gmail.com> wrote in message
news:hq22oe$h7f$1@build.eclipse.org...
> I'm not sure why I didn't see this before, but I think I found what I was
looking for
>
>
http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.swt.sni ppets/src/org/eclipse/swt/snippets/Snippet236.java?view=co
>
>
Re: StyledText in SWT Table [message #527064 is a reply to message #526990] Tue, 13 April 2010 21:08 Go to previous messageGo to next message
Brian is currently offline BrianFriend
Messages: 15
Registered: July 2009
Junior Member
Grant,

Thanks for the article but after beating my head against the wall for several hours, I was hoping you might have another example.

In the examples in the article, I can't figure out how to determine 1) in the EraseItem what cell was being drawn and 2) how to set the background of part of a text in a cell (for instance, if the cell had "THE LAZY BROWN FOX JUMPED", I want to be able to set the background for only LAZY and FOX).

In the original snippet, a StyledText is used which allows me to set the background for an index range of the cell text, but I'm struggling to see how the StyledText, TableItem, and Table are all related. The snippet just uses the same text in every cell which isn't helpful in the real world.

Basically, I've tried approaching the problem from two different directions and I'm hitting a wall with both of them. I'm still experimenting but if you have any ideas or examples, please send them my way.

Thanks.
Re: StyledText in SWT Table [message #527123 is a reply to message #526954] Wed, 14 April 2010 07:57 Go to previous messageGo to next message
Dmitry K is currently offline Dmitry KFriend
Messages: 17
Registered: July 2009
Junior Member
1) Event, which we are getting in SWT.EraseItem listener in a snippet, has a field item. It doesn't suits you?
2) In a snippet, you can change the last param in the line
TextStyle style2 = new TextStyle(font2, display.getSystemColor(SWT.COLOR_MAGENTA), null);

from null to some color, and you'll get such effect.
Re: StyledText in SWT Table [message #527227 is a reply to message #527064] Wed, 14 April 2010 14:15 Go to previous messageGo to next message
Grant Gayed is currently offline Grant GayedFriend
Messages: 2150
Registered: July 2009
Senior Member
Here's a modification of Snippet236 that does what you want.

public static void main(String [] args) {
Display display = new Display();
final String STRING_GRAY = "gray";
final String STRING_RED = "red";
final Color COLOR_GRAY = display.getSystemColor(SWT.COLOR_GRAY);
final Color COLOR_RED = display.getSystemColor(SWT.COLOR_RED);

Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
Table table = new Table(shell, SWT.NONE);
table.setLinesVisible(true);
table.setHeaderVisible(true);
new TableItem(table, SWT.NONE).setText("The quick red fox jumped");
new TableItem(table, SWT.NONE).setText("The quick gray fox jumped");

final TextLayout textLayout = new TextLayout(display);
table.addListener(SWT.PaintItem, new Listener() {
public void handleEvent(Event event) {
TableItem item = (TableItem)event.item;
String text = item.getText();
textLayout.setText(text);
if ((event.detail & SWT.SELECTED) == 0) {
/* only do highlighting if item is not currently selected */
int index = text.indexOf(STRING_GRAY);
if (index != -1) {
TextStyle style = new TextStyle(null, null, COLOR_GRAY);
textLayout.setStyle(style, index, index +
STRING_GRAY.length() - 1);
}
index = text.indexOf(STRING_RED);
if (index != -1) {
TextStyle style = new TextStyle(null, null, COLOR_RED);
textLayout.setStyle(style, index, index +
STRING_RED.length() - 1);
}
}
textLayout.draw(event.gc, event.x, event.y);
}
});
table.addListener(SWT.EraseItem, new Listener() {
public void handleEvent(Event event) {
/* indicate that we'll be drawing the foreground in the
PaintItem listener */
event.detail &= ~SWT.FOREGROUND;
}
});
shell.setSize(400, 200);
shell.open();
while(!shell.isDisposed()) {
if(!display.readAndDispatch()) display.sleep();
}
textLayout.dispose();
display.dispose();
}

Grant


"Brian" <nebulour@gmail.com> wrote in message
news:hq2mh8$s9r$1@build.eclipse.org...
> Grant,
>
> Thanks for the article but after beating my head against the wall for
several hours, I was hoping you might have another example.
>
> In the examples in the article, I can't figure out how to determine 1) in
the EraseItem what cell was being drawn and 2) how to set the background of
part of a text in a cell (for instance, if the cell had "THE LAZY BROWN FOX
JUMPED", I want to be able to set the background for only LAZY and FOX).
>
> In the original snippet, a StyledText is used which allows me to set the
background for an index range of the cell text, but I'm struggling to see
how the StyledText, TableItem, and Table are all related. The snippet just
uses the same text in every cell which isn't helpful in the real world.
>
> Basically, I've tried approaching the problem from two different
directions and I'm hitting a wall with both of them. I'm still
experimenting but if you have any ideas or examples, please send them my
way.
>
> Thanks.
Re: StyledText in SWT Table [message #527520 is a reply to message #527227] Thu, 15 April 2010 14:00 Go to previous messageGo to next message
Brian is currently offline BrianFriend
Messages: 15
Registered: July 2009
Junior Member
Thanks! That helped a lot.
Re: StyledText in SWT Table [message #527525 is a reply to message #527520] Thu, 15 April 2010 14:07 Go to previous message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6651
Registered: July 2009
Senior Member
JFace has a special label provider which can deal with
StyledText-Informations.

Tom

Am 15.04.10 16:00, schrieb Brian:
> Thanks! That helped a lot.
Previous Topic:Eclipse Form Section nesting problem
Next Topic:CTabItem Content Not showing up
Goto Forum:
  


Current Time: Thu Apr 25 19:44:15 GMT 2024

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

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

Back to the top