Table example

This example shows how to implement a table that retrieves and updates data from an array.

The table read data from an array. When you changes or adds values in the table, the change is reflected in the underlying array.
When you press the ShowArray button, the values from the Array are shown in the textarea.

 

 

Code

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


public class TableFrame extends JFrame
{
  private Object[][] myData;
  private JTextArea showArrayArea;

 public TableFrame()
  { //---------------------------------------------------------------
    //Set frame properties
    //---------------------------------------------------------------
    setSize(600,500);
    setTitle("JTable test");

   //---------------------------------------------------------------
   // Center screen
   //---------------------------------------------------------------
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    Dimension frameSize = this.getSize();
    if (frameSize.height > screenSize.height) {
      frameSize.height = screenSize.height;
    }
    if (frameSize.width > screenSize.width) {
      frameSize.width = screenSize.width;
    }
    this.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);


    Container myContentPane = getContentPane();
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel centerPanel = new JPanel();
    myContentPane.add(centerPanel,"Center");

    //Create layout
    Box vBox = Box.createVerticalBox();

    //---------------------------------------------------------------
    // TABLE SETUP
    //---------------------------------------------------------------

   TableModel myModel = new ItemTableModel();
   JTable myTable = new JTable(myModel);

   //Create scroll pane and add table
   JScrollPane myPane = new JScrollPane(myTable);

   //Setting column width for column 0
   TableColumn column = null;
   column = myTable.getColumnModel().getColumn(0);
   column.setMaxWidth(30);

   //Setting the table size
   myTable.setPreferredScrollableViewportSize(new Dimension(500,200));

   vBox.add(myPane);

   //Create button for displaying array contents
   JButton showArrayButton = new JButton("Show array");
   vBox.add(showArrayButton);

   //Create TextArea for displaying array contents
   showArrayArea = new JTextArea(10,20);
   showArrayArea.setLineWrap(true);
   vBox.add(showArrayArea);

   centerPanel.add(vBox);

   //---------------------------------------------------------------
   // CREATE ARRAY WITH DATA
   //---------------------------------------------------------------
    myData = new Object[3][10];

    myData[0][0] = "10";
    myData[1][0] = "Copper";
    myData[2][0] = "1.000";
    myData[0][1] = "20";
    myData[1][1] = "Iron";
    myData[2][1] = "500";

   //---------------------------------------------------------------
   // Action listener for showArrayButton
   //---------------------------------------------------------------
    showArrayButton.addActionListener(new ActionListener()
      { public void actionPerformed(ActionEvent evt)
        {
          for (int i = 0 ; i <= 9;i++)
          { String lineString = myData[0][i].toString() + " " +
                                myData[1][i].toString() + " " +
                                myData[2][i].toString() + " ##### ";

            showArrayArea.append(lineString);
            //  showArrayArea.insert("H",i);
            showArrayArea.setTabSize(10);
          }



        }
      } );

  } //END - class TableFrame


    //---------------------------------------------------------------
    // TABLE MODEL
    //---------------------------------------------------------------
    class ItemTableModel extends AbstractTableModel
    { String[] columnNames = {"Item", "Material", "Quantity"};

      //Number of columns in the table
      public int getColumnCount() {
         return 3;
      }

      // Number of rows in the table
      public int getRowCount() {
        return 10;
      }

      public String getColumnName(int col) {
        return columnNames[col];

      }

      // Sets the value of a cell
      public Object getValueAt(int row, int col) {
          return myData[col][row];
      }

      // Which cells are editable.
      // It is only necessary to implement this method
      // if the table is editable
      public boolean isCellEditable(int row, int col)
      { return true; //All cells are editable
      }

      // Update cell after it has been edited.
      public void setValueAt(Object value, int row, int col)
      { myData[col][row] = value;
        fireTableCellUpdated(row, col);
      }

   }//END - class ItemTableModel

}



 

 

 

 

Converted from CHM to HTML with chm2web Standard 2.7 (unicode)