Eclipse SWT

Groups articles regarding eclipse SWT.

In order to get the text of selected TreeItems from a Tree in SWT you can use the following method.

The Tree class already offers a nice function getSelection() which returns an array holding all selected TreeItems.

Source Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/**
 * Returns the text of selected TreeItems in a provided Tree.
 
 * @param tree
 *            a Tree with selected items.
 * @return an array of Strings with the text values of selected TreeItems in
 *         provided Tree.
 */
private String[] getSelectedTreeItems(Tree tree) {
	String[] ret = null;
 
	TreeItem[] items = tree.getSelection();
	ret = new String[items.length];
	for (int i = 0; i < items.length; i++) {
		ret[i] = items[i].getText();
	}
 
	return ret;
}

In case you need to show some web pages in an embedded browser in a Java Rich Client application you can use an SWT component of Eclipse.

This makes it quite easy, if you cannot use JavaFX 2.0 yet in order to do the trick because the client computers are running on older Java versions.

Source Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package dbDesktop;
 
import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
 
public class dbDesktopBrowser {
 
	/**
	 * Launch the application.
	 
	 * @param args
	 */
	public static void main(String[] args) {
		Display display = Display.getDefault();
		Shell shell = new Shell();
		shell.setSize(772, 528);
		shell.setText("SWT Application");
		shell.setLayout(new FillLayout(SWT.HORIZONTAL));
 
		Browser browser = new Browser(shell, SWT.NONE);
		browser.setUrl("https://www.some.url");
		browser.refresh();
 
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
 
	}
}