Java Web Development

Display Bold, Italic, Strike out fonts in SWT table/tree

Posted on Updated on

SWT(Software Widget toolkit). It’s a java based framework to develop GUI(standalone) applications. This framework is like Java Swings, AWT. SWT has rich set of widgets like tree, tables, groups, sahsforms, composites etc.

Usecase: Let’s say user has some valid data which is going to change dynamically some new data is added, some data is edited, deleted and some unchanged. If user wants to have keep track of which data is changed, deleted, added, etc. then the following technical blog is helpful.

Well, SWT is derived from the Java web development they are related to each other but further you should have SWT jar files if you want to execute this code.

SWT has given direct support to show the font styles, but its up to you how you want to show them.
In this blog we are showing four types of styles

  1.  Bold
  2. Bold and italic
  3. Strike out
  4. No style

SWT Table or tree

Enum:
/**
* The Enum FontStyleStatus.
This ENUM is used to provide the different styles to text
Based on the status
*/
public enum FontStyleStatus
{
UNCHANGED,
ADDED,
RENAMED,
DELETED
}

Codes Snippet to create table:

Display display = new Display();
Shell shell = new Shell(display, SWT.SHELL_TRIM);
shell.setText(“Styled text example”);
shell.setLayout(new FillLayout());

Composite container = new Composite(shell, SWT.NONE);
container.setLayout(new FormLayout());
final FormData containerFd = new FormData();
containerFd.top = new FormAttachment(0, 30);
containerFd.left = new FormAttachment(0, 4);
containerFd.right = new FormAttachment(100, -5);
containerFd.bottom = new FormAttachment(100, 0);
container.setLayoutData(containerFd);

tableViewer = new TableViewer(container, SWT.BORDER | SWT.FULL_SELECTION | SWT.MULTI);
tableViewer.setContentProvider(new ContentProvider());
ColumnViewerToolTipSupport.enableFor(tableViewer, ToolTip.RECREATE);

table = tableViewer.getTable();
final FormData fdFroTable = new FormData();
fdFroTable.top = new FormAttachment(0, 5);
fdFroTable.left = new FormAttachment(0, 4);
fdFroTable.right = new FormAttachment(100, -15);
fdFroTable.bottom = new FormAttachment(100, -35);
table.setLayoutData(fdFroTable);
table.setLinesVisible(true);
table.setHeaderVisible(true);

shell.setSize(400, 400);
shell.open();

final MyStyledLabelProvider labelProvider2 = new MyStyledLabelProvider();
final TableViewerColumn tableViewerColumn = new TableViewerColumn(tableViewer, SWT.NONE);
final TableColumn tableColumn = tableViewerColumn.getColumn();
tableColumn.setWidth(100);
tableViewerColumn.setLabelProvider(new MyStyledLabelProvider2(tableViewer, 0, labelProvider2));
tableColumn.setText(“Column1”);

final TableViewerColumn tableViewerColumn2 = new TableViewerColumn(tableViewer, SWT.NONE);
tableViewerColumn2.setLabelProvider(new MyStyledLabelProvider2(tableViewer, 1, labelProvider2));
final TableColumn tableColumn2 = tableViewerColumn2.getColumn();
tableColumn2.setWidth(100);
tableColumn2.setText(“Column2″);

Collection<ValuObject> valuObjects = new ArrayList<>();
for (int i = 0; i < 20; i++)

{
ValuObject valuObject = new ValuObject();
valuObject.setValue1(” Value ” + i);
valuObject.setValue2(” Value” + i);
if (i % 2 == 0)

{
valuObject.setFontStatus(FontStyleStatus.ADDED);
}

else if (i % 3 == 0)

{
valuObject.setFontStatus(FontStyleStatus.DELETED);
}

else if (i % 5 == 0)

{
valuObject.setValue1(” 1 Before edit Value , After edit “);
valuObject.setValue2(” 2 Before edit Value , After edit”);
valuObject.setFontStatus(FontStyleStatus.RENAMED);
}
valuObjects.add(valuObject);
}

tableViewer.setInput(valuObjects);

while (!shell.isDisposed())

{
if (!display.readAndDispatch())
display.sleep();
}

display.dispose();

Value Holder or Value Object to populate table

private class ValuObject
{
private String value1;
private String value2;
private FontStyleStatus fontStatus;
public String getValue1()
{
return value1;
}
public void setValue1(String value1)
{
this.value1 = value1;
}
public String getValue2()
{
return value2;
}
public void setValue2(String value2)
{
this.value2 = value2;
}
public FontStyleStatus getFontStatus()
{
return fontStatus;
}
public void setFontStatus(FontStyleStatus fontStatus)
{
this.fontStatus = fontStatus;
}
}

 

  • While setting the input values itself set the corresponding style you want to display so that from the label provider we can display
  • For the table viewer columns set styled label provider and this should extend StyledCellLabelProvider otherwise we can’t get the styles.
  • As this class is from the Framework it has update method, we have to override this method to get our desired styles.
  • This label provider will be called for each and every column and each and every cell.

Code snippet for StyledLabelProvider:

public static class MyStyledLabelProvider2 extends StyledCellLabelProvider
{
private MyStyledLabelProvider myStyledLabelProvider;
private int index;
public MyStyledLabelProvider2(Viewer treeViewer, int index, MyStyledLabelProvider myStyledLabelProvider)
{
this.myStyledLabelProvider = myStyledLabelProvider;
this.index = index;
}
/**
* {@inheritDoc}
*/
@Override
public void update(ViewerCell cell)
{
final StyledString styledString = myStyledLabelProvider.getStyledString(cell, index);
if (styledString != null)
{
cell.setText(styledString.toString());
cell.setStyleRanges(styledString.getStyleRanges());
}
super.update(cell);
}
}

As we have override update method, this label provider actually sets the styles to the table cells, from this label provider we need to get the actual styles that’s the reason we passed one more label provider with the corresponding column index.

StyleRange, StyledString are the framework classes to provide styling for the text.
You can have a plentt of tutorials about them.

Code snippet to get actual style

Styler is the framework class which provides the styles for the text like bold, strike out etc.
We have to define the required styles. We have some predefined styles also.

/** The strike out styler. */
private Styler strikeOutStyler = new Styler()
{
@Override
public void applyStyles(TextStyle textStyle)
{
textStyle.strikeout = true;
}
};
/** The bold italic styler. */
private Styler boldItalicStyler = new Styler()
{
@Override
public void applyStyles(TextStyle textStyle)
{
textStyle.font = FontStyles.getBoldItalicFont();
}
};
/**
* Instantiates a new my styled label provider.
*/
public MyStyledLabelProvider()
{
}
public StyledString getStyledString(final ViewerCell cell, int columnIndex)
{
final Object element = cell.getElement();
final StyledString styledString = getStyledString(element, columnIndex);
return styledString;
}
private StyledString getStyledString(final Object element, final int columnIndex)
{
StyledString styledString = null;
switch (columnIndex)
{
case 0:
ValuObject value = (ValuObject) element;
styledString = new StyledString(value.getValue1(), boldItalicStyler);
if (value.getFontStatus() == null ||value.getFontStatus().equals(FontStyleStatus.UNCHANGED))
{
styledString = new StyledString(value.getValue1());
}
else if (value.getFontStatus().equals(FontStyleStatus.DELETED))
{
styledString = new StyledString(value.getValue1(),strikeOutStyler);
}
else if (value.getFontStatus().equals(FontStyleStatus.ADDED))
{
styledString = new StyledString(value.getValue1(),
boldItalicStyler);
}
else if (value.getFontStatus().equals(FontStyleStatus.RENAMED))
{
final String[] strings = value.getValue1().split(“,”);
StyledString styledString2 = new StyledString(strings[1]);
styledString = new StyledString(strings[0], strikeOutStyler);
styledString.append(styledString2);
}
break;
case 1:
ValuObject value2 = (ValuObject) element;
if (value2.getFontStatus() == null ||value2.getFontStatus().equals(FontStyleStatus.UNCHANGED))
{
styledString = new StyledString(value2.getValue2());
}
else if (value2.getFontStatus().equals(FontStyleStatus.DELETED))
{
styledString = new StyledString(value2.getValue2(),
strikeOutStyler);
}
else if (value2.getFontStatus().equals(FontStyleStatus.ADDED))
{
styledString = new StyledString(value2.getValue2(),
boldItalicStyler);
}
else if (value2.getFontStatus().equals(FontStyleStatus.RENAMED))
{
final String[] strings = value2.getValue2().split(“,”);
StyledString styledString2 = new StyledString(strings[1]);
styledString = new StyledString(strings[0], strikeOutStyler);
styledString.append(styledString2);
}
break;
default:
break;
}
return styledString;
}
}
Font Data:
public final static class FontStyles
{
private static FontData[] italicValues;
private static FontData[] boldItalicValues;
public static FontData[] getItalicFontData() {
return italicValues;
}
public static Font getBoldItalicFont()
{
if (getItalicFontData() == null)
{
final Font originalFont = Display.getCurrent().getSystemFont();
boldItalicValues = originalFont.getFontData();
for (int i = 0; i < boldItalicValues.length; i++)
{
boldItalicValues[i].setStyle(boldItalicValues[i].getStyle() |SWT.ITALIC | SWT.BOLD);
}
}
return new Font(Display.getCurrent(), boldItalicValues);
}

}

Snapshot before applying styles:

SWT table

If you observe the above screenshot the there is no styles inside the cells.

Snapshot after applying the styles:

SWT table1

You can observe the text inside the cells are bold, bold-italic, strike out, strike out and normal and normal the various styles are displayed. Even we can change the background, font color also.

Conclusion:

This article discuss about you how to display the text font inside the table or tree with styles by sing styled text, styles and styles label provider class as these are from framework.
This blog has 3 parts:

  1. Table/tree creation.
  2. Creating the table data and while creating data itself we have to specify the style you want to display. It’s just a simulation real time it depends on the functionality.
  3. Setting the styled label provider and overriding the update method. This is very important step, from this method we have to get the actual styles. This method displays actual styles.

Hope this blog is helpful.