Core Java Books

Tuesday, 5 November 2013

JComboBox with Highlighted AutoComplete Using Multiple Object Attributes

I haven't posted for a while especially a Swing post so here is one which may prove useful. The standard Swing JComboBox is useful but is a bit bare bones. It doesn't really cater for what the modern user expects. Thanks to Google users now expect to start typing in a text field and for multiple options to appear in the drop down as they type.

This blog sets out to provide that functionality with a slight twist in that the searching can take place on object attributes and as matches are made the matched text is highlighted in the drop down. As an example of how this might be useful, imagine a hardware store which sells many products. Each product has a product code, a name and a description. A customer might come to the shop desk asking do you have any 6 inch nails, now one shop assistant might immediately know the product code and begin to type it and the matching entry would be displayed (see below):

User enters the known product code

Now another assistant might not be aware of the exact product code but knows all nail product codes start with NW so types that and two possible nails are displayed (see below):


User enters NW which all nail product code start with

So the combo box can search on the product code but also it searches on the product name. If a customer comes in to the shop and starts asking about wood, the assistant can enter 'wood' in to the field and a list of product with 'wood' in the name will be shown (yes I know there aren't many products in my shop!)

User enters 'wood' and that matches on two products names

Ah but the customers was only interested in Birch wood. Now in this example somebody forgot to put Birch in the product name but each product also has a description, maybe birch will be in the description of one of the products so the assistant types 'birch':

User enters 'birch' and now a match is found against a products description

So the field has now found a match on a product description, it would have shown multiple products if any other matches had been found. As an example if the user just enters the character 'a' that matches on multiple products via the codes and names.

Match on 'a' matches codes and names

As can be seen the field both autocompletes as the user types and also highlights on the string that it is matching on against each product.


So how is this achieved ?

Firstly I've decided not to copy out the code in the blog, you can check it out by downloading it in the link below. Instead I will describe how the code was put together to achieve what I wanted.

Firstly I created a data class called Product, this simply has get methods on it for code, name and description. For the demo I created a handful of Product object instances to allow the JComboBox to be tested.

I then created a class called ProductWordMatch, this has get methods for matchingString and Product. An instance of ProductWordMatch is created for each Product that has a match on code, name or description for what the user has typed so far. This class is used by the JComboBox model, more on that soon.

I then created a the ProductComboModelHelper class. Simply put this class is given the complete list of Products and when a user types a key is scans though the Products building up a list Products that have a match and for each unique one creates a new instance of ProductWordMatch. It then loads the ProductWordMatch instances into a ListComboBoxModel<ProductWordMatch>. In effect as the user is typing the model used by the JComboBox is being updated.

I next created a RegexTestHighlightPainter, this class extends the SwingX libraries AbstractPainter. This painter is used within the JComboBox drop down to highlight matching text on each matching product. As can be seen by the pictures it highlights the matching text in Orange. The painter is used to paint the background of a JXLabel which is used by the JComboBox renderer.

I finally created the ProductComboBox class which extends JComboBox. This class makes use of unique renderer and editor classes to display the items in the drop down. The Renderer uses two JXLabels, one used on the left for displaying product codes and names and one on the right to display the matching text string, this label also has the RegexTestHighlightPainter set as a background painter. The KeyType and KeyPress methods are also overridden so that as the user types the model is updated using the ProductComboModelHelper and matching data and the editor text are updated as appropriate.

Try the WebStart code to see what it's all about. If it doesn't work for you due to security permission download the code and unzip it so you can take a look and build it yourself.

There are lots of enhancements that could be made to make the code more flexible and also more performant if a lot of products existed. Also the demo simply uses products as a way of showing what the JComboBox is capable of but it could be used for many many other tasks.




Try it out (Webstart security permissions might cause issues with later versions of java):





You can download the code from here have a look and figure it out.

Please give a recommend below if you liked the article.

Saturday, 23 March 2013

I'm Back!

Well it's been a while!

I've was writing a Android application last time I posted on here and then got a contract working on Java Web based development which was all new to me. My Android app is currently in deep freeze but is about 75% complete. I think when it is complete it will be very useful to me and a few other people so some day it will be released. I suspect when I start work on it again there will be a bit of effort bringing up to date with the latest Android changes and I'll have to think about making it good to use on a tablet also.

So whats next on here. Well I've been working on the development of web application which provides a fancy almost application looking web based front end to the user using GXT which is basically GWT with a slight fancier looking style and some extra widget. Check out these technologies they are very smart. Basically you write the browser based front ends in Java and it is compiled in to html and javascript. It very similar to writing Swing type code but it runs in the browser using HTML and Javascript. You don't need to be a HTML or Javascript Ninja which personally I have no intention of being. It's useful to know the basics and perhaps a little CSS to help you with the style of components ut that is about it.

So I plan to write some blogs about what I've been learning and using recently and some general observations about Java server development.

By the way if you are interested in GWT I can highly recommend a new book which was only recently released. It is a bit of a brick however it covers everything in fine detail.

Thursday, 5 January 2012

Swing to Android

Well its been a while since I updated my blog. Basically I finished a contract back in June last year and decided to take some time off. I've been doing anything other that work really :) Actually I have been doing some of my own programming projects to keep my hand in, basically a bit of Swing and a fair bit of Android. I plan to start looking around for new Contracts again shortly.

I plan to have my first Android App out in the next month or so and around that time I will start posting on some of the more technical issues about the Android Eco System and some of the problems I came across and how I resolved them.

In the meantime here is a quick, brief 10,000ft overview/observations on Android from a Swing developers point of view.

Firstly if your coming to Android from Swing you are approaching it with a big advantage to J2EE developers. Now I realise Android doesn't actually have the AWT/Swing API's but that doesn't matter as the general front end/event driven mechanism in it is so close to Swing that you will find it easy. Now Android does rely a lot more on XML defs than Swing ever did and that took me a while to get my head around and realise how powerful a feature it actually it is if used correctly.

I started off by buying a couple of books:




Learning Android
Programming Android

I'd recommend both of these as they cover the basics very well, from then on you can use the online Android SDK docs. Other books I've seen are not great to be honest, you will be better off googling.

Android GUI is like Swing, it's on a single thread, if you do some heavy lifting on that thread it will cause some issue's just like with Swing. There are mechanisms and classes available that like SwingWorker help you out (more of this in later blogs).

A typical Android screen is created by an Activity class that you have extended. This class typically inflates an XML definition on the screen layout. The XML defines the textfields, comboboxes (called Spinners) and so on. Eclipse is typically used to create the XML (I use the Graphical Layout Tool in Eclipse which is still very buggy but ok). You can also hard code the layouts but I wouldn't bother as it's easier to use the XML. The Activities are state driven, they are created, paused, resumed and so on. The states are there because they run on a small device where calls can come in, other programs started an so on. The screens for your app can be hidden, displayed again, killed off and restarted .... You need to handle this and in some cases persist the data.

The Activity states are a bit of a head scratcher to start with but once you try a few examples out its straightforward enough.

Handling Events from components (Views in Android) is so similar to Swing it makes me wonder if somebody copied the event mechanism ;)

Any Activity you create needs to be defined in your applications Manifest file, this file is used by the Android device to figure out you apps main class, the icon, the privileges it needs and so on. If you don't specify an Activity on the manifest then the app will stack trace when you attempt to display it.

Jumping from screen to screen is done by the use of Intents. Intents are used by Android to signal that you want something to happen. You can create an Intent passing in the class that you want to process the Intent (so say an Activity which displays another screen) or you can ask for some operation, so for instance display this web page, Android in this case would open a browser and display the page. If you had several browsers installed it might ask you which one to use. Android keeps an internal stack so that when the back key is pressed then it will switch back from the browser back to your apps Activity (remember the states I mentioned, your Activity has just had its resume state called ;).

Android has some nice features to let you define backgrounds and the look of buttons. This seems to be similar to JavaFX. Basically you can define gradients, patch 9 images (a way of taking a small image and extending it to fit a background nicely), shapes .......

Android comes complete with SQLite3 which is a nice lightweight database. I've found it very quick and nice to use. I'll return to this in another blog as I'm not sure the preferred way to use the database in the most books is actually a good idea.

Finally there is a emulator to test your apps on. Its ok, lets you test things on different versions of android and different device specs (screens, memory, input methods (rollers, soft/hard keyboard)) but its slow and really I'd say if you want to really write and app you need atleast one Android device. I have a nice Nexus S Mmmmmm lovely.

Anyway thats a very brief overview and when I finish my App I will have some time to spend adding some nice new blogs.

Enjoy

Please give a recommend below if you liked the article.

Tuesday, 5 April 2011

Java GUI Application Shutdown Gotcha

In recent times I've had issues with one or two Java GUI application not shutting down when I close them. They seem to stay around as a process, consuming computer resources. Today I got to the bottom of the problem and it's a bit of a nasty gotcha which I wasn't aware of before so I thought I would share it.

In theory when you close a Java application all the threads should be stopped and the process should die. Im my case when I monitored the application the threads I expected to finish such as Swing worker pools were still alive, Strange. The reason turned out to be that the AWT Shutdown thread wasn't terminating all the helper threads, and the reason for this was that there were still AWT Events in the EventQueues. The reason for this is a real sneaky little gatcha, I will explain.

My application used a Thread which had a regular sleep but when woke up would so some calculation and then make a call to update the gui:

Thread updateThread = new Thread(new Runnable() {

  @Override
  public void run() {
    int i = 0;
    do {
      try {
        Thread.sleep(300); // 300ms
        gui.updateValue(SOME_VALUE);
      } catch (InterruptException ex) {
        return;
      }
      frame.setValue(SOMEDATA);
    } while (i++ < 100);
  }


}, "updateThread");

updateThread.setDaemon(true);
updateThread.start();

Now you will notice that the thread returns if it is interrupted and also it is started as a Daemon thread. I had thought that as part of the application shutdown the thread would be terminated but NO it wasn't. This was caused by gui.updateValue(SOME_VALUE) making use of InvokeLater:



  public void updateValue(final int value) {

        // make sure we access graphics in the EDT thread
        java.awt.EventQueue.invokeLater(new Runnable() {

          @Override
            public void run() {
                try {
                     ...
                     ...
                     ...
                    SOME CODE
                } catch (Exception t) {
                    // not a lot to do
                }
            }
        });
    }

The InvokeLater is basically putting an event on the EventQueue and because of this the AWT Shutdown thread want shutdown the application. The AWT Shutdown thread checks the EventQueues every seconds but as you will see my Thread does an update subsecond (300ms) so there is always an event on the Queue! So in short the AWT Shutdown thread never terminates the threads I want it to terminate and so the application needs to be killed.

The work around is simple in the while loop of my thread I also check that the JComonent that is to be updated via it is still visible and shown, if it is not the loop is exited, the thread dies and so there are no more events put on the Event thread and whooohooo the application closes as expected :)


Thread updateThread = new Thread(new Runnable() {

  @Override
  public void run() {
    int i = 0;
    do {
      try {
        Thread.sleep(300); // 300ms
        gui.updateValue(SOME_VALUE);
      } catch (InterruptException ex) {
        return;
      }
      frame.setValue(SOMEDATA);
    }while (i < 100 && progressGlassPane.isVisible() && progressGlassPane.isShowing());
  }
}, "updateThread");
updateThread.setDaemon(true);
updateThread.start();


So in short don't call InvokeLater from a helper thread at sub-second frequency unless you also terminate the thread if the component it is updating is no longer visible!
 
As a side note after I spotted the issue I found this very useful article on the same subject which highlighted the same issue I was having.

Monday, 28 March 2011

Providing Mock API's at Runtime

This article is about how I provide a mock api's to my applications at runtime so I can test them without external services being involved.

Many of the application I work on make use of api's to servers, communications engines and so on. I find it useful to be able to test my application with mock API's so I have more control of the tests on it and can test it standalone. Also the mechanism provided allows me to run my application when a service has yet to be delivered but is defined. I could make use of dependency injection if I was to drag JEE api's into my build but I prefer the simple approach. It might not be perfect but it works.

Lets say I have an external interface that my application makes use of:


public interface Service {
    void addListener(final ServiceListener listener);
    void removeListener(final ServiceListener listener);
}


Now in my application I would typically construct a concrete class either directly  or via a factory method which implements the interface contract. Whilst testing I would like to actually provide another version of the implementing class which can be used to test my application with the interface:


public class MockService implements Service {

    private static List<ServiceListener> listeners = new ArrayList<ServiceListener>();

    public void addListener(ServiceListener listener) {
        listeners.add(listener);
    }

    public void removeListener(ServiceListener listener) {
        listeners.remove(listener);
    }

    public void newRequestReceived(final ExternalRequest request) {
        RequestEvent event = new RequestEvent(request);
        for(ServiceListener listener : listeners) {
            listener.notifyRequestEvent(event);
        }
    }

}

I provide the mock class making use of a command line property:


java -Dservice.api.classname=com.webbyit.MockService -jar myapp.jar



Now in the code of my app I make use of the property (maybe within a factory method) and construct the class provided rather than the default class:


private static Service getService() {
  

    synchronized (ServiceLock) {
        if (Service == null) { 

            String serviceClassName = System.getProperty("service.api.classname");

                if (serviceClassName == null) {
                     Service = new RealService();
                } else {
                    try {
                        Service = (Service) Class.forName(serviceClassName).newInstance();
                    } catch (InstantiationException e) {
                        throwCallServiceInitialisationException(serviceClassName, e);
                    } catch (IllegalAccessException e) {
                        throwCallServiceInitialisationException(serviceClassName, e);
                    } catch (ClassNotFoundException e) {
                        throwCallServiceInitialisationException(serviceClassName, e);
                    }
                }
            }
        }
        return Service;
    } 

}

So what I have done is test for an alternative class being specified by a property (via the command line). If set the code attempts to construct the alternative class and use that instead of the default class usually used by the application.

Monday, 21 March 2011

The Culture of the Locked Stationary Cupboard

This blog is a little bit off topic, it's a short observation of company culture and politics. Specifically this is about the culture of 'The Locked Stationary Cupboard'.

I should start with a little background information. I'm a software consultant, in short I hire out my services to customers and in general develop software for them. This more than often involves working at the customers business premises. Most customers I've dealt with employee 100+ people.

Now when I offer my services to a customer and work on their site I always use my own pens, pencils, calculator ...... however there are times when the pen runs out, the pencil jumps down a black hole and the calculator takes a walk in to the ether. Nothing strange here, all part and parcel of working for yourself. Things get very strange though when you ask a member of staff where the stationary cupboard is though!

I'd always thought of a stationary cupboard as a place where an employee could obtain that most basic of office tools, the pen and pencil. In a well stocked cupboard marker pens, staples, staplers and even rulers were available. All of these items of little monetary value but essential to any office workers tool set. It was in a companies interest to have a well stocked cupboard.

In recent times, and I have noticed this more in larger companies, the stationary cupboard takes on an status similar to that of the 'Ark of the Covenant'. You know it exists, you know you want to find it and open it but by god its not going to be an easy task.

Several obstacles are put in place to stop anyway unwary soul gaining access to the holly of holies. The main obstacle being the BridgeKeeper, you can only pass the bridge and obtain the magical key to the cupboard if you answer all the questions correctly.


  • What do you want.
  • Why do you want it.
  • How many do you want.
  • Has the project manager signed in blood.

Once the key has been obtained and the cupboard door is opened, you feel a sudden chill, the Eye of Sauron. He/She will makes sure that you will NOT take more than you requested. If you are even tempted to take and extra pen, maybe a marker you will never be able to cross the bridge and obtain the golden key again.



So what is it about the stationary cupboard that endows it with such reverence. It seems totally illogical, why would any company employ people, pay them, let them alter code but make it so damn hard to obtain a pen! In many cases developers are allowed to create and alter code that could potentially cause loss of wealth or life if it doesn't function correctly, but a simple request for the keys to the cupboard and they are treated with mistrust.

The crux of the matter I believe is that some companies just have a culture of mistrust. I suspect this is more so in older companies and the Googles of this world are a lot more open minded. Here in the UK we certainly historically come from a culture of 'us and them' with regards to employee and employer relationship. Industry is littered with companies which have gone to the wall because both sides did not trust each other.

My conclusion to this little rant is that any company that has no lock on the stationary cupboard is a forward thinking organisation that is heading in the right direction as regards employee relationships. On the other hand those that protect the jewels within the cupboard should look closely at there internal structure and ask themselves the question, does a pen really warrant all this hassle and mistrust!

Friday, 18 March 2011

WebStart errors and Temporary Internet Files Setting

I've been working on a WebStart application for a few weeks and today it started failing to start. An error was raised about one of the jar files not being signed. I checked the jar and it was signed!

Turned out the error was down to a javaws setting (run 'javaws - viewer' to see the settings).





For some reason the 'Keep Temporary files on my computer' setting was not enabled. I read on various other forums that this had caused similar problems for other people. I enabled the setting, pressed deleted files just incase anything was hanging around and then started my application, it worked :)

So I'm puzzled as to how the setting became disabled as I did not do it. I'm also puzzled why this should cause the problem I was seeing, it seems to have happened to other people to so its not a one off. Some people reported the problem back in 1.4 Java, I'm running the latest 1.6.

Wednesday, 16 March 2011

Java Tables: Simple Column to Row Highlighting

In this post I show a simple solution for highlighting table rows based on the value in a particular column on the row.





(Java 1.6 required!)

Firstly I make use of SwingX libraries which provide highlighting functionality. I replace the normal JTable with a JXTable and then add a Highlighter to the table.

The highlighter (code below) is given a column name and two maps for background and foreground colors. The map is keyed on a String with color values. The highlighter takes the value of the specified column on each row and that is used to get the colors required for highlighting



package com.webbyit.swing;

import java.awt.Color;
import java.awt.Component;
import java.util.Map;
import org.jdesktop.swingx.JXLabel;
import org.jdesktop.swingx.decorator.ComponentAdapter;
import org.jdesktop.swingx.decorator.HighlightPredicate;
import org.jdesktop.swingx.painter.Painter;
import static org.jdesktop.swingx.color.ColorUtil.blend;

/**
 *
 * @author webbyit
 */
public class ColumnToRowHighlighter extends ColorBlendHighlighter {

    protected Map<String, Color> foregrounds;
    protected Map<String, Color> backgrounds;
    protected String columnName;

    public ColumnToRowHighlighter(String columnName, HighlightPredicate predicate) {
        this(columnName, predicate, null, null);
    }

    public ColumnToRowHighlighter(String columnName, HighlightPredicate predicate, Map<String, Color> backgrounds,
                                  Map<String, Color> foregrounds) {
        super(predicate);
        this.backgrounds = backgrounds;
        this.foregrounds = foregrounds;
        this.columnName = columnName;
    }

    public void setForegrounds(Map<String, Color> foregrounds) {
        this.foregrounds = foregrounds;
    }

    public void setBackgrounds(Map<String, Color> backgrounds) {
        this.backgrounds = backgrounds;
    }

    @Override
    protected void applyBackground(Component renderer, ComponentAdapter adapter) {
        if (backgrounds != null) {
            Color color = null;
            if (adapter.isSelected()) {
                color = getBackground();
            } else {
                int columnIndex = adapter.getColumnIndex(columnName);
                String str = adapter.getFilteredStringAt(adapter.row, columnIndex);
                color = backgrounds.get(str);
            }
            if (color != null) {
                if (renderer instanceof JXLabel) {
                    // might have a background painter
                    Painter painter = ((JXLabel) renderer).getBackgroundPainter();
                    renderer.setBackground(blend(renderer.getBackground(), color));
                } else {
                    renderer.setBackground(blend(renderer.getBackground(), color));
                }
            }
        }
    }

    @Override
    protected void applyForeground(Component renderer, ComponentAdapter adapter) {
        if (foregrounds != null) {
            Color color = null;
            if (adapter.isSelected()) {
                color = getSelectedForeground();
            } else {
                int columnIndex = adapter.getColumnIndex(columnName);
                String str = adapter.getFilteredStringAt(adapter.row, columnIndex);
                color = foregrounds.get(str);
            }
            if (color != null) {
                renderer.setForeground(blend(renderer.getForeground(), color));
            }
        }
    }
}

To make use of the highlighter a JXTable must be used instead of a JTable, to be honest I don't use JTables anymore I always use JXTable. This is how the highlighter in the demo is added:



Map<String, Color> backgrounds = new HashMap<String, Color>();
backgrounds.put("Visa", Color.BLUE);
backgrounds.put("Cash", Color.RED);
backgrounds.put("Cheque", Color.BLACK);
Map<String, Color> foregrounds = new HashMap<String, Color>();
foregrounds.put("Visa", Color.WHITE);
foregrounds.put("Cash", Color.YELLOW);
foregrounds.put("Cheque", Color.ORANGE);

// Use default predicate
ColumnToRowHighlighter highlighter = new ColumnToRowHighlighter("Payment Method", HighlightPredicate.ALWAYS,
      backgrounds, foregrounds);
table.addHighlighter(highlighter);

Notice that the highlighter allows a predicate to be set. This is another feature which provides the capability to filter when a highlight is used or not. In the example for instance I could have provided a predicate which defines the highlights is only used for balances outstanding over 50. It's worth searching around for article elsewhere on SwingX and highlighting as its very flexible and very easy to use.

Enjoy.

Tuesday, 15 March 2011

Displaying Application and Java Details in an About JDialog

I've decided that this post will offer a demonstration of a basic about dialog which displays basic information about the application and also about the java platform. Treat it as a starting point for you to extend and tweak.





Download the main frame and dialog source code to see how its done, its very simple. Please note I used Netbeans to create the code.

You could extend the code to display a company logo, provide a copy button on the Java details so they could be posted in an email ...................................

Enjoy :)

Thursday, 10 March 2011

JInternalFrame's Parent & Child Modality

This post is about managing JInternalFrame's within a JDesktopPane, specifically the ability to popup child JInternalFrame's from a parent JInternalFrame's. Now why would you want to do this you may ask? Well on several applications I have worked on users open forms to view/enter data, these forms then offer extra buttons to view related data in other forms. In many cases I require that the new form blocks the parent form, in short becomes modal to the parent form (not other forms).

This post offers code and a demo of a simple extension to JInternalFrame which controls the child and parent relationship. It also offers non desktop modal dialogs, but dialogs that are only modal to the parent JInternalFrame.

The functionality the provided specifically is:

  1. Frames can open child frames. The parent frame becomes busied out (lightweight modal).
  2. Clicking on a busied out parent frame brings the top related child frame to the front.
  3. Multiple parent and child hierarchy's can be on the desktop at one time.
  4. A frame can open a dialog, the dialog can be either desktop modal or modal just to the frame hierarchy it was opened from.
  5. A frame modal dialog will be brought to the front if any of its parent frames are clicked on.
Please try out the demo, the list below gives some idea of the functionality.



Play around with the demo, the menu lets you open many frames that are not related. Each frame then has buttons to let you open different children. Notice how you can click on different children and children of children and also busied out parents, you will see that the top most child frame of the associated from clicked is always brought to the front, even if iconized.

I think many people will find this functionality useful in JDesktopPane related applications

Try it out:




You can download the code from here have a look and figure it out.

Basically I have overridden JInternalFrame and added functionality to listen out for child frames opening and closing. This caused glassplanes to be installed or removed as appropriate.  Check out the logic, its easy(ish) to follow. Let me know if you can think of other uses or extensions to this.


In future I will create a post to demonstrate full modality for internal frames ;)

UPDATE: AUG 2016 - Google have retired the Google Drive Archive :( If you wish to obtain the code please email me and I can send it via email or you can obtain the whole archive here.

Please give a recommend below if you liked the article.

Monday, 7 March 2011

Nimbus L&F JDesktopPane Background Performance Tweak

This blog is for all the people out there using the Nimbus L&F and who find that it has a performance hit when using JDesktopPane components.

I had performance problems on a lightweight application using JDesktopPane's  and I tracked it down to the amount of processing going on when the background of the pane was repainted. Nimbus paints the background using what looks like vector type drawing routines to paint a fancy background pattern. This is all very impressive but on a client with a low(ish) spec processor this has a major impact whenever the user moved or resized JInternalFrame's.

I changed the background Painter used by the JDesktopPane to simply fill the pane with the Nimbus background color. This improved the performance significantly in my case and also left more processing available for the rest of the application to use.

Below is images of the before and after shots on the simple demo application.

Original Nimbus
Updated Background Painter




For the demo code I have simply overriden the updateUI() method on the JDesktopPane to simply provide a new Painter which does a simple fill using the default color.


package com.webbyit.swing;

import com.sun.java.swing.Painter;
import java.awt.Graphics2D;
import javax.swing.JComponent;
import javax.swing.JDesktopPane;
import javax.swing.UIDefaults;
import javax.swing.UIManager;

/**
 * A <code>JDesktopPane</code> that displays a simple fill <code>Color</code> as the
 * background when the nimbus l&f is being used. This saves many process cycles over the normal
 * background painting that makes use of vector drawing.
 *
 * @author webbyit
 */
public class SimpleFillDesktopPane extends JDesktopPane {

    @Override
    public void updateUI() {
        if ("Nimbus".equals(UIManager.getLookAndFeel().getName())) {
            UIDefaults map = new UIDefaults();
            Painter<JComponent> painter = new Painter<JComponent>() {

                @Override
                public void paint(Graphics2D g, JComponent c, int w, int h) {
                    // file using normal desktop color
                    g.setColor(UIManager.getDefaults().getColor("desktop"));
                    g.fillRect(0, 0, w, h);
                }
            };
            map.put("DesktopPane[Enabled].backgroundPainter", painter);
            putClientProperty("Nimbus.Overrides", map);
        }
        super.updateUI();
    }
}



And here is a quick demo for you to try it on your box. You should find if you expand the window to be full frame and move the internal frame around it should redraw fairly quick on a low spec box.




(Java 1.6 required!)

Friday, 4 March 2011

Using Java2D to Create Icons at Runtime

I needed some marker Icons to display unique points on a map. Each icon needed to have a unique id so I wanted a number or letter to be placed on each icon. Obviously I did not want to create and store hundreds of icons in a directory, the simple solution would be to create them on the fly using Java2D.

I'm posting a basic solution which creates some fairly basic marker icons but it is a good starting point for people to start creating more fancy looking markers/icons/images.

Basically I make use of Java2D to draw into BufferedImage and then use the image to construct an ImageIcon.

The example code can be downloaded and creates the following demo images:


Now obviously this isn't created by anyone with artistic talent but it shows the basics.


Click on the following button to try it out.



(Java 1.6 required!)

I'd be interested to hear about any other solutions and any neat ways of extending this.

Thursday, 3 March 2011

Using Google Code to Host WebStart Applications

As you can see from my previous posts I like to provide examples applications and not just some code. Initially I wasn't sure how I could provide WebStart applications via my blog. After some investigation it turned out to be fairly easy, here are some notes about how to go about it.

To start with I obviously needed to create applications that can be delivered as WebStart applications. Personally I use Netbeans for all my development and this make it very easy. You simply select in your projects preferences that it will be packaged as a WebStart application and provide some basic information. When you build a WebStart application a JNLP file will be created and also on e or more Jar files.

Secondly I needed somewhere to host my files. I decided to use project hosting, which is provided by Google Code. You create a project and select the source revision method you wish to use. I picked SVN as its something I know well and is supported by default by Netbeans. Basically this allows you to store you project on the Google Code servers and update them whenever you want.

So now we have a JNLP file, a Jar or Jars and a way of storing these remotely on the Google Code servers.

Now in a JNLP file some of the fields need to specify the location of the JNLP and also the Jars to be used. You need to make sure these are set correctly. In the example below I have highlighted what I needed to change.

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<jnlp codebase="http://yourprojecthost.googlecode.com/svn/trunk/myprojectname" href="youjnlpfilename.jnlp" spec="1.0+">
    <information>
        <title>TitleofYourApp</title>
        <vendor>YourId</vendor>
        <homepage href=""/>
        <description>YourAppDescription</description>
        <description kind="short">Something useful</description>
    </information>
<update check="always"/>
    <resources>
<j2se version="1.5+"/>
<jar href="YourJar.jar" main="true"/>
    </resources>
    <application-desc main-class="xxx.xxx.xxxx.xxxx.youmainclass">
    </application-desc>
</jnlp>



You should change these fields as appropriate for where your application is hosted, the name of the application and the jars and main class to be used.

One other thing you need to make sure the mime type is set correctly for the JNLP file, otherwise any request to obtain it will just be passed a text file, we want it to kick off the Java VM and start running the application. Netbeans allows you to easily add mime types for files by adding it as a SVN property. This needs to be set so the Google Hosting Server know how to deliver the JNLP file back to any requester. The mime type should be set as:

Property name: svn:mime-type 
Property value: application/x-java-jnlp-file

If you don't use Netbeans then you can set the mime type using SVN:

$ svn propset svn:mime-type application/x-java-jnlp-file my.jnlp

Now we have the files hosted, the mime type is set we now need to add the link into our blog/webpage. Oracle/Sun provide a handy standard bit of script for this :) Just add the following into you page:

<script src="http://java.com/js/deployJava.js">
</script>
<script>
      deployJava.createWebStartLaunchButton("https://locationofyourjnlp.jnlp")
</script>


And in theory that is it :)

Enjoy

Please +1 this post or pass it on if you think it is useful.