Let’s say if you want to display a message to the user, in that message some part will be in bold, some part will be italic, etc.
Now I will show you how to achieve this, have a look at the class
public class BoldTextMessageDialog extends Dialog {
private Composite container;
private String firstSring;
private String secondString;
private String boldString;
private Button restoreButton;
private Button cancelButton;
public BoldTextMessageDialog(Shell shell, String firstString, String secondString, String boldString) {
super(shell);
this.firstSring = firstString;
this.secondString = secondString;
this.boldString = boldString;
}
@Override
protected Control createDialogArea(Composite parent) {
container = new Composite(parent, SWT.NONE);
container.setLayout(new FormLayout());
final GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
gd.heightHint = 300;
gd.widthHint = 500;
container.setLayoutData(gd);
FormData fd = null;
final Label warningLabel = new Label(container, SWT.NONE);
warningLabel.setImage(Display.getCurrent().getSystemImage(SWT.ICON_WARNING));
fd = new FormData();
fd.left = new FormAttachment(0, 5);
fd.top = new FormAttachment(0, 5);
warningLabel.setLayoutData(fd);
fd = new FormData();
fd.left = new FormAttachment(warningLabel, 10);
fd.right = new FormAttachment(100, 0);
fd.top = new FormAttachment(0, 10);
final StringBuffer buffer = new StringBuffer();
buffer.append(firstSring).append(” “);
buffer.append(boldString).append(” “);
buffer.append(secondString);
final StyledText text = new StyledText(container, SWT.WRAP | SWT.READ_ONLY);
text.setText(buffer.toString());
text.setLayoutData(fd);
text.setBackground(GUIResourceManager
.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
final StyleRange styleRange = new StyleRange(firstSring.length(), boldString.length() + 1, null, null, SWT.BOLD);
text.setStyleRanges(new StyleRange[] { styleRange });
return parent;
}
}
Here Dialog is the Parent class in SWT. which has some predefined methods , now I no need to explain it.
Our main part is create dialog area method, in that method
first interesting class is StyledText, second StyleRange class,
StyledText is framework class, which is used to style the SWT widgets
StyleRange is also framework class, by using this object we can customize the style of our SWT widget.
If you observe above code, for the style range object I am passing that starting index for the bold text as last lenght of the first string, and end index as end of bold string +1.
So, in the middle Bold text will be displayed.
Now I will show you how to achieve this, have a look at the class
public class BoldTextMessageDialog extends Dialog {
private Composite container;
private String firstSring;
private String secondString;
private String boldString;
private Button restoreButton;
private Button cancelButton;
public BoldTextMessageDialog(Shell shell, String firstString, String secondString, String boldString) {
super(shell);
this.firstSring = firstString;
this.secondString = secondString;
this.boldString = boldString;
}
@Override
protected Control createDialogArea(Composite parent) {
container = new Composite(parent, SWT.NONE);
container.setLayout(new FormLayout());
final GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
gd.heightHint = 300;
gd.widthHint = 500;
container.setLayoutData(gd);
FormData fd = null;
final Label warningLabel = new Label(container, SWT.NONE);
warningLabel.setImage(Display.getCurrent().getSystemImage(SWT.ICON_WARNING));
fd = new FormData();
fd.left = new FormAttachment(0, 5);
fd.top = new FormAttachment(0, 5);
warningLabel.setLayoutData(fd);
fd = new FormData();
fd.left = new FormAttachment(warningLabel, 10);
fd.right = new FormAttachment(100, 0);
fd.top = new FormAttachment(0, 10);
final StringBuffer buffer = new StringBuffer();
buffer.append(firstSring).append(” “);
buffer.append(boldString).append(” “);
buffer.append(secondString);
final StyledText text = new StyledText(container, SWT.WRAP | SWT.READ_ONLY);
text.setText(buffer.toString());
text.setLayoutData(fd);
text.setBackground(GUIResourceManager
.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
final StyleRange styleRange = new StyleRange(firstSring.length(), boldString.length() + 1, null, null, SWT.BOLD);
text.setStyleRanges(new StyleRange[] { styleRange });
return parent;
}
}
Here Dialog is the Parent class in SWT. which has some predefined methods , now I no need to explain it.
Our main part is create dialog area method, in that method
first interesting class is StyledText, second StyleRange class,
StyledText is framework class, which is used to style the SWT widgets
StyleRange is also framework class, by using this object we can customize the style of our SWT widget.
If you observe above code, for the style range object I am passing that starting index for the bold text as last lenght of the first string, and end index as end of bold string +1.
So, in the middle Bold text will be displayed.
No comments:
Post a Comment