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:

Comments:

Post a Comment:
Comments are closed for this entry.
About

I'll be writing about topics that would interest users and developers of Sun Java Communication Suite.

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today