How can I create a simple model to test my JTable?

Scott Stanchfield

I like to use something like the following. It's a fixed table model that just prints the row,column for each cell.

package sample;

import javax.swing.table.AbstractTableModel;


/**
 * A simple table model with hardcoded data
 * Each cell displays (r,c) where r and c
 *   are integer row and column numbers
 * This model helps to show the effect of
 *   an applied filter model
 */
public class FixedTableModel extends AbstractTableModel {
  /**
   * Return the number of columns
   *   in the model -- we explicitly use 6
   */
  public int getColumnCount() {
    return 6;
  }
  /**
   * Return the number of rows
   *   in the model -- we explicitly use 20
   */
  public int getRowCount() {
    return 20;
  }
  /**
   * Return a string that describes the
   *   location in the data
   */
  public Object getValueAt(int rowIndex, 
                           int columnIndex) {
    return columnIndex + "," + rowIndex;
  }
}
<hr>
package sample;

import java.awt.GridLayout;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;

public class Test1 {
  public static void main(String[] args) {
    JFrame f = new JFrame();
    f.getContentPane().setLayout(new GridLayout(1,0));
    f.getContentPane().add(new JScrollPane(new JTable(new FixedTableModel())));
    f.pack();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
  }
}
0 Comments  (click to add your comment)
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact