Use JGraph to create a Wikipedia browser

Get started with an open-source, Swing-based library for creating graphs

1 2 3 Page 3
Page 3 of 3

Event handling for user interaction

With just a little more work you will have created a mini Wikipedia browser. You simply need to add a way to allow the user to click on a cell and use that event to determine which lemma the user wants to view next.

You do this by adding a MouseListener to the graph. The MouseListener implements the mouseClicked method. When a user clicks on a cell, the browser application will retrieve the data for the lemma the user clicked on, create a new graph, and display it.

Listing 9. Adding the MouseListener

public void mouseClicked(MouseEvent event) {
        if (event.getClickCount() == 1) {
            int x = event.getX();
            int y = event.getY();
            // Check if a cell is present at the clicked coordinates and return it.
           DefaultGraphCell cell = (DefaultGraphCell)graph.getSelectionCellAt(new Point2D.Double(x, y));
            if (cell != null) {
                System.out.println(“Next lemma: “ + cell.toString());

Note that the graph object used in the MouseListener implementation is the graph you have been working on so far.

Saving the image

JGraph supports exporting graphs as images. The Wikipedia browser application has a Save image button that allows a user to export the current graph as an image. Exporting a graph to an image file is easy, as shown in Listing 10.

Listing 10. Exporting a graph to an image file

try {
      BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(fileName));
      BufferedImage image = graph.getImage(graph.getBackground(),
             ImageIO.write(image, "png", out);
         } catch (Exception e) {
            // handle exception

Run the Wikipedia browser

All the essential parts of the program are in place. Running it will show something like what you see in Figure 1.

A screenshot of the Wikipedia browser showing the relationship between lemma and lemmata. Click the thumbnail for a larger image.
Figure 1. A Wikipedia browser display. Click the thumbnail for a larger image.

You can run the sample program to further explore what JGraph has to offer. It is easy to rearrange the layout of the graph by dragging the nodes to a new location. JGraph keeps track of the edges and redraws the edge between two nodes if necessary. JGraph supports grouping of cells and allows a user to move a selected number of cells in a single operation.


In this article you've seen one way of using a graph to show only the relevant data in a much larger data set. In this case, you've created a simple browser to extract data from the Wikipedia Web site and make it available to users in an easy visual format. There are countless other ways to use graphs to simplify data, including the data used for Java application programming. Consider the following uses for graphs:

  • Display the structure of the web.xml file for a Java Web application. This is especially useful if you want to show which filters will be triggered by a request for a certain URL.
  • Relate logging events recorded in a log file to one another.
  • Show relationships between classes and interfaces in a number of Java source files.

The most useful application of graphing to Java development would probably be using graphical editors for application configuration. At the moment a lot of applications use XML-based files to store application configuration data. Some shops then use a Java-XML binding tool like JAXB to create an object tree representing the XML configuration. Using JGraph to visually display that tree would be an improvement on this setup. Being able to change values or even the structure of the data within the visual display (the graph) would make it much easier to change an application's XML configuration, and the JAXB-generated objects could ensure the changes conformed to the XML Schema describing the configuration data.

See the Resources section to learn more about JGraph and download the complete Wikipedia browser created in this article.

Jeroen van Bergen started working in information technology in 1993. He has worked in the fields of billing, systems integration, and order management in the telecommunication industry. He has been programming in Java since 2000. Jeroen is at the moment mainly interested in workflow, Java-XML binding, and automatic document generation using Velocity and XSL:FO.

Learn more about this topic

This story, "Use JGraph to create a Wikipedia browser" was originally published by JavaWorld.

Copyright © 2007 IDG Communications, Inc.

1 2 3 Page 3
Page 3 of 3