|
Turn a JTable into a Spreadsheet
Take advantage of Swing's JTable component and its ready-to-use GUIs to display and manage spreadsheet data
by Thierry Manfé
May 2003 Issue
The spreadsheet is part of our everyday lives. Most of us have used one to help us calculate and present data quickly and efficiently. The JTable component, which is available in the Swing package, has itself become popular among Java developers, and you might have already imagined using a JTable as a spreadsheet for your application. Let's see how we can accomplish this and manage the issues related to this implementation such as layout management, editing of a cell, and integration into a larger application.
Certainly you know that the JTable class is a graphical user interface (GUI) component that allows you to display data in a table format. Moreover, JTable works in such a way that the data is managed independently from the GUI, adding a lot more flexibility in the way the GUI can be customized. The model component that wraps the data performs data management. There is a single requirement on the model component associated with the JTable; it must implement the TableModel interface. The AbstractTableModel class provides a default implementation of this interface. Therefore, to implement the spreadsheet, we'll use two key classes: the SpreadSheet class that extends the JTable and the SpreadSheetModel class that extends the AbstractTableModel class. Then we'll wrap up the SpreadSheet class in a JScrollPane—a technique that allows using scroll bars to browse through the spreadsheet when its window is too small to display all of its content.
Before going further, consider the features to be implemented by the spreadsheet. First, because a row number and a column name references every single cell in the spreadsheet, we want to use a row header and a column header to display those numbers and those names. Then, the spreadsheet's cells must be editable. Since JTable has its own editing mechanism, one might think that this feature doesn't require any work. But this default mechanism requires some customization. Recall that a spreadsheet cell has not only an associated value but also has an optional formula used to calculate the value. If a formula is available, it must be displayed for modification as soon as the cell is edited. If there is no formula, the value is displayed instead.
Also, we want to be able to change the foreground and background colors of the cells. To simplify this customization, we want to select the cells by region. In other words, we must be able to select cells grouped in a rectangle that is described by a pair of cells that define two of the rectangle's corners. The cells' colors are then changed all at once. Last, we want the spreadsheet to be updated automatically and the required formulas to be calculated as soon as we modify any of the spreadsheet's cells.
Row, Column Headers
When a JScrollPane is used to encompass the JTable, the column header is created automatically and displayed. The JTable's createDefaultTableHeader() method instantiates a JTableHeader component that displays the columns' names. Then the JTable's configureEnclosingScrollPane() method provides the JScrollPane with this component as the one to be used for the column header. To do so it just needs to call the setColumnHeaderView() method of the scroll pane.
Back to top
|