Home » Eclipse Projects » Standard Widget Toolkit (SWT) » StyledText in SWT Table(Highlight text similar to StyledText in a cell in SWT Table)
| | |
Re: StyledText in SWT Table [message #527064 is a reply to message #526990] |
Tue, 13 April 2010 21:08 |
Brian 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 #527227 is a reply to message #527064] |
Wed, 14 April 2010 14:15 |
Grant Gayed 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.
|
|
| | |
Goto Forum:
Current Time: Thu Sep 26 15:38:35 GMT 2024
Powered by FUDForum. Page generated in 0.05529 seconds
|