Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Thursday, May 13, 2010

Using DJ NativeSwing in Netbeans

In my previous post I linked to a library that allows the use of native components within Java applications as easily as using Swing components. At the time of this post I had managed to get the Web Browser sample to compile and run in Netbeans 6.8 but was having troubles getting the HTML Editor to compile in Netbeans.

Over the last couple of days I have managed to get the HTML Editor compiled and running in Netbeans 6.8.

This post will demonstrate how to configure Netbeans to use the DJ NativeSwing library to embed a HTML Editor into a Java Swing Application.

The first step is to download a copy of the libraries required for DJ NativeSwing to function.

DJ NativeSwing
SWT - requires the M6 development build to function correctly.
FCKEditor- requires FCKEditor not the CKEditor Download
FCKEditor Java Plugin

Once you have downloaded all the class libraries unzip them to a handy directory. I use c:\java_util\ and create a separate directory for each class library.

The FCKEditor ZIP does not require unzipping. The FCKEditor java plugin takes care of this.

Open Netbeans and create a new Java Application and add the downloaded libraries to the project. Once completed the libraries tab should look like the image below.


Now that the project has been created you need to create the two class files the project requires. Rather than have masses of code in the post, I have included these class files for you to download.

HTMLEditor.java- this is the class that includes the main class. Refer to image below for this class. This was copied directly from the sample HTMLEditor code in the JavaWebstart sample on the DJ NativeSwing site.


SimpleHTMLEditorExample.java- this is the HTMLEditor class that extends JPanel and builds the HTML Editor control.

This class was slightly changed as it returned a compiler error in the state it was in on the DJ NativeSwing site.

In the screenshot below you can see where the call to create the new JHTMLEditor is returning an error. To make this code work correctly requires a change to specify what HTMLEditor implementation is to be used.

JHTMLEditor htmlEditor = new JHTMLEditor(JHTMLEditor.HTMLEditorImplementation.FCKEditor);


Once these minor changes have been completed, it should be possible to compile and run the project and get a HTML Editor with WYSIWYG editing.

I have included a zipped file of the Netbeans Project for people to try out.

Enjoy.

Tuesday, May 11, 2010

DJ NativeSwing Class Library

Would you like to include an embedded browser, media player, flash player or html editor in your application. Then the DJ NativeSwing class libraries may be what you are looking for. This is a JAVA library built on SWT to allow the use of native components in Java applications as easily as using the Swing component library.

I have been experimenting with this library and have managed to get the Web Browser and HTML Editor samples to compile and run in Netbeans 6.8.

Over the next couple of weeks I will be preparing some posts about how to configure the projects to be able to work in Netbeans.

Thursday, April 8, 2010

Latitude/Longitude Converter Class Library

A few weeks ago I found an old photograph taken while we were doing a 4wd tour on the Mid North Coast of NSW. I wanted to scan and post this photo to the blog along with a Google Maps location.

The issue with this was I only had a rough idea where the photo was taken. During this 4wd tour we followed the directions in a printed book. This was 10 years ago and we did not have the use of a GPS to record our track. All I had to go on was the directions in the book. These directions made little sense trying to follow them on Google Maps as in many cases the tracks we followed are not named or marked in Google Maps.

However, the book did give GPS co-ordinates for key points on the tracks (major intersections or landmarks). I decided these could be used to provide a rough path followed and allow me to get my bearings while trying to follow the directions on Google Maps.

Once I tried to plot each of these points in Google Earth I realised it would be a slow process to import each of these points. An easier way would be to create a KML file of the points and load that into Google Earth. I had already written some code to produce a KML file of points from a CSV file so it was just a matter of loading the GPS co-ordinates into the CSV file and running the program.Unfortunately, KML files need GPS co-ordinates in Decimal Degrees and all my co-ordinates were in Degrees, Minutes and Seconds.

At this point I could have used any number of web based conversion pages to convert each co-ordinate manually. Instead, I decided to write a JAVA class library to convert co-ordinates between Degrees, Minutes and Seconds and Decimal Degrees.

The code was reasonably simple but as I had not been able to find an equivalent class on the net I decided that I would try to make it as versatile as possible with the aim to releasing it onto the web for anybody to use. It would also be a good learning exercise for me as I have created a Java class library previously.

After a weeks worth of coding I have now got the library to the point that I feel comfortable releasing a alpha version of the library to the net. There is a simple tutorial available and I am currently developing the Javadocs to go with the library.

Eventually, I will also post the source under an open source license to allow others to build or improve the library.

Sunday, March 7, 2010

Programming or Configuration?

A recent post over at the Reinvigorated Programmer highlighted an interesting point about the nature of the programming that many modern coders do. The post discusses the difference between creating an application from scratch and coding much of the functionality yourself or using frameworks and libraries to create much of the code.

As a VB5/6 programmer who has recently started coding in JAVA I have found that the choice of libraries available both in the standard class libraries and in third party libraries was very large and it is sometimes difficult to know if you are creating something from scratch that may already be available in the standard libraries. Every day I find myself searching the libraries to ensure I am not re-writing the wheel.


Another point the blog post touched on is the widespread use of frameworks in modern coding. These ready built code bases are used to simplify the coding of applications by hiding the underlying code and allowing the user to configure the system with very little actual programming.

Overall, the use of these frameworks is a good thing as they do minimize the production of a lot of code that is common from one application to the next. My biggest gripe with frameworks is that there are so many of them and more are being created every day. Especially in the Java world it is difficult to determine what is the best choice amongst the hundreds of frameworks. However, once selected it can be difficult to change frameworks without considerable work.

From a learning perspective the large number of libraries and frameworks can be daunting and given the communities willingness to create new libraries and frameworks on a rapid basis I guess I will be learning a new library or framework most of the time. 

Thursday, March 4, 2010

JAVA - Writing a StringBuffer to a File

During some recent development work I was using a StringBuffer to hold a large string (>400K characters). This string was built using a variety of methods based upon data being read in from a CSV file.

Once the StringBuffer had been created I was going to write the StringBuffer out to a file. This is where I had an issue. For some reason I thought that stringBuffer included a method to write the information to a file. It doesn't.

I then went looking for an easy way to write the contents of the StringBuffer to a text file. After looking at a few forum posts I managed to determine the simplest way to write the StringBuffer to the file.

StringBuffer output = new StringBuffer();
// Get the correct Line Separator for the OS (CRLF or LF)
String nl = System.getProperty("line.separator");
String filename = "output.txt";
output.append("Write First Line to StringBuffer");
output.append(nl);
output.append("Write Second Line to StringBuffer");

try {
  BufferedWriter out = new BufferedWriter(
                       new FileWriter(filename));
  String outText = output.toString();
  out.write(outText);
  out.close();
    }
catch (IOException e)
    {
    e.printStackTrace();
    }

The interesting parts of this code is the use of System.getProperties to get the correct line separator for the OS the code is running on and the use of the BufferedWriter in conjunction with a FileWriter to write the StringBuffer to the file.