Accessing files from inside NetBeans

At IDR Solutions we are constantly working on adding new functionality and improving our applications. In my previous article “Integrating Oracle Documentation into NetBeans IDE with PDF Viewer Plugin” I explained how we added an option to display the NetBeans documentation from inside NetBeans using our PDF Viewer plugin.

Our PDF viewer allows configuration data to be stored in an XML file inside NetBeans jar. So in this article I am going to show you how to access a file (which can be any binary or text file) from inside your code. So here is how we did it:

Create XML file

Creating XML file in NetBeans is very easy. Just right click on your project -> select New -> and XML Document option. Notice the other options as well.

1

If XML Document isn’t displayed select Other -> than select XML option from the left window -> and XML Document option from the right window.

2

Next chose the name for your file -> select the Folder you want to place your XML Document in -> and click Next

3

Chose default and click Next.

4

Now you are in position of adding your code to the XML file.

Adding the Action to read a File

In our action to show the Oracle guide we are reading the file path of the XML file as an InputStream that we are passing as a parameter to the TopComponent. Note the input stream is a location in our jar.

@Override
public void actionPerformed(ActionEvent e) {
 
TopComponent tc;
//this gives me a new window each time
 
InputStream inputFile = this.getClass().getResourceAsStream("/org/jpedal/netbeans/oracle.xml");
 
tc = new PDFDisplayTopComponent("http://docs.oracle.com/cd/E50453_01/doc.80/e50452.pdf", inputFile, PDFViewerTypes.INTERNAL_OPENVIWERFX);
 
tc.open();
tc.requestActive();
 
}

Using in The TopComponent

In the TopComponent we are assigning the value to the properitiesFile

private InputStream propertiesFile=null;

Than we creating PDFDisplayTopComponent method where we assigning 3 parameters.

public PDFDisplayTopComponent(String file, InputStream properties, PDFViewerTypes viewerType){
 
this(viewerType);
 
PDFfile = file;
 
this.propertiesFile=properties;
 
this.setDisplayName(file);
 
}

Next we are adding the functionality to load the propertiesFile. You could add your own code here:

//Root pane which holds JavaFX PDF Viewer
Pane viewerPane = new Pane();
 
fullViewer = new OpenViewerFX(viewerPane, null);
if(propertiesFile!=null){
fullViewer.loadProperties(propertiesFile);
}
fullViewer.setupViewer();
Scene scene = new Scene(viewerPane);

External access of a XML file

The XML file could be also accessed externally. All what you have to do is to sent the path to the XML file as a String parameter in the action functionality:

tc = new PDFDisplayTopComponent("http://docs.oracle.com/cd/E50453_01/doc.80/e50452.pdf", "C:\\Users\\Sylwia\\Desktop\\oracle.xml",PDFViewerTypes.INTERNAL_OPENVIWERFX);

Than in TopComponent assign String value to the properitiesFile

private String propertiesFile=null;

And pass String parameter to the PDFDisplayTopComponent metchod

public PDFDisplayTopComponent(String file, String properties, PDFViewerTypes viewerType){
 
this(viewerType);
 
PDFfile = file;
 
this.propertiesFile=properties;
 
this.setDisplayName(file);
 
}

 

Hopefully you have found this quick guide useful.

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 (Twitter, Facebook and Google+) or the Blog RSS.

Related Posts:

The following two tabs change content below.
Sylwia is a developer at IDR Solutions working on JavaFX Development and on the NetBeans JavaFX Plugin. She is very passionate about programming and aims to improve constantly.
Sylwia

About Sylwia Dorota Kedzia

Sylwia is a developer at IDR Solutions working on JavaFX Development and on the NetBeans JavaFX Plugin. She is very passionate about programming and aims to improve constantly.

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>