Swing组件添加到ScrollPane(JavaFX)

时间:2016-08-13 20:10:48

标签: java swing pdf javafx

我正在制作将打开pdf文件的JavaFX应用程序。我找到了PDF Viewer的免费库,但它是在Swing中制作的。所以我需要将JPanel添加到ScrollPane(JavaFX)。我试过但没有成功。

我收到了这个错误:

2016年8月13日下午9:59:09 org.icepdf.core.pobjects.Document 警告:在类路径上找不到PDF写入支持。

我在stackoverflow上找到了如何将swing组件添加到javafx窗格,我这样做但是我收到了这个错误。

欢迎任何建议。

package application;

import java.awt.Component;
import java.io.File;
import java.net.MalformedURLException;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import org.icepdf.ri.common.ComponentKeyBinding;
import org.icepdf.ri.common.SwingController;
import org.icepdf.ri.common.SwingViewBuilder;

import javafx.embed.swing.SwingNode;
import javafx.scene.Node;
import javafx.scene.layout.Pane;

    public class PDFView{

    public JPanel viewerComponentPanel;

    public static Node showPDF(File sFiles) throws MalformedURLException {

    String filePath = sFiles.toURI().toURL().toString();

    // build a controller
    SwingController controller = new SwingController();

    // Build a SwingViewFactory configured with the controller
    SwingViewBuilder factory = new SwingViewBuilder(controller);

    // Use the factory to build a JPanel that is pre-configured
    //with a complete, active Viewer UI.
    JPanel viewerComponentPanel = factory.buildViewerPanel();

    // add copy keyboard command
    ComponentKeyBinding.install(controller, viewerComponentPanel);

    // add interactive mouse link annotation support via callback
    controller.getDocumentViewController().setAnnotationCallback(
          new org.icepdf.ri.common.MyAnnotationCallback(
                 controller.getDocumentViewController()));


    final SwingNode swingNode = new SwingNode();
    createAndSetSwingContent(swingNode, viewerComponentPanel);


    // Open a PDF document to view
    controller.openDocument(filePath);
    return swingNode;

}

 private static void createAndSetSwingContent(final SwingNode swingNode, JPanel viewerComponentPanel) {
     SwingUtilities.invokeLater(new Runnable() {
         @Override
         public void run() {
             swingNode.setContent(viewerComponentPanel);
         }
     });
 }

}

这是我从PDFView类

调用方法的主类
for(int i=0;i<fileNumber;i++){

        choosedName=sFiles[i].getName();
        String ext=choosedName.substring(choosedName.lastIndexOf(".") + 1);

        switch (ext) {
        case "doc":  
                 break;
        case "docx": 
                 break;
        case "pdf":                  

            tab = new Tab();
            tab.setText(choosedName);
            s1=new ScrollPane();
            tab.setContent(s1);
            s1.setContent(PDFView.showPDF(sFiles[i]));
        tpane.getTabs().add(tab);

1 个答案:

答案 0 :(得分:1)

我下载了icepdf viewercore个罐子。 并且代码中的变化很小:

//String filePath = sFiles.toURI().toURL().toString();
String filePath = sFiles.getAbsolutePath();

然后,它对我有用,希望也适合你...