Using JTables in Frame-based Applications

Usually, numbers shown in tables are right-aligned, but in a JTable by default all data fields are treated as strings and are left-aligned. We include code to right-align the numeric date in the third and fourth columns of our table. The end user can edit the data and there is code to display in a JLabel the changed data.

Most of the code of this demonstration, which follows the screenshot, is based on the simpler examples on a Java page. See the same page for Java code for a TableModel, which we domonstrate on the next page. This Javalobby page was another useful resource.

Application in action

Application in action

The code in table_app1.pas

namespace table_app1;

interface

uses
  java.util, java.awt.*, javax.swing.*;

type
  TableDemo = public class(JFrame, TableModelListener)
  private
    lblChangedValue : JLabel;
    tblMarks : JTable;
    verticalBox : Box;
    sp : JScrollPane;
    columnNames : array[0 .. 3] of String;
    data : array[0 .. 4] of array[0 .. 3] of Object;
  public
    constructor;
    class method Main(args : array of String);
    method tableChanged(e : TableModelEvent);
  end;

implementation

constructor TableDemo;
var
  rightRenderer : DefaultTableCellRenderer;
begin
  verticalBox := Box.createVerticalBox;
  columnNames := ['Forename', 'Surname',  'Theory', 'Practical'];
  data := [['Jo', 'Wood', '55', '86'],
           ['John', 'Bode', '44', '55'],
           ['Kapil', 'Shah', '64', '59'],
           ['Kathy', 'Parr', '91', '78'],
           ['Helen', 'Coe', '51', '43']];

  tblMarks := new JTable(data, columnNames);
  tblMarks.getColumnModel.getColumn(0).setPreferredWidth(100);
  tblMarks.getColumnModel.getColumn(1).setPreferredWidth(100);
  tblMarks.getColumnModel.getColumn(2).setPreferredWidth(80);
  tblMarks.getColumnModel.getColumn(3).setPreferredWidth(80);

  rightRenderer := new DefaultTableCellRenderer;
  rightRenderer.setHorizontalAlignment(SwingConstants.RIGHT);
  tblMarks.getColumnModel.getColumn(2).setCellRenderer(rightRenderer);
  tblMarks.getColumnModel.getColumn(3).setCellRenderer(rightRenderer);
  tblMarks.getModel.addTableModelListener(self);
  tblMarks.getTableHeader.setReorderingAllowed(false);
  sp := new JScrollPane(tblMarks);
  verticalBox.add(sp);

  lblChangedValue := new JLabel('Changed value: ');
  verticalBox.add(lblChangedValue);
  add(verticalBox);

  //Ensure app closes when you click the close button
  DefaultCloseOperation := JFrame.EXIT_ON_CLOSE;
  //Set window caption and size
  Title := 'Table Demo 1';
  Size := new Dimension(400, 150);
  setVisible(true);
end;

class method TableDemo.Main(args : array of String);
begin
  new TableDemo;
end;

method TableDemo.tableChanged(e : TableModelEvent);
var
  strChanged : String;
begin
  if e.getType = TableModelEvent.UPDATE then
    begin
      strChanged := 'Changed value: ' + tblMarks.getValueAt(e.getFirstRow, e.getColumn).toString;
      lblChangedValue.setText(strChanged);
    end;
end;

end.

Programming - a skill for life!

How to use widgets such as JButtons, JRadioButtons, JLists, JTables and JDesktopPanes in Oxygene for Java Applications