I have a method in my SWT class that gets the selected value from my table. The value is actually the file name of the object.
public String getPDFFileName() {
int row = viewer.getTable().getSelectionIndex();
if (row != -1) {
return pdfFileName = AplotSaveDataModel.getInstance().getSelectedPDFFileName(row);
}
else {
MessageDialog.openError(null, "PDF Selection Error Message", "You need to select a PDF to view.");
}
return null;
}
I have a composite in the same class that is bridging SWT and Swing. This method takes the String Filename and creates a Swing Viewer displaying the file.
protected Control createPDFButtons(Composite parent) {
final Composite swtAwtComponent = new Composite(parent, SWT.EMBEDDED);
GridData mainLayoutData = new GridData(SWT.FILL, SWT.CENTER, true, false);
mainLayoutData.horizontalSpan = 1;
swtAwtComponent.setLayoutData(mainLayoutData);
GridLayout mainLayout = new GridLayout(1, false);
mainLayout.marginWidth = 0;
mainLayout.marginHeight = 0;
swtAwtComponent.setLayout(mainLayout);
final Frame frame = SWT_AWT.new_Frame(swtAwtComponent);
final JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
JButton viewerButton = new JButton("View Selected PDF");
viewerButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent actionevent) {
final File viewerFile = new File(getPDFFileName());
final AplotPdfViewer pdfv = new AplotPdfViewer(true);
try {
pdfv.openFile(viewerFile);
}
catch (IOException e) {
e.printStackTrace();
}
}
});
panel.add(viewerButton);
frame.add(panel);
return swtAwtComponent;
}
If I try and run the getPDFFileName() in the composite, I get a SWT thread error. I understand where that comes from.
I am not sure how to get the value from getPDFFileName() and use it in final File viewerFile = new File(“NEED FILENAME OF SELECTION”);
You need to be a UI Thread when you try to access a widget (in your case the
Table). You can do it usingDisplay.syncExecConsider to put the call to
syncExecdirectly in thegetPDFFileNamemethod if it’s needed several times. The String result is saved in an array because you can’t return a result withsyncExec.