20070627 Wednesday June 27, 2007

Viewing a Calendar with JScrollPane (Part 2)

In my blog yesterday, we created a basic JFrame application. In this frame we added a JScrollPane and a JPanel for our calendar data. Today, the Calendar client will start to look like a traditional calendar client. We will cover first JScrollPane Headers, working with date stamps, and also drawing text on a Swing component.

JScrollPane Headers

One of the issues with the JScrollPane's ViewPort is that you can quickly get lost. One way to keep your orientation on a sheet is to place a header for the column (top) and the row (left) that is always visible. The developers of the JScrollPane realized that this was an issue, so they added two headers into this class: the Column Header and the Row Header.

Jscrollpane-Headers

The above diagram (click to enlarge) shows the headers being added to the JScrollPane. I thought it would be good to have the header on top show the current date for this view. On the left side, I will put a list of hours from Midnight (0:00) to Midnight (0:00). As I scroll down, the hour will move, but the Date will remain on the top of the View Port. As I scroll left or right, both the Hour Header and the Date Header will remain in place.

You create these headers by first creating a JPanel for each, then calling either setColumnHeaderView(Component view) or setRowHeaderView(Component view). If you read the JavaDoc API for JScrollPane, you will notice that there is a setColumnHeader(JViewPort view) and setRowHeader(JViewPort view), but I chose to avoid these for now.

The following code will create a new Java file called DateHeader.java. We will be using this JPanel to create a Date label.

/* DateHeader.java */
import java.awt.*;
import java.util.Date;

public class DateHeader extends javax.swing.JPanel {
    Date date;
    
    /** Creates new form DateHeader */
    public DateHeader() {
      /* Calls DateHeader(Date date) constructor */
        this(new Date());
    }

    public DateHeader(Date date){
        setDate(date);
    }              

    public void setDate(Date date) {
        this.date = date;
        repaint();         /* we will talk about this later */
    }

    public Date getDate() {
        return this.date;
    }
    
}
/* DateHeader.java */

We will now add this Column Header Panel into our Calendar Client.

/* Replace the method customComponents() in SampleFrame.java */

    public void customCompoents() {
        this.setSize(450, 300);
        myDataPane = new JPanel();
        myDataPane.setSize(1200, 1200);
        scrollPane = new JScrollPane(myDataPane);
        scrollPane.setSize(300, 150);
        DateHeader dHeader = new DateHeader(new Date());    /* Newly added Column Header Instance */
        scrollPane.setColumnHeaderView(dHeader);            /* Adds Column Header to JScrollPane  */
        myDataPane.setBackground(new Color(255, 255, 204)); /* Set the Data Pane to a nice yellow */
        this.add( scrollPane);
    } 
 
/* end changes for SampleFrame.java */

Now add two lines in your existing SampleFrame.java which we worked on yesterday. In the above, we added an instance of the DateHeader class from DateHeader.java. We initialize the new instance with a new Date object. This will set the label to today's date. We then call setColumnHeaderView(Component view) to attach the JPanel to the JScrollPane. The final thing we add is to change the Data Pane to a nice yellow color.

That's it. The top header is now in the JScrollPane's View Port.

The next section, we will put more data on the Column Header and add a Row Header.

Writing Text on the DateHeader

In the previous section, we defined a new subclass called DateHeader. In this section, I'll add a date stamp to this header, and I'll add two buttons to allow us to navigate forward and back.


We added a new constructor in DateHeader to set the Date object. This object holds the current information for the Date. In itself, we cannot just print the Date object to a string. Instead, we need to use a new object SimpleDateFormat to format the date into a useful date format. This label will be added by overriding the paintComponent(Graphics g) method of the JPanel. You can modify the initialization of SimpleDateFormat to display the date in a manner you desire. For example, you may want to display the date like "dd-MMM-yyyy".

/* Add paintComponent() to DateHeader.java */ 

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        SimpleDateFormat dateStamp = new SimpleDateFormat("MMMM dd, yyyy"); 
        int x = getWidth() / 2 - 150;  // Center the Text by Width
        int y = getHeight() / 2;       // Center the Text by Height
        g.drawString(dateStamp.format(getDate()), x, y);
    }

/* DateHeader.java */

At this point, you should see a header with a date. 

Tomorrow  lets add a Row header.

Technorati Tags:

( Jun 27 2007, 01:20:58 PM PDT ) Permalink del.icio.us technorati digg
20070626 Tuesday June 26, 2007

Viewing a Calendar with JScrollPane

I've been playing around with Swing on my free time. I wanted to update my skills from my previous Java Programming days. In those days (back in 1997), I used the the older AWT-based forms. Today, we have the Swing Toolkit which makes it a lot easier to develop applications.

In the next few days, I will be writing some basic notes and tips for creating a new client using Swing. The client will eventually be a Calendar Client for our Sun Java Calendar Server.

The first topic will be on using JScrollPane. At some point, you will need to present data that will be bigger than the allowable view space.

The following code sets up our example. It defines our application as one that is extending a Swing JFrame class.

import javax.swing.*;

public class SampleFrame extends JFrame {

    /*
     * Constructor for SampleFrame
     */
    public SampleFrame() {
        initializeComponents();
        customComponents();
    }

    /*
     * Constructor for SampleFrame
     */
    public main() {
         SampleFrame sFrame = new SampleFrame();
         sFrame.setVisible(true);
    }
}

You could type the above and compile it from a console window. The easiest way to get started is to download NetBeans and simply create a new project using a JFrame.

One way to display data on a Java Application is to use a container. The container which I'll use for this demonstration is a JScrollPanel and a JPanel. These two Swing containers provide a very large canvas for data (the JPanel) and a view port navigation tool (the JScrollPanel).

Jscrollpane-Overview
The above figure demonstrates a JScrollPane. In my Calendar Client, I really won't be able to display all the information at once. It is useful to allow a user to navigate their view (view port) across a larger calendar. The navigation is done through the use of the scroll bars on the bottom or the right of the view port.

In the next code example, I'll define a JScrollPane and my Data Pane. I'll do this within the JFrame defined above by adding a method called customComponents(). If you are using NetBeans, a method called initializeComponents() will be created automatically.

private JScrollPane scrollPane;
private JPanel      myDataPane;

    public void customCompoents() {
        this.setSize(450, 300);
        myDataPane = new JPanel();
        scrollPane = new JScrollPane(myDataPane);
        scrollPane.setSize(300, 150);
        this.add( scrollPane);
    } 

If you write, compile and run the above, you will likely not see much. This is because the JPanel is pretty much empty. Once we had some interesting stuff into the Data Pane, the scroll bars will appear.

We first resize our JFrame application to a size that is manageable. The next few lines define the JPanel for our Data Pane. We then create our JScrollPane (scrollPane) with myDataPane in the constructor.

Tomorrow.. Part 2 will go into making this more "calendar-like" by adding header information.

Technorati Tags:

( Jun 26 2007, 03:27:01 PM PDT ) Permalink del.icio.us technorati digg