Ever wondered how SWT Style bits work? It is actually very simple. The style bits are a multitud of 2
SWT.NONE = 0, // 00000000 binary
SWT.MULTI = 2, // 00000010 binary
SWT.SINGLE = 4; // 00000100 binary
SWT.READ_ONLY = 8; // 00001000 binary
…
Via the bitwise OR operator | you combine the bits
SWT.NONE | SWT.MULTI | SWT.READ_ONLY results in 00001010
To get later the information if a bit was set use the bitwise AND operation &.
if (SWT.WRAP==(result & SWT.WRAP)){
System.out.println(“SWT.WRAP available”);
}
Here is a small code snippets to run a little test.
[code type=”java”]
package de.vogella.swt.widgets;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class SWTbitwiseOr {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
new Text(shell, SWT.NONE);
System.out.println(SWT.NONE);
System.out.println(SWT.MULTI);
System.out.println(SWT.SINGLE);
System.out.println(SWT.READ_ONLY);
System.out.println(SWT.WRAP);
System.out.println(SWT.SEARCH);
System.out.println(SWT.ICON_CANCEL);
System.out.println(SWT.ICON_SEARCH);
System.out.println(SWT.LEFT);
System.out.println(SWT.RIGHT);
System.out.println(SWT.PASSWORD);
System.out.println(SWT.CENTER);
// Lets see what we have
int result = SWT.MULTI | SWT.WRAP;
System.out.println(result);
if (SWT.MULTI==(result & SWT.MULTI)){
System.out.println("SWT.MULI available");
}
if (SWT.WRAP==(result & SWT.WRAP)){
System.out.println("SWT.WRAP available");
}
if (SWT.SINGLE==(result & SWT.SINGLE)){
System.out.println("SWT.SINGLE available");
} else {
System.out.println("SWT.SINGLE available");
}
}
}
[/code]
For more information please see Bitwise Operators.
If you are located in Germany you may be interested in my Eclipse RCP training which I deliver together with Ralf Ebert.
3 Responses to SWT Style Bits – Bitwise Or and Bitwise And