【问题标题】:SWT Method - Getting a value from the table viewer / Swing Method - Using the valueSWT 方法 - 从表格查看器中获取值 / Swing 方法 - 使用值
【发布时间】:2012-12-05 21:12:31
【问题描述】:

我的 SWT 类中有一个方法可以从我的表中获取选定的值。该值实际上是对象的文件名。

 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;
  }

我在同一类中有一个复合材料,它桥接 SWT 和 Swing。此方法采用字符串文件名并创建一个显示文件的 Swing 查看器。

 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;
}      

如果我尝试在组合中运行 getPDFFileName(),我会收到 SWT 线程错误。我明白这是从哪里来的。

我不确定如何从 getPDFFileName() 获取值并在最终文件查看器中使用它File = new File("NEED FILENAME OF SELECTION");

【问题讨论】:

    标签: java swing swt


    【解决方案1】:

    当您尝试访问小部件(在您的情况下为 Table)时,您需要成为 UI 线程。你可以使用Display.syncExec

      JButton viewerButton = new JButton("View Selected PDF");
      viewerButton.addActionListener(new ActionListener(){
         public void actionPerformed(ActionEvent actionevent) {
            // Retrieve the pdf file name in the UI thread
            final String[] filename = new String[1];
            Display.getCurrent().syncExec(new Runnable() { 
                public void run() {
                    filename[0] = getPDFFileName();
                }
            }
    
            final File viewerFile = new File(filename[0]);
            final AplotPdfViewer pdfv = new AplotPdfViewer(true);
            try {
               pdfv.openFile(viewerFile);
            }
            catch (IOException e) {
               e.printStackTrace();
            }    
         }
      });
    

    如果需要多次调用,请考虑直接在getPDFFileName 方法中调用syncExec。字符串结果保存在数组中,因为您无法返回带有syncExec 的结果。

    【讨论】:

      【解决方案2】:

      我建议您通过添加SelectionChangeListener 来保持对TableViewer 选择的引用。

      当用户从TableViewer 选择输入时,您会在选择侦听器中获得事件并将所选文件名分配给变量。

      在 Swing 代码中,使用此(变量)文件名打开 Pdf 视图。我不建议使用 Display.async 或同步执行 CALLS 来弄乱您的 Swing 代码。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-02-02
        • 2012-05-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多