See also the thread:
Eclipse SWT & Swing Coexistance - Help!
Caveats:
1) It only works on Windows. Swing/AWT ships with a library that is linked in such a way that it does not export its X/motif libraries and this causes a problem where globals are concerned since SWT tries to use the same libraries.
2) It does not work with light weight widgets. There are issues with the keystrokes getting sent to the light weight widget.
3) SWT_AWT is experimental (in the internal package org.eclipse.swt.internal.awt.win32) and therefore the API may change.
/* Allocate an area for the AWT widgets */
Composite composite = new Composite(shell, SWT.NONE);
composite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL));
Panel awtPanel = SWT_AWT.new_Panel(composite);
awtPanel.setLayout(new java.awt.FlowLayout());
final Label status = new Label(shell, SWT.BORDER | SWT.READ_ONLY);
status.setText("Status information");
status.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
swtButton.addListener(SWT.Selection, new Listener () {
public void handleEvent (Event event) {
status.setText ("SWT Button Selected");
}
});
/* Create AWT and Swing Widgets */
Button awtButton = new Button("AWT Button");
awtPanel.add(awtButton);
awtButton.addActionListener(new ActionListener () {
public void actionPerformed (ActionEvent event) {
display.syncExec (new Runnable () {
public void run () {
status.setText ("AWT Button Selected");
}
});
}
});
JButton jbutton = new JButton("Swing Button");
awtPanel.add(jbutton);
jbutton.addActionListener(new ActionListener () {
public void actionPerformed (ActionEvent event) {
display.syncExec (new Runnable () {
public void run () {
status.setText ("Swing Button Selected");
}
});
}
});
DefaultMutableTreeNode root = new DefaultMutableTreeNode("root");
for (int i = 0; i < 10; i++) {
DefaultMutableTreeNode node = new DefaultMutableTreeNode("node "+i);
root.add(node);
}
final JTree jTree = new JTree(root);
JScrollPane scrollPane = new JScrollPane(jTree);
scrollPane.setPreferredSize(new Dimension(380, 100));
awtPanel.add(scrollPane);
jTree.addTreeSelectionListener(new TreeSelectionListener() {
public void valueChanged(TreeSelectionEvent e) {
display.syncExec (new Runnable () {
public void run () {
TreePath[] paths = jTree.getSelectionPaths();
if (paths == null) {
status.setText ("No items selected");
return;
}
String text = "Tree item(s) selected : ";
for (int i= 0; i < paths.length; i++) {
text += paths[i].getLastPathComponent();
if (i < paths.length - 1)
text += ", ";
}
status.setText (text);
}
});
}
});
final JTable jTable = new JTable(new DefaultEditorModel());
scrollPane = new JScrollPane(jTable);
scrollPane.setPreferredSize(new Dimension(380, 100));
awtPanel.add(scrollPane);
jTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
ListSelectionModel lsm = jTable.getSelectionModel();
lsm.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
display.syncExec (new Runnable () {
public void run () {
int row = jTable.getSelectedRow();
if (row < 0) {
status.setText("No table items selected");
} else {
status.setText("Table item selected : "+jTable.getValueAt(row, 0));
}
}
});
}
});
shell.setSize(700, 300);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) display.sleep ();
}
System.exit(0);
}
}
// Define a model for the Swing table so that the default editors are used for each type
class DefaultEditorModel extends javax.swing.table.AbstractTableModel {
final Object[][] data = "">
{"item 1", new Integer(1), new Boolean(false)},
{"item 2", new Integer(2), new Boolean(true)},
{"item 3", new Integer(3), new Boolean(true)},
{"item 4", new Integer(4), new Boolean(false)},
{"item 5", new Integer(5), new Boolean(false)},
{"item 6", new Integer(6), new Boolean(false)},
{"item 7", new Integer(7), new Boolean(true)},
{"item 8", new Integer(8), new Boolean(false)},
{"item 9", new Integer(9), new Boolean(true)},
{"item 10", new Integer(10), new Boolean(true)},
{"item 11", new Integer(11), new Boolean(true)},
{"item 12", new Integer(12), new Boolean(false)},
{"item 13", new Integer(13), new Boolean(false)},
{"item 14", new Integer(14), new Boolean(true)},
};
final String[] columns = {"A String", "An Integer", "A Boolean"};
public int getColumnCount() {
return columns.length;
}
public int getRowCount() {
return data.length;
}
public String getColumnName(int column) {
return columns[column];
}
public Object getValueAt(int row, int column) {
return data[row][column];
}
public Class getColumnClass(int column) {
return getValueAt(0, column).getClass();
}
public boolean isCellEditable(int row, int column) {
return (column == 2);
}
public void setValueAt(Object value, int row, int column) {
data[row][column] = value;
fireTableCellUpdated(row, column);
}