Opening A File Into An Editor By Double-Clicking It
By Geertjan on Oct 18, 2011
Let's create a simple text editor and set up a file on disk to be double-clicked for opening into the simple text editor. So, a file will be on disk, I will double-click the file, the simple text editor will start up, and automatically my text file will be open in the simple text editor. I can then immediately start working with the file in the editor.
No coding will be required for all this functionality.
- Create the Application. Start NetBeans IDE, go to the New Project dialog (Ctrl-Shift-N), and choose "NetBeans Modules | NetBeans Platform Application". Click Next. Name the application "SimpleTextEditor" and click Finish.
- Add "Open File" Support to the Application. The NetBeans Platform includes an implementation of a command line parser that will enable you to bind a file to the application's executable. Using your operating system's tools, you'll use, e.g., "Open With" on Ubuntu, to make your executable the executable used to open the file of your choice. Then the NetBeans "OpenFile" command will take the file name you have provided and open the file into the application. This "OpenFile" command line parser is found in the "User Utilities" module in the "ide" cluster.
To include this module in your application, right-click the "SimpleTextEditor" project node in the Projects window, choose "Properties", then select "Libraries" on the left in the Project Properties window, expand "ide", and go down the list of checkboxes until you see "User Utilities", which includes the "OpenFile" command line parser. Check that checkbox and then click the red "Resolve" button at the bottom of the dialog. Other modules will now automatically be selected and included in your application. These other modules provide a plain editor, otherwise you wouldn't be able to open a file into an editor, i.e., if you didn't have an editor in the first place.
- Create an Executable. Our simple text editor with "OpenFile" support is finished. Right-click the "SimpleTextEditor" project node in the Projects window and choose "Package as | ZIP Distribution". In the Files window, browse into the ZIP file that's been created in the "dist" folder and notice that "org-netbeans-modules-utilities.jar" includes a package named "openfile", as you can see below:
Unzip the ZIP file and you have your full application available, including an executable in the "bin" folder.
- Bind a File to the Executable. How a file is bound to an executable depends on the operating system. On Ubuntu, I right-click a file, choose "Open With | Other Application", choose "Use a custom command" at the bottom of the dialog, and then browse to the executable. Then the file automatically opens in the editor, after the editor starts up. Subsequently, any double-click on the bound file will result in the executable starting up and the file opening within it.
I really don't think it could be any simpler than this.