Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Java Tip 116: Set your table options -- at runtime!

Enhance the display and usability of JTable

  • Print
  • Feedback
Java Foundation Classes (JFC) offer a rich selection of components for building smart and interactive graphical user interfaces (GUIs). You can display tabular data using the javax.swing.JTable class. In this Java tip, we'll investigate how to solve some common JTable issues.

First, let's define our initial, basic JTable class, MyTable:

import javax.swing.table.*;
import javax.swing.*;
import java.awt.*;
public class MyTable extends JTable{
      //default constructor
      public MyTable(){
           super();
           }
      //constructor to create a table with given number of rows and columns
        public MyTable(int row, int col){
            super(row, col);
            }
     }


Pretty simple! Our initial MyTable implementation is just a stock JTable.

In the following sections, we'll work with various JTable display options -- such as scroll bars, column widths, selection, and other attributes. We'll extend MyTable and incorporate various methods that will support the display features we want to change. Each section adds a new method to the MyTable class, so in the end, we'll have a totally reusable JTable.

Scroll your tables

First, let's use our JTable to show some tabular data. I've created the TableColumnTest class to demonstrate JTable's capabilities:

import javax.swing.table.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
/**Author Sonal Goyal, sonal_goyal@hotmail.com
*/
public class TableColumnTest{
    protected JFrame frame;
    protected JScrollPane scrollpane;
    protected MyTable table;
    public TableColumnTest(){
      //(1) Create the table model.
      DefaultTableModel dm = new DefaultTableModel();
      // Names for each of the columns.
      String[] columnNames = {
            "This is going to be a really long column header",
            "Column B", "Column C", "Column D", "Column E", "Column F",
            "Column G", "Column H", "Column I",  "Column J"
            };
      // The actual data values.
      Integer[][] data = new Integer[8][10];
      // Populate the data matrix.
      for (int row = 0; row < 8; row++){
            for (int col = 0; col < 10; ++col){
                  data[row][col] = new Integer(1000000);
            }
      }
      // Configure the model with the data and column headers.
      dm.setDataVector(data, columnNames);
      //(2) Create the table.
      table = new MyTable();
      //(3) Connect the model to the table.
      table.setModel(dm);
      //(4) Create a scroll pane for the table.
      scrollpane =  new JScrollPane(table);
      //(5) Make the table visible.
      frame =  new JFrame();
      frame.getContentPane().add(scrollpane);
      frame.setSize(200, 150);
      frame.setVisible(true);
  }
public static void main(String[] args){
       TableColumnTest test = new TableColumnTest();
}


The demonstration application is pretty straightforward. We construct a simple JTable by doing the following:

  • Create and configure the TableModel, which has information on rows, columns, column headers, and the actual data

  • Create and configure the JTable, which displays the data from the model

  • Connect the JTable to the model created in the first step



But there is a twist in this first code listing: a scroll pane is added in step 4. We display the constructed and configured table inside a JFrame; see Figure 1 for the scroll results.

  • Print
  • Feedback

Resources