Using the SWT Component

Lee Painter

Setting up

You will first need to import the terminal-swt module in your maven project.


Creating the Terminal Display

Now create a Java class with main method for execution. You will need the following imports:

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import com.sshtools.terminal.vt.swt.SWTScrollBar;
import com.sshtools.terminal.vt.swt.SWTTerminalPanel;

First off you'll need the actual SWT component and a Shell to put it in. This can be added to your UI as any other component would be.

Shell frame = new Shell();
frame.setLayout(new GridLayout(2, false));
frame.setText("Terminal Emulator");

SWTTerminalPanel display = new SWTTerminalPanel(frame);
display.getControl().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

You probably want a scrollbar to allow scrolling back through previous output:

new SWTScrollBar(frame, display).getNativeComponent().setLayoutData(new GridData(SWT.END, SWT.FILL, false, false));

Now size and display your frame and show it:

frame.setSize(640, 480);;

You will next need to obtain a reference to the Terminal Buffer. See Obtaining The Terminal Buffer in the Native Toolkit Client below.

Don't forget to run the SWT UI event loop if something else is not already doing so:

while (!frame.isDisposed()) {
if (!frame.getDisplay().readAndDispatch())
if (!frame.isDisposed())

You will next need to obtain a reference to the terminal and connect the terminal to a remote host. Return to the article Integrating a Terminal Into Your Application to complete this.

Configure Terminal Colors

You can set the background/foreground colors of the SWT panel using: