Using the SWT Component

Lee Painter

Setting up

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

<dependencies>
   <dependency>
      <groupId>com.sshtools</groupId>
      <artifactId>terminal-swt</artifactId>
      <version>3.0.0-SNAPSHOT</version>
   </dependency>
</dependencies>
<repositories>
   <repository>
      <id>sshtools</id>
      <name>sshtools</name>
      <url>http://artifactory.javassh.com/opensource-releases</url>
   </repository>
</repositories>

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.events.ShellAdapter;
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);
frame.open();

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())
   frame.getDisplay().sleep();
}
if (!frame.isDisposed())
   frame.dispose();

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:

display.setDefaultBackground(VDUColor.BLACK);
display.setDefaultForeground(VDUColor.GREEN);