Programming Resources
For Fun and Learning
Charles Cusack
Computer Science
Hope College
main

Python
C++

JAVA


PHP
SQL
Alice

TableExample


Description

Run it
Download jar file (Right click)

PowersModel.java
TableExample.java

TableExample.java

//------------------------------------------------------------------------
// TableExample.java
// Written by Chuck Cusack, Jan 2002
// A simple applet to show how to use a JTable with a Model.
//------------------------------------------------------------------------

import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
import java.awt.event.*;
import javax.swing.border.*;

//------------------------------------------------------------------------
public class TableExample extends JApplet implements ActionListener {

   //---------------------------------------------------------------------
   PowersModel powersModel=new PowersModel();  // The table model
   JTable myTable=new JTable(powersModel);     // The table

   //---------------------------------------------------
   // Buttons to change the size of the table
   //
   JButton addColumn=new JButton("Add Column");
   JButton addRow=new JButton("Add Row");
   JButton removeColumn=new JButton("Remove Column");
   JButton removeRow=new JButton("Remove Row");

   //---------------------------------------------------------------------
   public void init() {

      //------------------------------------------------------
      // Make this applet handle the events for the buttons
      //
      addColumn.addActionListener(this);
      addRow.addActionListener(this);
      removeRow.addActionListener(this);
      removeColumn.addActionListener(this);

      //------------------------------------------------------
      // Place the buttons on a box
      //
      Box buttonBox=Box.createHorizontalBox();
      buttonBox.add(Box.createHorizontalStrut(20));
      buttonBox.add(addColumn);
      buttonBox.add(Box.createHorizontalStrut(20));
      buttonBox.add(removeColumn);
      buttonBox.add(Box.createHorizontalStrut(20));
      buttonBox.add(addRow);
      buttonBox.add(Box.createHorizontalStrut(20));
      buttonBox.add(removeRow);
      buttonBox.add(Box.createHorizontalStrut(20));
 
      //------------------------------------------------------
      // Place the table on a scroll pane
      //
      JScrollPane tablePane=new JScrollPane(myTable); 
      myTable.setPreferredScrollableViewportSize(new Dimension(400,400));

      //------------------------------------------------------------------
      // Place everything on the applet
      //
      JLabel title=new JLabel("Powers of Integers");
      title.setHorizontalAlignment(SwingConstants.CENTER);
      this.getContentPane().setLayout(new BorderLayout());
      this.getContentPane().add(title,BorderLayout.NORTH);
      this.getContentPane().add(tablePane,BorderLayout.CENTER);
      this.getContentPane().add(buttonBox,BorderLayout.SOUTH);
      //------------------------------------------------------------------
   }

   //---------------------------------------------------------------------
   // When a button is pushed, do as its name indicates
   //
   public void actionPerformed(ActionEvent e) {
        if(e.getSource()==addColumn) {
           powersModel.addColumn();
        }
        else if(e.getSource()==addRow) {
           powersModel.addRow();
       }
        else if(e.getSource()==removeRow) {
           powersModel.removeRow();
       }
        else if(e.getSource()==removeColumn) {
           powersModel.removeColumn();
       }
   }
   //---------------------------------------------------------------------
}
//------------------------------------------------------------------------