Writing a JavaFX PDF Viewer plugin for Netbeans – 3. Display a Window

Introduction

In the part 2, we created a plugin and added an Action which appears on the menu when you run the plugin. Now we are going to make that Action do something.

1. Create a new Window

We want our Action to open a Window in which we can display a PDF file. So we select Menu option with our project again and this time choose Window.

new window

 

2. Choose where the Window appears.

NetBeans allows you to select where in the IDE your Window will appear. We want it in the main Window so we choose Editor. You can also customise how the Window behaves – I have chosen to make it undockable.

window options

 3. Create the actual Java class.

I have given it the name PDFDisplay. You can also set an Icon.

window class

 

4. NetBeans generates the boilerplate code

NetBeans will create the Java code for you including some boilerplate methods.

window code

 

5. Link Action to Window

We can now go back to the Action class and we can add 3 lines of code to link the Action to the Window. This will open a single window in NetBeans for our plugin.

 TopComponent tc = WindowManager.getDefault().findTopComponent("PDFDisplayTopComponent");
        tc.open();
        tc.requestActive();

If you want a window each time the action is called, you could change this code as I have done in the full code below.

@Override
    public void actionPerformed(ActionEvent e) {
 
        TopComponent tc;
        //this gives me a singleton window 
        //tc = WindowManager.getDefault().findTopComponent("PDFDisplayTopComponent");
 
        //this gives me a new window each time 
        tc=new PDFDisplayTopComponent();
 
        tc.open();
        tc.requestActive();
 
    }

actions

If you now run the project, you can open empty windows! NetBeans is taking care of all the plumbing for us with minimal coding effort from us.

blank window

 

6.Get jpedalFX jars

To complete this tutorial, we are going to use our PDF library to show the PDF file in the Window. You will need to download the jpedalFX.jar from our website (click here) and add the jar as a Wrapped jar to your project as below.

jar

 

If you ever need to alter or update the jar you will need to remove and then re-add the jar back to your project. Otherwise the changes will not be included.

8. Add in JavaFX support

I strongly recommend you use Java 8 for writing any code which calls JavaFX. Otherwise you will have issues with needing to include a wrapped jfxrt.jar and platform issues. If you do not have Java8, click here. I will explain how you can get it to run under Java7 next time.

9. And finally….

The actual integration of JavaFX in Swing deserves its own article so that is where we will continue next time. In the meantime, here is a little teaser (and Leonard’s book is well worth a read if you are interested in the PDF file format)….
pdf

Click here for the next article in the series.

This post is part of our “NetBeans article Index” series. In these articles, we aim to explore NetBeans in different ways, from useful hint and tips, to our how-to’s, experiences and usage of the NetBeans IDE.

If you’re a first-time reader, or simply want to be notified when we post new articles and updates, you can keep up to date by social media (TwitterFacebook and Google+) or the Blog RSS.

 

Related Posts:

The following two tabs change content below.

Mark Stephens

System Architect and Lead Developer at IDRSolutions
Mark Stephens has been working with Java and PDF since 1999 and has diversified into HTML5, SVG and JavaFX. He also enjoys speaking at conferences and has been a Speaker at user groups, Business of Software, Seybold and JavaOne conferences. He has a very dry sense of humor and an MA in Medieval History for which he has not yet found a practical use.
Markee174

About Mark Stephens

Mark Stephens has been working with Java and PDF since 1999 and has diversified into HTML5, SVG and JavaFX.

He also enjoys speaking at conferences and has been a Speaker at user groups, Business of Software, Seybold and JavaOne conferences. He has a very dry sense of humor and an MA in Medieval History for which he has not yet found a practical use.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>